@@ -1,6 +1,8 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Servant.API.StreamSpec where
33
4+import Control.Monad.Except
5+ (runExcept)
46import qualified Data.Aeson as Aeson
57import qualified Data.ByteString as BS
68import qualified Data.ByteString.Lazy as LBS
@@ -32,6 +34,11 @@ spec = describe "Servant.API.Stream" $ do
3234
3335 describe "NewlineFraming" $ do
3436 let tp = framingUnrender (Proxy :: Proxy NewlineFraming) (Right . LBS.toStrict)
37+ let re = framingRender (Proxy :: Proxy NewlineFraming) id
38+
39+ it "framingRender examples" $ do
40+ runRenderFrames re [] `shouldBe` Right ""
41+ runRenderFrames re ["foo", "bar", "baz"] `shouldBe` Right "foo\nbar\nbaz"
3542
3643 it "framingUnrender examples" $ do
3744 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
@@ -51,6 +58,11 @@ spec = describe "Servant.API.Stream" $ do
5158
5259 describe "NetstringFraming" $ do
5360 let tp = framingUnrender (Proxy :: Proxy NetstringFraming) (Right . LBS.toStrict)
61+ let re = framingRender (Proxy :: Proxy NetstringFraming) id
62+
63+ it "framingRender examples" $ do
64+ runRenderFrames re [] `shouldBe` Right ""
65+ runRenderFrames re ["foo", "bar", "baz"] `shouldBe` Right "3:foo,3:bar,3:baz,"
5466
5567 it "framingUnrender examples" $ do
5668 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
@@ -74,6 +86,9 @@ roundtrip
7486roundtrip render unrender xs =
7587 map Right xs === runUnrenderFrames (unrender . fmap LBS.toStrict . render) xs
7688
89+runRenderFrames :: (SourceT Identity a -> SourceT Identity LBS.ByteString) -> [a] -> Either String LBS.ByteString
90+runRenderFrames f = fmap mconcat . runExcept . runSourceT . f . source
91+
7792runUnrenderFrames :: (SourceT Identity b -> SourceT Identity a) -> [b] -> [Either String a]
7893runUnrenderFrames f = go . Effect . flip unSourceT return . f . source where
7994 go :: StepT Identity a -> [Either String a]