-
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.
Browse files
Browse the repository at this point in the history
…nagement [#218] Change redirects default behaviour
- Loading branch information
Showing
7 changed files
with
217 additions
and
57 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
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,79 @@ | ||
{- SPDX-FileCopyrightText: 2021 Serokell <https://serokell.io> | ||
- | ||
- SPDX-License-Identifier: MPL-2.0 | ||
-} | ||
|
||
module Test.Xrefcheck.RedirectRequestsSpec where | ||
|
||
import Universum | ||
|
||
import Data.CaseInsensitive qualified as CI | ||
import Data.Map qualified as M | ||
import Network.HTTP.Types (Status, mkStatus) | ||
import Network.HTTP.Types.Header (hLocation) | ||
import Test.Tasty (TestName, TestTree, testGroup) | ||
import Test.Tasty.HUnit (Assertion, testCase) | ||
import Web.Firefly (ToResponse (toResponse), route, run) | ||
|
||
import Test.Xrefcheck.UtilRequests | ||
import Xrefcheck.Progress | ||
import Xrefcheck.Verify | ||
|
||
test_redirectRequests :: TestTree | ||
test_redirectRequests = testGroup "Redirect response tests" | ||
[ testGroup "Temporary" $ temporaryRedirectTests <$> [302, 303, 307] | ||
, testGroup "Permanent" $ permanentRedirectTests <$> [301, 308] | ||
] | ||
where | ||
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 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 expectedLocation expectedSocation = | ||
run 5000 $ route "/redirect" $ pure $ toResponse | ||
( "" :: Text | ||
, expectedSocation | ||
, M.fromList [(CI.map (decodeUtf8 @Text) hLocation, maybeToList expectedLocation)] | ||
) |
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,75 @@ | ||
{- SPDX-FileCopyrightText: 2019 Serokell <https://serokell.io> | ||
- | ||
- SPDX-License-Identifier: MPL-2.0 | ||
-} | ||
|
||
module Test.Xrefcheck.UtilRequests | ||
( checkLinkAndProgressWithServer | ||
, verifyLink | ||
, verifyReferenceWithProgress | ||
) where | ||
|
||
import Universum | ||
|
||
import Control.Exception qualified as E | ||
import Data.Map qualified as M | ||
import Text.Interpolation.Nyan | ||
|
||
import Control.Concurrent (forkIO, killThread) | ||
import Test.Tasty.HUnit (assertBool) | ||
import Xrefcheck.Config | ||
import Xrefcheck.Core | ||
import Xrefcheck.Progress | ||
import Xrefcheck.Scan | ||
import Xrefcheck.Util | ||
import Xrefcheck.Verify | ||
|
||
checkLinkAndProgressWithServer | ||
:: IO () | ||
-> Text | ||
-> Progress Int | ||
-> VerifyResult VerifyError | ||
-> IO () | ||
checkLinkAndProgressWithServer mock link progress vrExpectation = | ||
E.bracket (forkIO mock) killThread $ \_ -> do | ||
(result, progRes) <- verifyLink link | ||
flip assertBool (result == vrExpectation) $ | ||
[int|| | ||
Verification results differ: expected | ||
#{interpolateIndentF 2 (show vrExpectation)} | ||
but got | ||
#{interpolateIndentF 2 (show result)} | ||
|] | ||
flip assertBool (progRes `progEquiv` progress) $ | ||
[int|| | ||
Expected the progress bar state to be | ||
#{interpolateIndentF 2 (show progress)} | ||
but got | ||
#{interpolateIndentF 2 (show progRes)} | ||
|] | ||
where | ||
-- Check whether the two @Progress@ values are equal up to similarity of their essential | ||
-- components, ignoring the comparison of @pTaskTimestamp@s, which is done to prevent test | ||
-- failures when comparing the resulting progress, gotten from running the link | ||
-- verification algorithm, with the expected one, where @pTaskTimestamp@ is hardcoded | ||
-- as @Nothing@. | ||
progEquiv :: Eq a => Progress a -> Progress a -> Bool | ||
progEquiv p1 p2 = and [ ((==) `on` pCurrent) p1 p2 | ||
, ((==) `on` pTotal) p1 p2 | ||
, ((==) `on` pErrorsUnfixable) p1 p2 | ||
, ((==) `on` pErrorsFixable) p1 p2 | ||
] | ||
|
||
verifyLink :: Text -> IO (VerifyResult VerifyError, Progress Int) | ||
verifyLink link = do | ||
let reference = Reference "" link Nothing (Position Nothing) | ||
progRef <- newIORef $ initVerifyProgress [reference] | ||
result <- verifyReferenceWithProgress reference progRef | ||
p <- readIORef progRef | ||
return (result, vrExternal p) | ||
|
||
verifyReferenceWithProgress :: Reference -> IORef VerifyProgress -> IO (VerifyResult VerifyError) | ||
verifyReferenceWithProgress reference progRef = do | ||
fmap wrlItem <$> verifyReference | ||
(defConfig GitHub & cExclusionsL . ecIgnoreExternalRefsToL .~ []) FullMode | ||
progRef (RepoInfo M.empty mempty) "." "" reference |
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