@@ -13,7 +13,6 @@ import Data.Foldable (forM_)
1313import Data.Proxy
1414import Data.Typeable
1515import Network.Socket
16−import Network.URI
1716import Network.Wai
1817import Network.Wai.Handler.Warp
1918import Test.Hspec
@@ -56,17 +55,17 @@ server = serve api (
5655 :<|> \ a b c d -> return (a, b, c, d)
5756 )
5857
59−withServer :: (URIAuth -> IO a) -> IO a
60−withServer action = withWaiDaemon (return server) (action . mkHost "localhost")
58+withServer :: (BaseUrl -> IO a) -> IO a
59+withServer action = withWaiDaemon (return server) action
6160
62−getGet :: URIAuth -> EitherT String IO Person
63−getCapture :: String -> URIAuth -> EitherT String IO Person
64−getBody :: Person -> URIAuth -> EitherT String IO Person
65−getQueryParam :: Maybe String -> URIAuth -> EitherT String IO Person
66−getQueryParams :: [String] -> URIAuth -> EitherT String IO [Person]
67−getQueryFlag :: Bool -> URIAuth -> EitherT String IO Bool
61+getGet :: BaseUrl -> EitherT String IO Person
62+getCapture :: String -> BaseUrl -> EitherT String IO Person
63+getBody :: Person -> BaseUrl -> EitherT String IO Person
64+getQueryParam :: Maybe String -> BaseUrl -> EitherT String IO Person
65+getQueryParams :: [String] -> BaseUrl -> EitherT String IO [Person]
66+getQueryFlag :: Bool -> BaseUrl -> EitherT String IO Bool
6867getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
69− -> URIAuth
68+ -> BaseUrl
7069 -> EitherT String IO (String, Maybe Int, Bool, [(String, [Rational])])
7170( getGet
7271 :<|> getCapture
@@ -119,8 +118,8 @@ spec = do
119118 test (WrappedApi api) =
120119 it (show (typeOf api)) $
121120 withWaiDaemon (return (serve api (left (500, "error message")))) $
122− \ (mkHost "localhost" -> host) -> do
123− let getResponse :: URIAuth -> EitherT String IO ()
121+ \ host -> do
122+ let getResponse :: BaseUrl -> EitherT String IO ()
124123 getResponse = client api
125124 Left result <- runEitherT (getResponse host)
126125 result `shouldContain` "error message"
@@ -133,17 +132,17 @@ spec = do
133132
134133data WrappedApi where
135134 WrappedApi :: (HasServer api, Server api ~ EitherT (Int, String) IO a,
136− HasClient api, Client api ~ (URIAuth -> EitherT String IO ()),
135+ HasClient api, Client api ~ (BaseUrl -> EitherT String IO ()),
137136 Typeable api) =>
138137 Proxy api -> WrappedApi
139138
140139
141140-- * utils
142141
143−withWaiDaemon :: IO Application -> (Port -> IO a) -> IO a
142+withWaiDaemon :: IO Application -> (BaseUrl -> IO a) -> IO a
144143withWaiDaemon mkApplication action = do
145144 application <- mkApplication
146− bracket (acquire application) free (\ (_, _, port) -> action port)
145+ bracket (acquire application) free (\ (_, _, baseUrl) -> action baseUrl)
147146 where
148147 acquire application = do
149148 (notifyStart, waitForStart) <- lvar
@@ -158,7 +157,8 @@ withWaiDaemon mkApplication action = do
158157 runSettingsSocket settings socket application)
159158 `finally` notifyKilled ()
160159 krakenPort <- waitForStart
161− return (thread, waitForKilled, krakenPort)
160+ let baseUrl = (httpBaseUrl "localhost"){baseUrlPort = krakenPort}
161+ return (thread, waitForKilled, baseUrl)
162162 free (thread, waitForKilled, _) = do
163163 killThread thread
164164 waitForKilled