Skip to content

Commit

Permalink
[#25] Refactor tests
Browse files Browse the repository at this point in the history
Removing some repetitive code from tests.
  • Loading branch information
aeqz committed Dec 21, 2022
1 parent a395963 commit 0615ba3
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions tests/Test/Xrefcheck/RedirectConfigSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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 Web.Firefly (App, Status, ToResponse (toResponse), route, run)

import Test.Xrefcheck.UtilRequests
import Xrefcheck.Config
Expand All @@ -30,54 +30,54 @@ test_redirectRequests = testGroup "Redirect chain tests"
checkLinkAndProgressWithServer
(config [RedirectRule Nothing (Just RROPermanent) RROValid] [])
mockRedirect
"http://127.0.0.1:5000/permanent-redirect"
(link "/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"
(link "/temporary-redirect")
(progress 1)
(VerifyResult [ExternalHttpResourceUnavailable (mkStatus 302 "Redirect")])
(VerifyResult [ExternalHttpResourceUnavailable (status 302)])
]
, testGroup "By \"on\""
[ testCase "Do match" $
checkLinkAndProgressWithServer
(config [RedirectRule (rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/permanent-.*$") Nothing RROValid] [])
(config [RedirectRule (regex "^.*/permanent-.*$") Nothing RROValid] [])
mockRedirect
"http://127.0.0.1:5000/permanent-redirect"
(link "/permanent-redirect")
(progress 0)
(VerifyResult [])
, testCase "Do not match" $
checkLinkAndProgressWithServer
(config [RedirectRule (rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/permanent-.*$") (Just RROPermanent) RROValid] [])
(config [RedirectRule (regex "^.*/permanent-.*$") (Just RROPermanent) RROValid] [])
mockRedirect
"http://127.0.0.1:5000/temporary-redirect"
(link "/temporary-redirect")
(progress 1)
(VerifyResult [ExternalHttpResourceUnavailable (mkStatus 302 "Redirect")])
(VerifyResult [ExternalHttpResourceUnavailable (status 302)])
]
, testGroup "By \"to\" and \"on\""
[ testCase "Do match" $
checkLinkAndProgressWithServer
(config [RedirectRule (rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/follow.*$") (Just (RROCode 307)) RROValid] [])
(config [RedirectRule (regex "^.*/follow.*$") (Just (RROCode 307)) RROValid] [])
mockRedirect
"http://127.0.0.1:5000/follow3"
(link "/follow3")
(progress 0)
(VerifyResult [])
, testCase "Do not match" $
checkLinkAndProgressWithServer
(config [RedirectRule (rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/follow.*$") (Just (RROCode 307)) RROValid] [])
(config [RedirectRule (regex "^.*/follow.*$") (Just (RROCode 307)) RROValid] [])
mockRedirect
"http://127.0.0.1:5000/follow2"
(link "/follow2")
(progress 1)
(VerifyResult [ExternalHttpResourceUnavailable (mkStatus 302 "Redirect")])
(VerifyResult [ExternalHttpResourceUnavailable (status 302)])
]
, testCase "By any" $
checkLinkAndProgressWithServer
(config [RedirectRule Nothing Nothing RROValid] [])
mockRedirect
"http://127.0.0.1:5000/follow1"
(link "/follow1")
(progress 0)
(VerifyResult [])
]
Expand All @@ -86,34 +86,34 @@ test_redirectRequests = testGroup "Redirect chain tests"
checkLinkAndProgressWithServer
(config [RedirectRule Nothing Nothing RROFollow] [])
mockRedirect
"http://127.0.0.1:5000/follow1"
(link "/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"
(link "/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")])
(VerifyResult [RedirectRuleError (Just (RROCode 307)) (link "/follow3" :| [link "/follow2", link "/follow1"]) (Just (link "/ok"))])
, testCase "Mixed with ignore" $
checkLinkAndProgressWithServer
(config [RedirectRule Nothing (Just (RROCode 307)) RROInvalid, RedirectRule Nothing Nothing RROFollow] (maybeToList $ rightToMaybe $ R.compile defaultCompOption defaultExecOption "^.*/follow3$"))
(config [RedirectRule Nothing (Just (RROCode 307)) RROInvalid, RedirectRule Nothing Nothing RROFollow] (maybeToList (regex "^.*/follow3$")))
mockRedirect
"http://127.0.0.1:5000/follow1"
(link "/follow1")
(progress 0)
(VerifyResult [])
]
]
where
progress :: Int -> Progress Int
progress errors = Progress
{ pTotal = 1
, pCurrent = 1
, pErrorsUnfixable = errors
, pErrorsFixable = 0
, pTaskTimestamp = Nothing
}
link :: Text -> Text
link = ("http://127.0.0.1:5000" <>)

regex :: Text -> Maybe R.Regex
regex = rightToMaybe . R.compile defaultCompOption defaultExecOption

status :: Int -> Status
status code = mkStatus code "Redirect"

config :: [RedirectRule] -> [R.Regex] -> Config
config rules exclussions = localConfig
Expand All @@ -123,10 +123,19 @@ test_redirectRequests = testGroup "Redirect chain tests"
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)]
, status code
, M.fromList [(CI.map (decodeUtf8 @Text) hLocation, fmap link $ maybeToList to)]
)

progress :: Int -> Progress Int
progress errors = Progress
{ pTotal = 1
, pCurrent = 1
, pErrorsUnfixable = errors
, pErrorsFixable = 0
, pTaskTimestamp = Nothing
}

mockRedirect :: IO ()
mockRedirect =
run 5000 do
Expand Down

0 comments on commit 0615ba3

Please sign in to comment.