Skip to content

Commit

Permalink
Add 'testWithApplication'' function
Browse files Browse the repository at this point in the history
Same as `testWithApplication` but accepts `Application` as a pure argument
The requirement of `testWithApplication` to pass `Application` parameter in wrapped in `IO` monad seems unnecessary strict:
Apparently the only action `testWithApplicationSettings` (the underlying implementation) does is to unwrap it with:
```haskell
testWithApplicationSettings :: Settings -> IO Application -> (Port -> IO a) -> IO a
testWithApplicationSettings settings mkApp action = do
  callingThread <- myThreadId
  app <- mkApp
  let wrappedApp request respond =
        app request respond `catch` \ e -> do
  [truncated]
```

into `app` which can be done outside of  `testWithApplication'` with:

```haskell
testWithApplication :: IO Application -> (Port -> IO a) -> IO a
testWithApplication mkApp action =
  mkApp >>= flip testWithApplication' action
```
  • Loading branch information
EduardSergeev committed May 13, 2021
1 parent def53be commit 2322225
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions warp/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.3.16

* Add `testWithApplication'` function
Same as `testWithApplication` but accepts pure `Application` instead of `IO Application`

## 3.3.15

* Using http2 v3.
Expand Down
1 change: 1 addition & 0 deletions warp/Network/Wai/Handler/Warp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ module Network.Wai.Handler.Warp (
, withApplication
, withApplicationSettings
, testWithApplication
, testWithApplication'
, testWithApplicationSettings
, openFreePort
-- * Version
Expand Down
15 changes: 15 additions & 0 deletions warp/Network/Wai/Handler/Warp/WithApplication.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Network.Wai.Handler.Warp.WithApplication (
withApplication,
withApplicationSettings,
testWithApplication,
testWithApplication',
testWithApplicationSettings,
openFreePort,
withFreePort,
Expand Down Expand Up @@ -48,6 +49,20 @@ withApplicationSettings settings' mkApp action = do
Left () -> throwIO $ ErrorCall "Unexpected: runSettingsSocket exited"
Right x -> return x

-- | Same as 'testWithApplication'
-- but accepts @app :: 'Application'@ as a first argument instead of @mkApp :: 'IO' 'Application'@.
--
-- Except for the purity of its first argument, the behaviour of this function is identical to 'testWithApplication'.
--
-- 'testWithApplication' can be expressed via 'testWithApplication'' as:
--
-- >>> testWithApplication mkApp action = mkApp >>= flip testWithApplication' action
--
-- @since 3.3.16
testWithApplication' :: Application -> (Port -> IO a) -> IO a
testWithApplication' =
testWithApplication . return

-- | Same as 'withApplication' but with different exception handling: If the
-- given 'Application' throws an exception, 'testWithApplication' will re-throw
-- the exception to the calling thread, possibly interrupting the execution of
Expand Down
7 changes: 7 additions & 0 deletions warp/test/WithApplicationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ spec = do
readProcess "curl" ["-s", "localhost:" ++ show port] "")
`shouldThrow` (errorCall "foo")

describe "testWithApplication'" $ do
it "propagates exceptions from the server to the executing thread" $ do
let mkApp _request _respond = throwIO $ ErrorCall "foo"
(testWithApplication' mkApp $ \ port -> do
readProcess "curl" ["-s", "localhost:" ++ show port] "")
`shouldThrow` (errorCall "foo")

{- The future netwrok library will not export MkSocket.
describe "withFreePort" $ do
it "closes the socket before exiting" $ do
Expand Down
2 changes: 1 addition & 1 deletion warp/warp.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: warp
Version: 3.3.15
Version: 3.3.16
Synopsis: A fast, light-weight web server for WAI applications.
License: MIT
License-file: LICENSE
Expand Down

0 comments on commit 2322225

Please sign in to comment.