-
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.
Some tests for testing errors related to redirect chains: broken chains and cycles.
- Loading branch information
Showing
4 changed files
with
116 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
{- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io> | ||
- | ||
- SPDX-License-Identifier: MPL-2.0 | ||
-} | ||
|
||
module Test.Xrefcheck.RedirectChainSpec 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 Web.Firefly (App, ToResponse (toResponse), route, run) | ||
|
||
import Test.Xrefcheck.UtilRequests | ||
import Xrefcheck.Config | ||
import Xrefcheck.Progress | ||
import Xrefcheck.Verify | ||
|
||
test_redirectRequests :: TestTree | ||
test_redirectRequests = testGroup "Redirect chain tests" | ||
[ testCase "Broken" $ | ||
checkLinkAndProgressWithServer | ||
config | ||
mockRedirect | ||
brokenStart | ||
progress | ||
(VerifyResult [RedirectChainBroken brokenEnd brokenStack]) | ||
, testCase "Cycle" $ | ||
checkLinkAndProgressWithServer | ||
config | ||
mockRedirect | ||
cycleStart | ||
progress | ||
(VerifyResult [RedirectChainCycle cycleEnd cycleStack]) | ||
] | ||
where | ||
brokenStart :: Text | ||
brokenStart = "http://127.0.0.1:5000/broken1" | ||
|
||
brokenEnd :: Text | ||
brokenEnd = "http://127.0.0.1:5000/broken3" | ||
|
||
brokenStack :: [Text] | ||
brokenStack = fmap ("http://127.0.0.1:5000" <>) ["/broken2", "/broken1"] | ||
|
||
cycleStart :: Text | ||
cycleStart = "http://127.0.0.1:5000/cycle1" | ||
|
||
cycleEnd :: Text | ||
cycleEnd = "http://127.0.0.1:5000/cycle2" | ||
|
||
cycleStack :: [Text] | ||
cycleStack = fmap ("http://127.0.0.1:5000" <>) ["/cycle4", "/cycle3", "/cycle2", "/cycle1"] | ||
|
||
progress :: Progress Int | ||
progress = Progress | ||
{ pTotal = 1 | ||
, pCurrent = 1 | ||
, pErrorsUnfixable = 1 | ||
, pErrorsFixable = 0 | ||
, pTaskTimestamp = Nothing | ||
} | ||
|
||
config :: Config | ||
config = localConfig | ||
& cNetworkingL . ncExternalRefRedirectsL .~ [RedirectRule Nothing Nothing RROFollow] | ||
|
||
redirectRoute :: Text -> Maybe Text -> App () | ||
redirectRoute name to = route name $ pure $ toResponse | ||
( "" :: Text | ||
, mkStatus 301 "Permanent redirect" | ||
, M.fromList [(CI.map (decodeUtf8 @Text) hLocation, fmap ("http://127.0.0.1:5000" <>) $ maybeToList to)] | ||
) | ||
|
||
mockRedirect :: IO () | ||
mockRedirect = | ||
run 5000 do | ||
-- A set of redirect routes that correspond to a broken chain. | ||
redirectRoute "/broken1" $ Just "/broken2" | ||
redirectRoute "/broken2" $ Just "/broken3" | ||
redirectRoute "/broken3" Nothing | ||
|
||
-- A set of redirect routes that correspond to a cycle. | ||
redirectRoute "/cycle1" $ Just "/cycle2" | ||
redirectRoute "/cycle2" $ Just "/cycle3" | ||
redirectRoute "/cycle3" $ Just "/cycle4" | ||
redirectRoute "/cycle4" $ Just "/cycle2" |
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
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