2017-10-26 21:19:30 +00:00
|
|
|
module Test.HTTPure.ServerSpec where
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-18 05:31:46 +00:00
|
|
|
import Prelude
|
2022-05-04 21:02:29 +00:00
|
|
|
|
2021-11-19 06:16:35 +00:00
|
|
|
import Control.Monad.Except (throwError)
|
|
|
|
import Data.Maybe (Maybe(Nothing))
|
2018-02-09 05:46:45 +00:00
|
|
|
import Data.Options ((:=))
|
2021-11-19 06:16:35 +00:00
|
|
|
import Data.String (joinWith)
|
2022-05-04 21:02:29 +00:00
|
|
|
import Effect.Class (liftEffect)
|
|
|
|
import Effect.Exception (error)
|
2021-11-19 06:16:35 +00:00
|
|
|
import Foreign.Object (empty)
|
2022-05-04 21:02:29 +00:00
|
|
|
import HTTPure.Request (Request)
|
|
|
|
import HTTPure.Response (ResponseM, ok)
|
|
|
|
import HTTPure.Server (serve, serve', serveSecure, serveSecure')
|
2021-11-19 06:16:35 +00:00
|
|
|
import Node.Encoding (Encoding(UTF8))
|
|
|
|
import Node.FS.Sync (readTextFile)
|
2022-05-04 21:02:29 +00:00
|
|
|
import Node.HTTP.Secure (cert, certString, key, keyString)
|
|
|
|
import Test.HTTPure.TestHelpers (Test, get, get', getStatus, (?=))
|
2021-11-19 06:16:35 +00:00
|
|
|
import Test.Spec (describe, it)
|
|
|
|
import Test.Spec.Assertions (expectError)
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2021-11-19 06:16:35 +00:00
|
|
|
mockRouter :: Request -> ResponseM
|
|
|
|
mockRouter { path } = ok $ "/" <> joinWith "/" path
|
2017-07-10 10:17:13 +00:00
|
|
|
|
2021-11-19 06:16:35 +00:00
|
|
|
serveSpec :: Test
|
2021-03-22 19:02:36 +00:00
|
|
|
serveSpec =
|
2021-11-19 06:16:35 +00:00
|
|
|
describe "serve" do
|
|
|
|
it "boots a server on the given port" do
|
|
|
|
close <- liftEffect $ serve 8080 mockRouter $ pure unit
|
|
|
|
out <- get 8080 empty "/test"
|
|
|
|
liftEffect $ close $ pure unit
|
2021-03-22 19:02:36 +00:00
|
|
|
out ?= "/test"
|
2021-11-19 06:16:35 +00:00
|
|
|
it "responds with a 500 upon unhandled exceptions" do
|
|
|
|
let router _ = throwError $ error "fail!"
|
|
|
|
close <- liftEffect $ serve 8080 router $ pure unit
|
|
|
|
status <- getStatus 8080 empty "/"
|
|
|
|
liftEffect $ close $ pure unit
|
2021-03-22 19:02:36 +00:00
|
|
|
status ?= 500
|
2018-10-09 17:37:23 +00:00
|
|
|
|
2021-11-19 06:16:35 +00:00
|
|
|
serve'Spec :: Test
|
2021-03-22 19:02:36 +00:00
|
|
|
serve'Spec =
|
2021-11-19 06:16:35 +00:00
|
|
|
describe "serve'" do
|
|
|
|
it "boots a server with the given options" do
|
|
|
|
let options = { hostname: "localhost", port: 8080, backlog: Nothing }
|
2021-03-22 19:02:36 +00:00
|
|
|
close <-
|
2021-11-19 06:16:35 +00:00
|
|
|
liftEffect
|
|
|
|
$ serve' options mockRouter
|
2021-03-22 19:02:36 +00:00
|
|
|
$ pure unit
|
2021-11-19 06:16:35 +00:00
|
|
|
out <- get 8080 empty "/test"
|
|
|
|
liftEffect $ close $ pure unit
|
2021-03-22 19:02:36 +00:00
|
|
|
out ?= "/test"
|
2018-02-09 05:46:45 +00:00
|
|
|
|
2021-11-19 06:16:35 +00:00
|
|
|
serveSecureSpec :: Test
|
2021-03-22 19:02:36 +00:00
|
|
|
serveSecureSpec =
|
2021-11-19 06:16:35 +00:00
|
|
|
describe "serveSecure" do
|
|
|
|
describe "with valid key and cert files" do
|
|
|
|
it "boots a server on the given port" do
|
2021-03-22 19:02:36 +00:00
|
|
|
close <-
|
2021-11-19 06:16:35 +00:00
|
|
|
liftEffect
|
|
|
|
$ serveSecure 8080 "./test/Mocks/Certificate.cer" "./test/Mocks/Key.key" mockRouter
|
2021-03-22 19:02:36 +00:00
|
|
|
$ pure unit
|
2021-11-19 06:16:35 +00:00
|
|
|
out <- get' 8080 empty "/test"
|
|
|
|
liftEffect $ close $ pure unit
|
2021-03-22 19:02:36 +00:00
|
|
|
out ?= "/test"
|
2021-11-19 06:16:35 +00:00
|
|
|
describe "with invalid key and cert files" do
|
|
|
|
it "throws" do
|
|
|
|
expectError $ liftEffect
|
|
|
|
$ serveSecure 8080 "" "" mockRouter
|
2021-03-22 19:02:36 +00:00
|
|
|
$ pure unit
|
2017-07-23 19:17:02 +00:00
|
|
|
|
2021-11-19 06:16:35 +00:00
|
|
|
serveSecure'Spec :: Test
|
2021-03-22 19:02:36 +00:00
|
|
|
serveSecure'Spec =
|
2021-11-19 06:16:35 +00:00
|
|
|
describe "serveSecure'" do
|
|
|
|
describe "with valid key and cert files" do
|
|
|
|
it "boots a server on the given port" do
|
|
|
|
let
|
|
|
|
options = { hostname: "localhost", port: 8080, backlog: Nothing }
|
|
|
|
sslOptions = do
|
|
|
|
cert' <- readTextFile UTF8 "./test/Mocks/Certificate.cer"
|
|
|
|
key' <- readTextFile UTF8 "./test/Mocks/Key.key"
|
|
|
|
pure $ key := keyString key' <> cert := certString cert'
|
|
|
|
sslOpts <- liftEffect $ sslOptions
|
2021-03-22 19:02:36 +00:00
|
|
|
close <-
|
2021-11-19 06:16:35 +00:00
|
|
|
liftEffect
|
|
|
|
$ serveSecure' sslOpts options mockRouter
|
2021-03-22 19:02:36 +00:00
|
|
|
$ pure unit
|
2021-11-19 06:16:35 +00:00
|
|
|
out <- get' 8080 empty "/test"
|
|
|
|
liftEffect $ close $ pure unit
|
2021-03-22 19:02:36 +00:00
|
|
|
out ?= "/test"
|
2018-02-09 05:46:45 +00:00
|
|
|
|
2021-11-19 06:16:35 +00:00
|
|
|
serverSpec :: Test
|
2021-03-22 19:02:36 +00:00
|
|
|
serverSpec =
|
2021-11-19 06:16:35 +00:00
|
|
|
describe "Server" do
|
2021-03-22 19:02:36 +00:00
|
|
|
serveSpec
|
|
|
|
serve'Spec
|
|
|
|
serveSecureSpec
|
|
|
|
serveSecure'Spec
|