escape characters in URLs

This commit is contained in:
Jonathan Lamothe 2021-10-22 16:51:04 -04:00
parent 8590219126
commit 3484fda01e
2 changed files with 23 additions and 15 deletions

View File

@ -42,10 +42,10 @@ encodeRequest req =
authority = reqHost req ++ case reqPort req of authority = reqHost req ++ case reqPort req of
Just port -> ':' : show port Just port -> ':' : show port
Nothing -> "" Nothing -> ""
path = intercalate "/" $ reqPath req path = intercalate "/" $ map escapeString $ reqPath req
query = case reqQuery req of query = case reqQuery req of
"" -> "" "" -> ""
q -> '?' : q q -> '?' : escapeString q
-- | add required escape sequences to a string -- | add required escape sequences to a string
escapeString :: String -> String escapeString :: String -> String

View File

@ -39,22 +39,30 @@ encodeRequestSpec = describe "encodeRequest" $ mapM_
encodeRequest req `shouldBe` expected encodeRequest req `shouldBe` expected
) )
-- description, request, expected -- description, request, expected
[ ( "simple", simpleReq, simpleExp ) [ ( "simple", simpleReq, simpleExp )
, ( "with port", withPortReq, withPortExp ) , ( "with port", withPortReq, withPortExp )
, ( "with path", withPathReq, withPathExp ) , ( "with path", withPathReq, withPathExp )
, ( "with query", withQueryReq, withQueryExp ) , ( "with query", withQueryReq, withQueryExp )
, ( "with escape", withEscapeReq, withEscapeExp )
] ]
where where
simpleReq = newRequest "example.com" simpleReq = newRequest "example.com"
simpleExp = "gemini://example.com/" simpleExp = "gemini://example.com/"
withPortReq = simpleReq { reqPort = Just 1965 } withPortReq = simpleReq { reqPort = Just 1965 }
withPortExp = "gemini://example.com:1965/" withPortExp = "gemini://example.com:1965/"
withPathReq = simpleReq { reqPath = ["foo", "bar"] } withPathReq = simpleReq { reqPath = ["foo", "bar"] }
withPathExp = "gemini://example.com/foo/bar" withPathExp = "gemini://example.com/foo/bar"
withQueryReq = simpleReq { reqQuery = "foo" } withQueryReq = simpleReq { reqQuery = "foo" }
withQueryExp = "gemini://example.com/?foo" withQueryExp = "gemini://example.com/?foo"
withEscapeReq = simpleReq
{ reqPath = ["foo bar"]
, reqQuery = "baz quux"
}
withEscapeExp = "gemini://example.com/foo%20bar?baz%20quux"
escapeStringSpec :: Spec escapeStringSpec :: Spec
escapeStringSpec = describe "escapeString" $ mapM_ escapeStringSpec = describe "escapeString" $ mapM_