1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 module Hspec.Utils.Server where
4 import Control.Concurrent (ThreadId, forkIO, killThread)
5 import Control.Monad (Monad(..))
6 import Data.Function (($))
7 import Data.Maybe (fromJust)
9 import qualified Network.HTTP.Client as Client
10 import qualified Network.Socket as Net
11 import qualified Network.Wai as Wai
12 import qualified Network.Wai.Handler.Warp as Warp
15 import Symantic.HTTP.Client
17 -- * Type 'TestServer'
18 data TestServer = TestServer
20 , socket :: Net.Socket
24 runTestServer :: Wai.Application -> IO TestServer
25 runTestServer waiApp = do
26 let baseURI = fromJust $ parseURI "http://localhost:8080"
27 (port, socket) <- openTestSocket
29 Warp.runSettingsSocket
30 (Warp.setPort port $ Warp.defaultSettings)
32 manager <- Client.newManager Client.defaultManagerSettings
34 { env = clientEnv manager baseURI
37 killTestServer :: TestServer -> IO ()
38 killTestServer TestServer{..} = do
42 openTestSocket :: IO (Warp.Port, Net.Socket)
44 sock <- Net.socket Net.AF_INET Net.Stream Net.defaultProtocol
45 Net.setSocketOption sock Net.ReuseAddr 1
46 Net.bind sock (Net.SockAddrInet port host)
50 host = Net.tupleToHostAddress (127, 0, 0, 1)