Skip to content

Commit

Permalink
[#218] Test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aeqz committed Dec 8, 2022
1 parent df214aa commit ae0094c
Showing 1 changed file with 50 additions and 78 deletions.
128 changes: 50 additions & 78 deletions tests/Test/Xrefcheck/RedirectRequestsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Universum

import Data.CaseInsensitive qualified as CI
import Data.Map qualified as M
import Network.HTTP.Types (Status, mkStatus, permanentRedirect308, temporaryRedirect307)
import Network.HTTP.Types (Status, mkStatus)
import Network.HTTP.Types.Header (hLocation)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty (TestName, TestTree, testGroup)
import Test.Tasty.HUnit (Assertion, testCase)
import Web.Firefly (ToResponse (toResponse), route, run)

Expand All @@ -21,87 +21,59 @@ import Xrefcheck.Verify

test_redirectRequests :: TestTree
test_redirectRequests = testGroup "Redirect response tests"
[ testCase "307 no location pass by default" $
redirectAssertion
temporaryRedirect307
"http://127.0.0.1:5000/redirect"
Nothing
(VerifyResult []),
testCase "307 with location pass by default" $
redirectAssertion
temporaryRedirect307
"http://127.0.0.1:5000/redirect"
(Just "http://127.0.0.1:5000/other")
(VerifyResult []),
testCase "302 no location pass by default" $
redirectAssertion
(mkStatus 302 "Temporary redirect")
"http://127.0.0.1:5000/redirect"
Nothing
(VerifyResult []),
testCase "302 with location pass by default" $
redirectAssertion
(mkStatus 302 "Temporary redirect")
"http://127.0.0.1:5000/redirect"
(Just "http://127.0.0.1:5000/other")
(VerifyResult []),
testCase "303 no location pass by default" $
redirectAssertion
(mkStatus 303 "Temporary redirect")
"http://127.0.0.1:5000/redirect"
Nothing
(VerifyResult []),
testCase "303 with location pass by default" $
redirectAssertion
(mkStatus 303 "Temporary redirect")
"http://127.0.0.1:5000/redirect"
(Just "http://127.0.0.1:5000/other")
(VerifyResult []),
testCase "308 no location fail by default" $
redirectAssertion
permanentRedirect308
"http://127.0.0.1:5000/redirect"
Nothing
(VerifyResult [PermanentRedirectError "http://127.0.0.1:5000/redirect" Nothing]),
testCase "308 with location fail by default" $
redirectAssertion
permanentRedirect308
"http://127.0.0.1:5000/redirect"
(Just "http://127.0.0.1:5000/other")
(VerifyResult [PermanentRedirectError "http://127.0.0.1:5000/redirect" (Just "http://127.0.0.1:5000/other")]),
testCase "301 no location fail by default" $
redirectAssertion
(mkStatus 301 "Permanent redirect")
"http://127.0.0.1:5000/redirect"
Nothing
(VerifyResult [PermanentRedirectError "http://127.0.0.1:5000/redirect" Nothing]),
testCase "301 with location fail by default" $
redirectAssertion
(mkStatus 301 "Permanent redirect")
"http://127.0.0.1:5000/redirect"
(Just "http://127.0.0.1:5000/other")
(VerifyResult [PermanentRedirectError "http://127.0.0.1:5000/redirect" (Just "http://127.0.0.1:5000/other")])
[ testGroup "Temporary" $ temporaryRedirectTests <$> [302, 302, 307]
, testGroup "Permanent" $ permanentRedirectTests <$> [301, 308]
]
where
redirectAssertion :: Status -> Text -> Maybe Text -> VerifyResult VerifyError -> Assertion
redirectAssertion status link redirect res@(VerifyResult errors) =
url :: Text
url = "http://127.0.0.1:5000/redirect"

location :: Maybe Text
location = Just "http://127.0.0.1:5000/other"

temporaryRedirectTests :: Int -> TestTree
temporaryRedirectTests statusCode =
redirectTests
(show statusCode <> " passes by default")
(mkStatus statusCode "Temporary redirect")
(const Nothing)

permanentRedirectTests :: Int -> TestTree
permanentRedirectTests statusCode =
redirectTests
(show statusCode <> " fails by default")
(mkStatus statusCode "Permanent redirect")
(Just . PermanentRedirectError url)

redirectTests :: TestName -> Status -> (Maybe Text -> Maybe VerifyError) -> TestTree
redirectTests name expectedStatus expectedError =
testGroup name
[
testCase "With no location" $
redirectAssertion expectedStatus Nothing (expectedError Nothing),
testCase "With location" $
redirectAssertion expectedStatus location (expectedError location)
]

redirectAssertion :: Status -> Maybe Text -> Maybe VerifyError -> Assertion
redirectAssertion expectedStatus expectedLocation expectedError =
checkLinkAndProgressWithServer
(mockRedirect redirect status)
link
(Progress
{ pTotal = 1
, pCurrent = 1
, pErrorsUnfixable = length errors
, pErrorsFixable = 0
, pTaskTimestamp = Nothing
}
)
res
(mockRedirect expectedLocation expectedStatus)
url
(Progress
{ pTotal = 1
, pCurrent = 1
, pErrorsUnfixable = length $ maybeToList expectedError
, pErrorsFixable = 0
, pTaskTimestamp = Nothing
}
)
(VerifyResult $ maybeToList expectedError)

mockRedirect :: Maybe Text -> Status -> IO ()
mockRedirect location status =
mockRedirect expectedLocation expectedSocation =
run 5000 $ route "/redirect" $ pure $ toResponse
( "" :: Text
, status
, M.fromList [(CI.map (decodeUtf8 @Text) hLocation, maybeToList location)]
, expectedSocation
, M.fromList [(CI.map (decodeUtf8 @Text) hLocation, maybeToList expectedLocation)]
)

0 comments on commit ae0094c

Please sign in to comment.