@@ -1,6 +1,8 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module Servant.API.StreamSpec where
2module Servant.API.StreamSpec where
4+import Control.Monad.Except
4import qualified Data.Aeson as Aeson
6import qualified Data.Aeson as Aeson
5import qualified Data.ByteString as BS
7import qualified Data.ByteString as BS
6import qualified Data.ByteString.Lazy as LBS
8import qualified Data.ByteString.Lazy as LBS
@@ -32,6 +34,11 @@ spec = describe "Servant.API.Stream" $ do
33 describe "NewlineFraming" $ do
35 describe "NewlineFraming" $ do
34 let tp = framingUnrender (Proxy :: Proxy NewlineFraming) (Right . LBS.toStrict)
36 let tp = framingUnrender (Proxy :: Proxy NewlineFraming) (Right . LBS.toStrict)
37+ let re = framingRender (Proxy :: Proxy NewlineFraming) id
39+ it "framingRender examples" $ do
40+ runRenderFrames re [] `shouldBe` Right ""
41+ runRenderFrames re ["foo", "bar", "baz"] `shouldBe` Right "foo\nbar\nbaz"
36 it "framingUnrender examples" $ do
43 it "framingUnrender examples" $ do
37 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
44 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
@@ -51,6 +58,11 @@ spec = describe "Servant.API.Stream" $ do
52 describe "NetstringFraming" $ do
59 describe "NetstringFraming" $ do
53 let tp = framingUnrender (Proxy :: Proxy NetstringFraming) (Right . LBS.toStrict)
60 let tp = framingUnrender (Proxy :: Proxy NetstringFraming) (Right . LBS.toStrict)
61+ let re = framingRender (Proxy :: Proxy NetstringFraming) id
63+ it "framingRender examples" $ do
64+ runRenderFrames re [] `shouldBe` Right ""
65+ runRenderFrames re ["foo", "bar", "baz"] `shouldBe` Right "3:foo,3:bar,3:baz,"
55 it "framingUnrender examples" $ do
67 it "framingUnrender examples" $ do
56 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
68 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
@@ -74,6 +86,9 @@ roundtrip
74roundtrip render unrender xs =
86roundtrip render unrender xs =
75 map Right xs === runUnrenderFrames (unrender . fmap LBS.toStrict . render) xs
87 map Right xs === runUnrenderFrames (unrender . fmap LBS.toStrict . render) xs
89+runRenderFrames :: (SourceT Identity a -> SourceT Identity LBS.ByteString) -> [a] -> Either String LBS.ByteString
90+runRenderFrames f = fmap mconcat . runExcept . runSourceT . f . source
77runUnrenderFrames :: (SourceT Identity b -> SourceT Identity a) -> [b] -> [Either String a]
92runUnrenderFrames :: (SourceT Identity b -> SourceT Identity a) -> [b] -> [Either String a]
78runUnrenderFrames f = go . Effect . flip unSourceT return . f . source where
93runUnrenderFrames f = go . Effect . flip unSourceT return . f . source where
79 go :: StepT Identity a -> [Either String a]
94 go :: StepT Identity a -> [Either String a]