2015-11-25 10:31:58 +01:00
|
|
|
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
|
|
|
|
|
|
|
|
module Servant.Client.TestServer.GHC where
|
|
|
|
|
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Exception
|
|
|
|
import Network.Socket
|
|
|
|
import Network.Wai
|
|
|
|
import Network.Wai.Handler.Warp
|
|
|
|
|
|
|
|
import Servant.Common.BaseUrl
|
2016-01-04 17:59:28 +01:00
|
|
|
import Servant.Client.TestServer.Types
|
2015-11-25 10:31:58 +01:00
|
|
|
|
|
|
|
buildTestServer :: IO ()
|
|
|
|
buildTestServer = return ()
|
|
|
|
|
2016-01-04 19:04:10 +01:00
|
|
|
withServer :: TestServer -> (BaseUrl -> IO a) -> IO a
|
|
|
|
withServer (TestServer _ app) action =
|
2015-11-25 10:31:58 +01:00
|
|
|
bracket (startWaiApp app) endWaiApp $ \ (_, url) ->
|
|
|
|
action url
|
|
|
|
|
|
|
|
startWaiApp :: Application -> IO (ThreadId, BaseUrl)
|
|
|
|
startWaiApp app = do
|
|
|
|
(port, socket) <- openTestSocket
|
|
|
|
let settings = setPort port $ defaultSettings
|
|
|
|
thread <- forkIO $ runSettingsSocket settings socket app
|
|
|
|
return (thread, BaseUrl Http "localhost" port "")
|
|
|
|
|
|
|
|
endWaiApp :: (ThreadId, BaseUrl) -> IO ()
|
|
|
|
endWaiApp (thread, _) = killThread thread
|
|
|
|
|
|
|
|
openTestSocket :: IO (Port, Socket)
|
|
|
|
openTestSocket = do
|
|
|
|
s <- socket AF_INET Stream defaultProtocol
|
|
|
|
localhost <- inet_addr "127.0.0.1"
|
|
|
|
bind s (SockAddrInet aNY_PORT localhost)
|
|
|
|
listen s 1
|
|
|
|
port <- socketPort s
|
|
|
|
return (fromIntegral port, s)
|