From d56116d4c9c7ea398ed21a3c6be5f52cf410672c Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Fri, 22 Oct 2021 14:29:15 -0400 Subject: [PATCH] implemented encodeRequest Note: this does not yet handle percent-escaped characters --- src/Network/GemServ.hs | 21 ++++++++++++++++++++- test/Network/GemServSpec.hs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/Network/GemServ.hs b/src/Network/GemServ.hs index be34ef5..6f9946d 100644 --- a/src/Network/GemServ.hs +++ b/src/Network/GemServ.hs @@ -24,6 +24,25 @@ License along with this program. If not, see -} -module Network.GemServ where +module Network.GemServ ( + encodeRequest +) where + +import Data.List (intercalate) + +import Network.GemServ.Types + +-- | Encodes a 'Request' into a 'String' +encodeRequest :: Request -> String +encodeRequest req = + "gemini://" ++ authority ++ "/" ++ path ++ query + where + authority = reqHost req ++ case reqPort req of + Just port -> ':' : show port + Nothing -> "" + path = intercalate "/" $ reqPath req + query = case reqQuery req of + "" -> "" + q -> '?' : q --jl diff --git a/test/Network/GemServSpec.hs b/test/Network/GemServSpec.hs index c8d7e3d..4cde415 100644 --- a/test/Network/GemServSpec.hs +++ b/test/Network/GemServSpec.hs @@ -22,9 +22,37 @@ License along with this program. If not, see module Network.GemServSpec (spec) where -import Test.Hspec (Spec, describe) +import Test.Hspec (Spec, context, describe, it, shouldBe) + +import Network.GemServ +import Network.GemServ.Types spec :: Spec -spec = describe "Network.GemServ" $ return () +spec = describe "Network.GemServ" + encodeRequestSpec + +encodeRequestSpec :: Spec +encodeRequestSpec = describe "encodeRequest" $ mapM_ + ( \(desc, req, expected) -> context desc $ + it ("should be " ++ show expected) $ + encodeRequest req `shouldBe` expected + ) + + -- description, request, expected + [ ( "simple", simpleReq, simpleExp ) + , ( "with port", withPortReq, withPortExp ) + , ( "with path", withPathReq, withPathExp ) + , ( "with query", withQueryReq, withQueryExp ) + ] + + where + 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" --jl