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

81 lines
2.8 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
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
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
EffectClass.liftEffect $ Server.serve 7901 mockRouter $ pure unit
out <- TestHelpers.get 7901 Object.empty "/test"
out ?= "/test"
2017-07-10 10:17:13 +00:00
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
EffectClass.liftEffect $ Server.serve' options mockRouter $ pure unit
out <- TestHelpers.get 7902 Object.empty "/test"
2018-02-09 05:46:45 +00:00
out ?= "/test"
where
options = { hostname: "localhost", port: 7902, backlog: Maybe.Nothing }
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
EffectClass.liftEffect $ Server.serveSecure 7903 cert key mockRouter $ pure unit
out <- TestHelpers.get' 7903 Object.empty "/test"
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 do
EffectClass.liftEffect $ Server.serveSecure 7904 "" "" 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
EffectClass.liftEffect $
2018-02-09 05:46:45 +00:00
Server.serveSecure' sslOpts (options 7905) mockRouter $ pure unit
out <- TestHelpers.get' 7905 Object.empty "/test"
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