implemented stripCRLF

This commit is contained in:
Jonathan Lamothe 2021-11-24 19:07:58 -05:00
parent 05e83857a7
commit 391ffd3eea
2 changed files with 26 additions and 5 deletions

View File

@ -132,7 +132,10 @@ readMax maxLen conn = do
-- | Strips the CR/LF characters from the end of a string, retuning -- | Strips the CR/LF characters from the end of a string, retuning
-- Nothing if they are not present -- Nothing if they are not present
stripCRLF :: String -> Maybe String stripCRLF :: String -> Maybe String
stripCRLF = undefined stripCRLF = \case
"" -> Nothing
"\r\n" -> Just ""
c:str -> (c:) <$> stripCRLF str
readLoop :: Int -> S.InputStream BS.ByteString -> MaybeT IO Builder readLoop :: Int -> S.InputStream BS.ByteString -> MaybeT IO Builder
readLoop maxLen src = lift (S.read src) >>= \case readLoop maxLen src = lift (S.read src) >>= \case

View File

@ -31,7 +31,14 @@ import Data.Connection (Connection (..))
import Data.IORef (IORef, modifyIORef', newIORef, readIORef) import Data.IORef (IORef, modifyIORef', newIORef, readIORef)
import Data.X509 (Certificate (..)) import Data.X509 (Certificate (..))
import System.IO.Streams (nullInput, unRead) import System.IO.Streams (nullInput, unRead)
import Test.Hspec (Spec, context, describe, it, shouldReturn, xit) import Test.Hspec (
Spec,
context,
describe,
it,
shouldBe,
shouldReturn,
xit)
import Network.Gemini.Capsule.Types import Network.Gemini.Capsule.Types
import Network.Gemini.Capsule.Internal import Network.Gemini.Capsule.Internal
@ -83,7 +90,7 @@ runConnectionSpec = describe "runConnection" $ mapM_
readURLSpec :: Spec readURLSpec :: Spec
readURLSpec = describe "readURL" $ mapM_ readURLSpec = describe "readURL" $ mapM_
( \(desc, ioConn, expect) -> context desc $ ( \(desc, ioConn, expect) -> context desc $
xit ("should return " ++ show expect) $ it ("should return " ++ show expect) $
do do
conn <- ioConn conn <- ioConn
readURL conn `shouldReturn` expect readURL conn `shouldReturn` expect
@ -115,7 +122,7 @@ sendResponseSpec = describe "sendResponse" $ return ()
strFromConnSpec :: Spec strFromConnSpec :: Spec
strFromConnSpec = describe "strFromConn" $ mapM_ strFromConnSpec = describe "strFromConn" $ mapM_
( \(desc, maxLen, ioConn, expect) -> context desc $ ( \(desc, maxLen, ioConn, expect) -> context desc $
xit ("should return " ++ show expect) $ do it ("should return " ++ show expect) $ do
conn <- ioConn conn <- ioConn
strFromConn maxLen conn `shouldReturn` expect strFromConn maxLen conn `shouldReturn` expect
) )
@ -154,7 +161,18 @@ readMaxSpec = describe "readMax" $ mapM_
longBS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" longBS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
stripCRLFSpec :: Spec stripCRLFSpec :: Spec
stripCRLFSpec = describe "stripCRLF" $ return () stripCRLFSpec = describe "stripCRLF" $ mapM_
( \(input, expected) -> context (show input) $
it ("should be" ++ show expected) $
stripCRLF input `shouldBe` expected
)
-- input, expectation
[ ( "foo\r\n", Just "foo" )
, ( "foo\n", Nothing )
, ( "foo", Nothing )
, ( "\r\n", Just "" )
]
mkIOConn :: [BS.ByteString] -> IO (Connection (), IORef [BSL.ByteString]) mkIOConn :: [BS.ByteString] -> IO (Connection (), IORef [BSL.ByteString])
mkIOConn input = do mkIOConn input = do