2017-11-04 05:10:29 +01:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE FunctionalDependencies #-}
|
|
|
|
{-# LANGUAGE GADTs #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
{-# OPTIONS_GHC -freduction-depth=100 #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
|
|
|
|
|
|
|
|
module Servant.StreamSpec (spec) where
|
|
|
|
|
2018-06-26 19:11:28 +02:00
|
|
|
import Control.Monad.Trans.Except
|
2018-11-01 18:42:30 +01:00
|
|
|
import qualified Data.ByteString as BS
|
2017-11-04 05:10:29 +01:00
|
|
|
import Data.Proxy
|
2018-11-01 18:42:30 +01:00
|
|
|
import qualified Network.HTTP.Client as C
|
2018-06-29 21:08:26 +02:00
|
|
|
import Prelude ()
|
2018-02-27 15:31:41 +01:00
|
|
|
import Prelude.Compat
|
2018-06-29 21:08:26 +02:00
|
|
|
import Servant.API
|
2019-01-31 10:18:22 +01:00
|
|
|
((:<|>) ((:<|>)), (:>), JSON, NetstringFraming, StreamBody,
|
|
|
|
NewlineFraming, NoFraming, OctetStream, SourceIO, StreamGet,
|
|
|
|
)
|
2018-11-01 18:42:30 +01:00
|
|
|
import Servant.Client.Streaming
|
2017-11-04 05:10:29 +01:00
|
|
|
import Servant.Server
|
2018-11-01 18:42:30 +01:00
|
|
|
import Servant.Test.ComprehensiveAPI
|
2018-06-26 19:11:28 +02:00
|
|
|
import Servant.Types.SourceT
|
|
|
|
import System.Entropy
|
|
|
|
(getEntropy, getHardwareEntropy)
|
|
|
|
import System.IO.Unsafe
|
|
|
|
(unsafePerformIO)
|
|
|
|
import Test.Hspec
|
2019-03-16 08:35:32 +01:00
|
|
|
import Servant.ClientTestUtils (Person(..))
|
|
|
|
import qualified Servant.ClientTestUtils as CT
|
2017-11-04 05:10:29 +01:00
|
|
|
|
2018-11-01 18:42:30 +01:00
|
|
|
-- This declaration simply checks that all instances are in place.
|
|
|
|
-- Note: this is streaming client
|
|
|
|
_ = client comprehensiveAPI
|
|
|
|
|
2017-11-04 05:10:29 +01:00
|
|
|
spec :: Spec
|
2018-11-01 18:42:30 +01:00
|
|
|
spec = describe "Servant.Client.Streaming" $ do
|
2017-11-04 05:10:29 +01:00
|
|
|
streamSpec
|
|
|
|
|
2018-06-26 19:11:28 +02:00
|
|
|
type StreamApi =
|
|
|
|
"streamGetNewline" :> StreamGet NewlineFraming JSON (SourceIO Person)
|
|
|
|
:<|> "streamGetNetstring" :> StreamGet NetstringFraming JSON (SourceIO Person)
|
|
|
|
:<|> "streamALot" :> StreamGet NoFraming OctetStream (SourceIO BS.ByteString)
|
2019-01-31 10:18:22 +01:00
|
|
|
:<|> "streamBody" :> StreamBody NoFraming OctetStream (SourceIO BS.ByteString) :> StreamGet NoFraming OctetStream (SourceIO BS.ByteString)
|
2017-11-04 05:10:29 +01:00
|
|
|
|
2018-06-26 19:11:28 +02:00
|
|
|
api :: Proxy StreamApi
|
|
|
|
api = Proxy
|
2017-11-04 05:10:29 +01:00
|
|
|
|
2018-11-01 18:42:30 +01:00
|
|
|
getGetNL, getGetNS :: ClientM (SourceIO Person)
|
2022-03-17 14:12:57 +01:00
|
|
|
_getGetALot :: ClientM (SourceIO BS.ByteString)
|
2019-01-31 10:18:22 +01:00
|
|
|
getStreamBody :: SourceT IO BS.ByteString -> ClientM (SourceIO BS.ByteString)
|
2022-03-17 14:12:57 +01:00
|
|
|
getGetNL :<|> getGetNS :<|> _getGetALot :<|> getStreamBody = client api
|
2017-11-04 05:10:29 +01:00
|
|
|
|
|
|
|
alice :: Person
|
|
|
|
alice = Person "Alice" 42
|
|
|
|
|
|
|
|
bob :: Person
|
|
|
|
bob = Person "Bob" 25
|
|
|
|
|
|
|
|
server :: Application
|
2018-06-26 19:11:28 +02:00
|
|
|
server = serve api
|
|
|
|
$ return (source [alice, bob, alice])
|
|
|
|
:<|> return (source [alice, bob, alice])
|
|
|
|
-- 2 ^ (18 + 10) = 256M
|
|
|
|
:<|> return (SourceT ($ lots (powerOfTwo 18)))
|
2019-01-31 10:18:22 +01:00
|
|
|
:<|> return
|
2018-06-26 19:11:28 +02:00
|
|
|
where
|
|
|
|
lots n
|
|
|
|
| n < 0 = Stop
|
|
|
|
| otherwise = Effect $ do
|
|
|
|
let size = powerOfTwo 10
|
|
|
|
mbs <- getHardwareEntropy size
|
|
|
|
bs <- maybe (getEntropy size) pure mbs
|
|
|
|
return (Yield bs (lots (n - 1)))
|
2017-11-04 05:10:29 +01:00
|
|
|
|
2018-06-26 19:11:28 +02:00
|
|
|
powerOfTwo :: Int -> Int
|
|
|
|
powerOfTwo = (2 ^)
|
2017-11-04 05:10:29 +01:00
|
|
|
|
|
|
|
{-# NOINLINE manager' #-}
|
|
|
|
manager' :: C.Manager
|
|
|
|
manager' = unsafePerformIO $ C.newManager C.defaultManagerSettings
|
|
|
|
|
2019-02-18 19:17:46 +01:00
|
|
|
withClient :: ClientM a -> BaseUrl -> (Either ClientError a -> IO r) -> IO r
|
2018-11-01 18:42:30 +01:00
|
|
|
withClient x baseUrl' = withClientM x (mkClientEnv manager' baseUrl')
|
2017-11-04 05:10:29 +01:00
|
|
|
|
2018-11-01 18:42:30 +01:00
|
|
|
testRunSourceIO :: SourceIO a
|
2018-06-26 19:11:28 +02:00
|
|
|
-> IO (Either String [a])
|
2018-11-01 18:42:30 +01:00
|
|
|
testRunSourceIO = runExceptT . runSourceT
|
2017-11-04 05:10:29 +01:00
|
|
|
|
|
|
|
streamSpec :: Spec
|
2019-03-16 08:35:32 +01:00
|
|
|
streamSpec = beforeAll (CT.startWaiApp server) $ afterAll CT.endWaiApp $ do
|
2018-02-27 15:31:41 +01:00
|
|
|
it "works with Servant.API.StreamGet.Newline" $ \(_, baseUrl) -> do
|
2018-11-01 18:42:30 +01:00
|
|
|
withClient getGetNL baseUrl $ \(Right res) ->
|
|
|
|
testRunSourceIO res `shouldReturn` Right [alice, bob, alice]
|
2017-11-04 05:10:29 +01:00
|
|
|
|
2018-02-27 15:31:41 +01:00
|
|
|
it "works with Servant.API.StreamGet.Netstring" $ \(_, baseUrl) -> do
|
2018-11-01 18:42:30 +01:00
|
|
|
withClient getGetNS baseUrl $ \(Right res) ->
|
|
|
|
testRunSourceIO res `shouldReturn` Right [alice, bob, alice]
|
2018-02-27 15:31:41 +01:00
|
|
|
|
2019-01-31 10:18:22 +01:00
|
|
|
it "works with Servant.API.StreamBody" $ \(_, baseUrl) -> do
|
2019-02-02 11:44:35 +01:00
|
|
|
withClient (getStreamBody (source input)) baseUrl $ \(Right res) ->
|
|
|
|
testRunSourceIO res `shouldReturn` Right output
|
2019-01-31 10:18:22 +01:00
|
|
|
where
|
2019-02-02 11:44:35 +01:00
|
|
|
input = ["foo", "", "bar"]
|
|
|
|
output = ["foo", "bar"]
|