This commit is contained in:
Sönke Hahn 2016-04-06 16:58:44 +08:00
parent b5dc4dd350
commit 2434399845
8 changed files with 26 additions and 20 deletions

View file

@ -80,6 +80,8 @@ test-suite spec
Servant.ClientSpec Servant.ClientSpec
, Servant.Client.PerformRequest.GHCJSSpec , Servant.Client.PerformRequest.GHCJSSpec
, Servant.Client.TestServer , Servant.Client.TestServer
, Servant.Client.TestServer.Types
, Servant.Common.BaseUrl
, Servant.Common.BaseUrlSpec , Servant.Common.BaseUrlSpec
, Spec , Spec
if impl(ghcjs) if impl(ghcjs)

View file

@ -18,7 +18,6 @@ import Data.String.Conversions
import Data.Proxy import Data.Proxy
import Data.Text (Text) import Data.Text (Text)
import Data.Text.Encoding import Data.Text.Encoding
import Data.Typeable
import Network.HTTP.Client hiding (Proxy, path) import Network.HTTP.Client hiding (Proxy, path)
import Network.HTTP.Media import Network.HTTP.Media
import Network.HTTP.Types import Network.HTTP.Types
@ -31,8 +30,6 @@ import Web.HttpApiData
import qualified Network.HTTP.Client as Client import qualified Network.HTTP.Client as Client
import Web.HttpApiData
data Req = Req data Req = Req
{ reqPath :: String { reqPath :: String
, qs :: QueryText , qs :: QueryText

View file

@ -27,7 +27,7 @@ withServer (TestServer testServerName _) action = do
} }
line <- hGetLine stdout line <- hGetLine stdout
case readMay line :: Maybe Int of case readMay line :: Maybe Int of
Nothing -> error ("unparseable port: " ++ show line) Nothing -> die ("unparseable port: " ++ show line)
Just port -> return (port, process) Just port -> return (port, process)
stop (_, process) = do stop (_, process) = do
terminateProcess process terminateProcess process

View file

@ -34,8 +34,9 @@ import GHC.Generics (Generic)
import qualified Network.HTTP.Client as C import qualified Network.HTTP.Client as C
import Network.HTTP.Media import Network.HTTP.Media
import qualified Network.HTTP.Types as HTTP import qualified Network.HTTP.Types as HTTP
import Network.Wai (Application, responseLBS) import Network.Wai (responseLBS)
import qualified Network.Wai as Wai import qualified Network.Wai as Wai
import System.Exit
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
import Test.HUnit import Test.HUnit
import Test.Hspec import Test.Hspec
@ -77,12 +78,14 @@ spec = describe "Servant.Client" $ do
-- This rather cumbersome approach is taken because it's not easy to run a wai -- This rather cumbersome approach is taken because it's not easy to run a wai
-- Application as a http server when using ghcjs. -- Application as a http server when using ghcjs.
withTestServer :: String -> (BaseUrl -> IO a) -> IO a withTestServer :: String -> (BaseUrl -> IO a) -> IO a
withTestServer = withServer . lookupTestServer withTestServer name action = do
server <- lookupTestServer name
withServer server action
lookupTestServer :: String -> TestServer lookupTestServer :: String -> IO TestServer
lookupTestServer name = case lookup name mapping of lookupTestServer name = case lookup name mapping of
Nothing -> error ("test server not found: " ++ name) Nothing -> die ("test server not found: " ++ name)
Just testServer -> testServer Just testServer -> return testServer
where where
mapping :: [(String, TestServer)] mapping :: [(String, TestServer)]
mapping = map (\ server -> (testServerName server, server)) allTestServers mapping = map (\ server -> (testServerName server, server)) allTestServers
@ -93,6 +96,8 @@ allTestServers =
server : server :
errorServer : errorServer :
failServer : failServer :
basicAuthServer :
genAuthServer :
[] []
-- * test data types -- * test data types
@ -227,8 +232,9 @@ basicAuthHandler =
basicServerContext :: Context '[ BasicAuthCheck () ] basicServerContext :: Context '[ BasicAuthCheck () ]
basicServerContext = basicAuthHandler :. EmptyContext basicServerContext = basicAuthHandler :. EmptyContext
basicAuthServer :: Application basicAuthServer :: TestServer
basicAuthServer = serveWithContext basicAuthAPI basicServerContext (const (return alice)) basicAuthServer = TestServer "basicAuthServer" $
serveWithContext basicAuthAPI basicServerContext (const (return alice))
-- * general auth stuff -- * general auth stuff
@ -251,8 +257,9 @@ genAuthHandler =
genAuthServerContext :: Context '[ AuthHandler Wai.Request () ] genAuthServerContext :: Context '[ AuthHandler Wai.Request () ]
genAuthServerContext = genAuthHandler :. EmptyContext genAuthServerContext = genAuthHandler :. EmptyContext
genAuthServer :: Application genAuthServer :: TestServer
genAuthServer = serveWithContext genAuthAPI genAuthServerContext (const (return alice)) genAuthServer = TestServer "genAuthServer" $
serveWithContext genAuthAPI genAuthServerContext (const (return alice))
{-# NOINLINE manager #-} {-# NOINLINE manager #-}
manager :: C.Manager manager :: C.Manager

View file

@ -20,6 +20,7 @@ executables:
- wai - wai
- transformers - transformers
- network - network
- bytestring
source-dirs: source-dirs:
- ./ - ./
- ../ - ../

View file

@ -9,7 +9,4 @@ packages:
- servant-server - servant-server
extra-dep: true extra-dep: true
resolver: nightly-2015-10-08 resolver: nightly-2016-04-01
extra-deps:
- http-api-data-0.1.1.1

View file

@ -1,4 +1,4 @@
-- This file has been generated from package.yaml by hpack version 0.8.0. -- This file has been generated from package.yaml by hpack version 0.10.0.
-- --
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
@ -29,6 +29,7 @@ executable testServer
, wai , wai
, transformers , transformers
, network , network
, bytestring
other-modules: other-modules:
Servant.Client.PerformRequest.GHCJSSpec Servant.Client.PerformRequest.GHCJSSpec
Servant.Client.TestServer Servant.Client.TestServer

View file

@ -17,8 +17,9 @@ main = do
setBeforeMainLoop (print port >> hFlush stdout) $ setBeforeMainLoop (print port >> hFlush stdout) $
defaultSettings defaultSettings
runSettingsSocket settings socket $ runSettingsSocket settings socket $
serve testServerApi $ \ testServerName -> serve testServerApi $ \ testServerName request respond -> do
testServerApp $ lookupTestServer testServerName app <- testServerApp <$> lookupTestServer testServerName
app request respond
type TestServerApi = type TestServerApi =
Capture "testServerName" String :> Raw Capture "testServerName" String :> Raw