@@ -13,7 +13,6 @@ import Data.Foldable (forM_)
13import Data.Proxy
13import Data.Proxy
14import Data.Typeable
14import Data.Typeable
15import Network.Socket
15import Network.Socket
17import Network.Wai
16import Network.Wai
18import Network.Wai.Handler.Warp
17import Network.Wai.Handler.Warp
19import Test.Hspec
18import Test.Hspec
@@ -56,17 +55,17 @@ server = serve api (
56 :<|> \ a b c d -> return (a, b, c, d)
55 :<|> \ a b c d -> return (a, b, c, d)
59−withServer :: (URIAuth -> IO a) -> IO a
58+withServer :: (BaseUrl -> IO a) -> IO a
60−withServer action = withWaiDaemon (return server) (action . mkHost "localhost")
59+withServer action = withWaiDaemon (return server) action
62−getGet :: URIAuth -> EitherT String IO Person
61+getGet :: BaseUrl -> EitherT String IO Person
63−getCapture :: String -> URIAuth -> EitherT String IO Person
62+getCapture :: String -> BaseUrl -> EitherT String IO Person
64−getBody :: Person -> URIAuth -> EitherT String IO Person
63+getBody :: Person -> BaseUrl -> EitherT String IO Person
65−getQueryParam :: Maybe String -> URIAuth -> EitherT String IO Person
64+getQueryParam :: Maybe String -> BaseUrl -> EitherT String IO Person
66−getQueryParams :: [String] -> URIAuth -> EitherT String IO [Person]
65+getQueryParams :: [String] -> BaseUrl -> EitherT String IO [Person]
67−getQueryFlag :: Bool -> URIAuth -> EitherT String IO Bool
66+getQueryFlag :: Bool -> BaseUrl -> EitherT String IO Bool
68getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
67getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
69− -> URIAuth
68+ -> BaseUrl
70 -> EitherT String IO (String, Maybe Int, Bool, [(String, [Rational])])
69 -> EitherT String IO (String, Maybe Int, Bool, [(String, [Rational])])
72 :<|> getCapture
71 :<|> getCapture
@@ -119,8 +118,8 @@ spec = do
119 test (WrappedApi api) =
118 test (WrappedApi api) =
120 it (show (typeOf api)) $
119 it (show (typeOf api)) $
121 withWaiDaemon (return (serve api (left (500, "error message")))) $
120 withWaiDaemon (return (serve api (left (500, "error message")))) $
122− \ (mkHost "localhost" -> host) -> do
121+ \ host -> do
123− let getResponse :: URIAuth -> EitherT String IO ()
122+ let getResponse :: BaseUrl -> EitherT String IO ()
124 getResponse = client api
123 getResponse = client api
125 Left result <- runEitherT (getResponse host)
124 Left result <- runEitherT (getResponse host)
126 result `shouldContain` "error message"
125 result `shouldContain` "error message"
@@ -133,17 +132,17 @@ spec = do
134data WrappedApi where
133data WrappedApi where
135 WrappedApi :: (HasServer api, Server api ~ EitherT (Int, String) IO a,
134 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 ()),
137 Typeable api) =>
136 Typeable api) =>
138 Proxy api -> WrappedApi
137 Proxy api -> WrappedApi
141-- * utils
140-- * utils
143−withWaiDaemon :: IO Application -> (Port -> IO a) -> IO a
142+withWaiDaemon :: IO Application -> (BaseUrl -> IO a) -> IO a
144withWaiDaemon mkApplication action = do
143withWaiDaemon mkApplication action = do
145 application <- mkApplication
144 application <- mkApplication
146− bracket (acquire application) free (\ (_, _, port) -> action port)
145+ bracket (acquire application) free (\ (_, _, baseUrl) -> action baseUrl)
148 acquire application = do
147 acquire application = do
149 (notifyStart, waitForStart) <- lvar
148 (notifyStart, waitForStart) <- lvar
@@ -158,7 +157,8 @@ withWaiDaemon mkApplication action = do
158 runSettingsSocket settings socket application)
157 runSettingsSocket settings socket application)
159 `finally` notifyKilled ()
158 `finally` notifyKilled ()
160 krakenPort <- waitForStart
159 krakenPort <- waitForStart
160+ let baseUrl = (httpBaseUrl "localhost"){baseUrlPort = krakenPort}
161− return (thread, waitForKilled, krakenPort)
161+ return (thread, waitForKilled, baseUrl)
162 free (thread, waitForKilled, _) = do
162 free (thread, waitForKilled, _) = do
163 killThread thread
163 killThread thread
164 waitForKilled
164 waitForKilled