@@ -142,33 +142,31 @@ of it and see how it responds.
142
142
Let's write some tests:
143
143
144
144
`` `haskell
145
- withUserApp :: IO () -> IO ()
145
+ withUserApp :: (Warp.Port -> IO () ) -> IO ()
146
146
withUserApp action =
147
- -- we can spin up a server in another thread and kill that thread when done
148
- -- in an exception-safe way
149
- bracket (liftIO $ C.forkIO $ Warp.run 8888 userApp)
150
- C.killThread
151
- (const action)
147
+ -- testWithApplication makes sure the action is executed after the server has
148
+ -- started and is being properly shutdown.
149
+ Warp.testWithApplication (pure userApp) action
152
150
153
151
154
152
businessLogicSpec :: Spec
155
153
businessLogicSpec =
156
154
-- `around` will start our Server before the tests and turn it off after
157
- around_ withUserApp $ do
155
+ around withUserApp $ do
158
156
-- create a test client function
159
157
let createUser = client (Proxy :: Proxy UserApi)
160
158
-- create a servant-client ClientEnv
161
- baseUrl <- runIO $ parseBaseUrl " http://localhost:8888 "
159
+ baseUrl <- runIO $ parseBaseUrl " http://localhost"
162
160
manager <- runIO $ newManager defaultManagerSettings
163
- let clientEnv = mkClientEnv manager baseUrl
161
+ let clientEnv port = mkClientEnv manager ( baseUrl { baseUrlPort = port })
164
162
165
163
-- testing scenarios start here
166
164
describe "POST /user" $ do
167
- it " should create a user with a high enough ID" $ do
168
- result <- runClientM (createUser 50001 ) clientEnv
165
+ it " should create a user with a high enough ID" $ \port -> do
166
+ result <- runClientM (createUser 50001 ) ( clientEnv port)
169
167
result `shouldBe` (Right $ User { name = "some user" , user_id = 50001})
170
- it " will it fail with a too-small ID?" $ do
171
- result <- runClientM (createUser 4999 ) clientEnv
168
+ it " will it fail with a too-small ID?" $ \port -> do
169
+ result <- runClientM (createUser 4999 ) ( clientEnv port)
172
170
result `shouldBe` (Right $ User { name = "some user" , user_id = 50001})
173
171
`` `
174
172
0 commit comments