-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding some config redirect tests.
- Loading branch information
Showing
3 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
{- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io> | ||
- | ||
- SPDX-License-Identifier: MPL-2.0 | ||
-} | ||
|
||
module Test.Xrefcheck.RedirectConfigSpec where | ||
|
||
import Universum | ||
|
||
import Data.CaseInsensitive qualified as CI | ||
import Data.Map qualified as M | ||
import Network.HTTP.Types (mkStatus) | ||
import Network.HTTP.Types.Header (hLocation) | ||
import Test.Tasty (TestTree, testGroup) | ||
import Test.Tasty.HUnit (testCase) | ||
import Text.Regex.TDFA.Text qualified as R | ||
import Web.Firefly (App, ToResponse (toResponse), route, run) | ||
|
||
import Test.Xrefcheck.UtilRequests | ||
import Xrefcheck.Config | ||
import Xrefcheck.Progress | ||
import Xrefcheck.Scan | ||
import Xrefcheck.Verify | ||
|
||
test_redirectRequests :: TestTree | ||
test_redirectRequests = testGroup "Redirect chain tests" | ||
[ testGroup "Match" | ||
[ testGroup "By \"to\"" | ||
[ testCase "Do match" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule Nothing (Just RROPermanent) RROValid] []) | ||
mockRedirect | ||
"http://127.0.0.1:5000/permanent-redirect" | ||
(progress 0) | ||
(VerifyResult []) | ||
, testCase "Do not match" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule Nothing (Just RROPermanent) RROValid] []) | ||
mockRedirect | ||
"http://127.0.0.1:5000/temporary-redirect" | ||
(progress 1) | ||
(VerifyResult [ExternalHttpResourceUnavailable (mkStatus 302 "Redirect")]) | ||
] | ||
, testGroup "By \"on\"" | ||
[ testCase "Do match" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule (rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/permanent-.*$") Nothing RROValid] []) | ||
mockRedirect | ||
"http://127.0.0.1:5000/permanent-redirect" | ||
(progress 0) | ||
(VerifyResult []) | ||
, testCase "Do not match" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule (rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/permanent-.*$") (Just RROPermanent) RROValid] []) | ||
mockRedirect | ||
"http://127.0.0.1:5000/temporary-redirect" | ||
(progress 1) | ||
(VerifyResult [ExternalHttpResourceUnavailable (mkStatus 302 "Redirect")]) | ||
] | ||
, testGroup "By \"to\" and \"on\"" | ||
[ testCase "Do match" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule (rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/follow.*$") (Just (RROCode 307)) RROValid] []) | ||
mockRedirect | ||
"http://127.0.0.1:5000/follow3" | ||
(progress 0) | ||
(VerifyResult []) | ||
, testCase "Do not match" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule (rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/follow.*$") (Just (RROCode 307)) RROValid] []) | ||
mockRedirect | ||
"http://127.0.0.1:5000/follow2" | ||
(progress 1) | ||
(VerifyResult [ExternalHttpResourceUnavailable (mkStatus 302 "Redirect")]) | ||
] | ||
, testCase "By any" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule Nothing Nothing RROValid] []) | ||
mockRedirect | ||
"http://127.0.0.1:5000/follow1" | ||
(progress 0) | ||
(VerifyResult []) | ||
] | ||
, testGroup "Chain" | ||
[ testCase "End valid" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule Nothing Nothing RROFollow] []) | ||
mockRedirect | ||
"http://127.0.0.1:5000/follow1" | ||
(progress 0) | ||
(VerifyResult []) | ||
, testCase "End invalid" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule Nothing (Just (RROCode 307)) RROInvalid, RedirectRule Nothing Nothing RROFollow] []) | ||
mockRedirect | ||
"http://127.0.0.1:5000/follow1" | ||
(progress 1) | ||
(VerifyResult [RedirectRuleError (Just (RROCode 307)) ("http://127.0.0.1:5000/follow3" :| ["http://127.0.0.1:5000/follow2", "http://127.0.0.1:5000/follow1"]) (Just "http://127.0.0.1:5000/ok")]) | ||
, testCase "Mixed with ignore" $ | ||
checkLinkAndProgressWithServer | ||
(config [RedirectRule Nothing (Just (RROCode 307)) RROInvalid, RedirectRule Nothing Nothing RROFollow] (maybeToList $ rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/follow3$")) | ||
mockRedirect | ||
"http://127.0.0.1:5000/follow1" | ||
(progress 0) | ||
(VerifyResult []) | ||
] | ||
] | ||
where | ||
progress :: Int -> Progress Int | ||
progress errors = Progress | ||
{ pTotal = 1 | ||
, pCurrent = 1 | ||
, pErrorsUnfixable = errors | ||
, pErrorsFixable = 0 | ||
, pTaskTimestamp = Nothing | ||
} | ||
|
||
config :: [RedirectRule] -> [R.Regex] -> Config | ||
config rules exclussions = localConfig | ||
& cNetworkingL . ncExternalRefRedirectsL .~ rules | ||
& cExclusionsL . ecIgnoreExternalRefsToL .~ exclussions | ||
|
||
redirectRoute :: Text -> Int -> Maybe Text -> App () | ||
redirectRoute name code to = route name $ pure $ toResponse | ||
( "" :: Text | ||
, mkStatus code "Redirect" | ||
, M.fromList [(CI.map (decodeUtf8 @Text) hLocation, fmap ("http://127.0.0.1:5000" <>) $ maybeToList to)] | ||
) | ||
|
||
mockRedirect :: IO () | ||
mockRedirect = | ||
run 5000 do | ||
route "/ok" $ pure $ toResponse ("Ok" :: Text) | ||
redirectRoute "/permanent-redirect" 301 $ Just "/ok" | ||
redirectRoute "/temporary-redirect" 302 $ Just "/ok" | ||
redirectRoute "/follow1" 301 $ Just "/follow2" | ||
redirectRoute "/follow2" 302 $ Just "/follow3" | ||
redirectRoute "/follow3" 307 $ Just "/ok" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters