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