gemcap/test/Network/GemServSpec.hs

99 lines
2.9 KiB
Haskell
Raw Normal View History

2021-10-22 12:49:03 -04:00
{-
gemserv
Cooyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with this program. If not, see
<https://www.gnu.org/licenses/>.
-}
module Network.GemServSpec (spec) where
import Test.Hspec (Spec, context, describe, it, shouldBe)
import Network.GemServ
import Network.GemServ.Types
2021-10-22 12:49:03 -04:00
spec :: Spec
2021-10-22 16:40:44 -04:00
spec = describe "Network.GemServ" $ do
encodeRequestSpec
2021-10-22 16:40:44 -04:00
escapeStringSpec
2021-10-24 12:47:37 -04:00
unescapeStringSpec
encodeRequestSpec :: Spec
encodeRequestSpec = describe "encodeRequest" $ mapM_
( \(desc, req, expected) -> context desc $
it ("should be " ++ show expected) $
encodeRequest req `shouldBe` expected
)
2021-10-22 16:51:04 -04:00
-- description, request, expected
[ ( "simple", simpleReq, simpleExp )
, ( "with port", withPortReq, withPortExp )
, ( "with path", withPathReq, withPathExp )
, ( "with query", withQueryReq, withQueryExp )
, ( "with escape", withEscapeReq, withEscapeExp )
]
where
2021-10-22 16:51:04 -04:00
simpleReq = newRequest "example.com"
simpleExp = "gemini://example.com/"
withPortReq = simpleReq { reqPort = Just 1965 }
withPortExp = "gemini://example.com:1965/"
withPathReq = simpleReq { reqPath = ["foo", "bar"] }
withPathExp = "gemini://example.com/foo/bar"
withQueryReq = simpleReq { reqQuery = "foo" }
withQueryExp = "gemini://example.com/?foo"
withEscapeReq = simpleReq
{ reqPath = ["foo bar"]
, reqQuery = "baz quux"
}
withEscapeExp = "gemini://example.com/foo%20bar?baz%20quux"
2021-10-22 12:49:03 -04:00
2021-10-22 16:40:44 -04:00
escapeStringSpec :: Spec
escapeStringSpec = describe "escapeString" $ mapM_
( \(input, expected) -> context (show input) $
it ("should be " ++ show expected) $
escapeString input `shouldBe` expected
)
-- input, expected
[ ( "~foo-bar_baz.quux", "~foo-bar_baz.quux" )
, ( "foo:/?=&#%", "foo%3a%2f%3f%3d%26%23%25" )
, ( "foo\xe9", "foo%c3%a9" )
2021-10-22 16:40:44 -04:00
]
2021-10-24 12:47:37 -04:00
unescapeStringSpec :: Spec
unescapeStringSpec = describe "unescapeString" $ mapM_
( \(input, expected) -> context (show input) $
it ("should be " ++ show expected) $
unescapeString input `shouldBe` expected
)
-- input, expected
[ ( "foo", Just "foo" )
, ( "foo%20bar", Just "foo bar" )
, ( "foo%7x", Just "foo%7x" )
, ( "foo%a", Just "foo%a" )
, ( "foo%", Just "foo%" )
, ( "foo%c3%a9", Just "foo\xe9" )
, ( "foo%ff", Nothing )
]
2021-10-22 12:49:03 -04:00
--jl