purescript-httpurple/test/Test/HTTPure/ServerSpec.purs

98 lines
3.5 KiB
Haskell
Raw Normal View History

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