Skip to content

Commit

Permalink
[#25] Config redirect tests
Browse files Browse the repository at this point in the history
Adding some config redirect tests.
  • Loading branch information
aeqz committed Dec 21, 2022
1 parent b1dc0ec commit a395963
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Xrefcheck/Scan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module Xrefcheck.Scan
, normaliseExclusionConfigFilePaths
, scanRepo
, specificFormatsSupport
, defaultCompOption
, defaultExecOption
, ecIgnoreL
, ecIgnoreLocalRefsToL
, ecIgnoreRefsFromL
Expand Down
138 changes: 138 additions & 0 deletions tests/Test/Xrefcheck/RedirectConfigSpec.hs
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"
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- SPDX-License-Identifier: MPL-2.0
-}

module Test.Xrefcheck.RedirectRequestsSpec where
module Test.Xrefcheck.RedirectDefaultSpec where

import Universum

Expand All @@ -21,7 +21,7 @@ import Xrefcheck.Progress
import Xrefcheck.Verify

test_redirectRequests :: TestTree
test_redirectRequests = testGroup "Redirect response tests"
test_redirectRequests = testGroup "Redirect response defaults"
[ testGroup "Temporary" $ temporaryRedirectTests <$> [302, 303, 307]
, testGroup "Permanent" $ permanentRedirectTests <$> [301, 308]
]
Expand Down

0 comments on commit a395963

Please sign in to comment.