1{-# LANGUAGE CPP #-}
2{-# LANGUAGE OverloadedStrings #-}
3
4module Network.Socket.ByteString.LazySpec (main, spec) where
5
6import Prelude hiding (getContents)
7
8import qualified Data.ByteString.Lazy as L
9import Network.Socket
10import Network.Socket.ByteString.Lazy
11import Network.Test.Common
12import Control.Monad
13
14import Test.Hspec
15
16main :: IO ()
17main = hspec spec
18
19spec :: Spec
20spec = do
21    describe "send" $ do
22        it "works well" $ do
23            let server sock = recv sock 1024 `shouldReturn` lazyTestMsg
24                client sock = send sock lazyTestMsg
25            tcpTest client server
26
27        it "throws when closed" $ do
28            let server _ = return ()
29                client sock = do
30                    close sock
31                    send sock lazyTestMsg `shouldThrow` anyException
32            tcpTest client server
33
34    describe "sendAll" $ do
35        it "works well" $ do
36            let server sock = recv sock 1024 `shouldReturn` lazyTestMsg
37                client sock = sendAll sock lazyTestMsg
38            tcpTest client server
39
40        it "throws when closed" $ do
41            let server _ = return ()
42                client sock = do
43                    close sock
44                    sendAll sock lazyTestMsg `shouldThrow` anyException
45            tcpTest client server
46
47    describe "getContents" $ do
48        it "works well" $ do
49            let server sock = getContents sock `shouldReturn` lazyTestMsg
50                client sock = do
51                    void $ send sock lazyTestMsg
52                    shutdown sock ShutdownSend
53            tcpTest client server
54
55        it "returns empty string at EOF" $ do
56            let client s = getContents s `shouldReturn` L.empty
57                server s = shutdown s ShutdownSend
58            tcpTest client server
59
60    describe "recv" $ do
61        it "works well" $ do
62            let server sock = recv sock 1024 `shouldReturn` lazyTestMsg
63                client sock = send sock lazyTestMsg
64            tcpTest client server
65
66        it "throws when closed" $ do
67            let server sock = do
68                    close sock
69                    recv sock 1024 `shouldThrow` anyException
70                client sock = send sock lazyTestMsg
71            tcpTest client server
72
73        it "can treat overflow" $ do
74            let server sock = do
75                    seg1 <- recv sock (L.length lazyTestMsg - 3)
76                    seg2 <- recv sock 1024
77                    let msg = L.append seg1 seg2
78                    msg `shouldBe` lazyTestMsg
79                client sock = send sock lazyTestMsg
80            tcpTest client server
81
82        it "returns empty string at EOF" $ do
83            let client s = recv s 4096 `shouldReturn` L.empty
84                server s = shutdown s ShutdownSend
85            tcpTest client server
86