Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Network/WebSockets/Connection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Network.WebSockets.Connection
, RejectRequest(..)
, defaultRejectRequest
, rejectRequestWith
, rejectRequestAndCloseWith

, Connection (..)

Expand Down Expand Up @@ -245,6 +246,16 @@ rejectRequest
rejectRequest pc body = rejectRequestWith pc
defaultRejectRequest {rejectBody = body}

--------------------------------------------------------------------------------

-- | Send a rejection message to the client and close the underlying connection
rejectRequestAndCloseWith
:: PendingConnection -- ^ Connection to reject and close
-> RejectRequest -- ^ Params on how to reject the request
-> IO ()
rejectRequestAndCloseWith pc reject = do
rejectRequestWith pc reject
Stream.close $ pendingStream pc

--------------------------------------------------------------------------------
data Connection = Connection
Expand Down
32 changes: 30 additions & 2 deletions tests/haskell/Network/WebSockets/Handshake/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
--------------------------------------------------------------------------------
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
module Network.WebSockets.Handshake.Tests
( tests
Expand All @@ -7,21 +8,22 @@ module Network.WebSockets.Handshake.Tests

--------------------------------------------------------------------------------
import Control.Concurrent (forkIO)
import Control.Exception (handle)
import Control.Exception (catch, handle, throwIO)
import Data.ByteString.Char8 ()
import Data.IORef (newIORef, readIORef,
writeIORef)
import Data.Maybe (fromJust)
import Test.Framework (Test, testGroup)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (Assertion, assert, (@?=))
import Test.HUnit (Assertion, assert, assertFailure, (@?=))


--------------------------------------------------------------------------------
import Network.WebSockets
import Network.WebSockets.Connection
import Network.WebSockets.Http
import qualified Network.WebSockets.Stream as Stream
import Network.WebSockets.Types (ConnectionException(ConnectionClosed))


--------------------------------------------------------------------------------
Expand All @@ -33,6 +35,7 @@ tests = testGroup "Network.WebSockets.Handshake.Test"
, testCase "handshake Hybi13 with subprotocols and headers" testHandshakeHybi13WithProtoAndHeaders
, testCase "handshake reject" testHandshakeReject
, testCase "handshake reject with custom code" testHandshakeRejectWithCode
, testCase "handshake reject and close connection" testHandshakeRejectAndClose
, testCase "handshake Hybi9000" testHandshakeHybi9000
]

Expand Down Expand Up @@ -157,6 +160,31 @@ testHandshakeRejectWithCode = do

code @?= 401

--------------------------------------------------------------------------------
testHandshakeRejectAndClose :: Assertion
testHandshakeRejectAndClose = do
ResponseHead code _ _ <- test' rq13 $ \pc -> do
rejectRequestAndCloseWith pc defaultRejectRequest
(do
Stream.write (pendingStream pc) "Stream should be closed"
assertFailure "Stream should be closed"
) `catch` (\(e :: ConnectionException) ->
case e of
ConnectionClosed -> pure ()
_ -> throwIO e
)

code @?= 400
where
test' rq app = do
echo <- Stream.makeEchoStream
_ <- forkIO $ do
_ <- app (PendingConnection defaultConnectionOptions rq (const $ return ()) echo)
return ()
mbRh <- Stream.parse echo decodeResponseHead
case mbRh of
Nothing -> fail "testHandshake: No response"
Just rh -> return rh

--------------------------------------------------------------------------------
-- I don't believe this one is supported yet
Expand Down