2017-10-26 21:19:30 +00:00
|
|
|
module Test.HTTPure.ResponseSpec where
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-18 05:31:46 +00:00
|
|
|
import Prelude
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2018-08-27 04:54:04 +00:00
|
|
|
import Data.Either as Either
|
|
|
|
import Effect.Aff as Aff
|
2018-07-08 23:16:48 +00:00
|
|
|
import Effect.Class as EffectClass
|
2018-08-20 02:50:07 +00:00
|
|
|
import Node.Encoding as Encoding
|
2018-08-27 04:54:04 +00:00
|
|
|
import Node.HTTP as HTTP
|
|
|
|
import Node.Stream as Stream
|
2017-07-10 10:17:13 +00:00
|
|
|
import Test.Spec as Spec
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2018-08-30 22:01:49 +00:00
|
|
|
import HTTPure.Body as Body
|
2017-09-26 06:08:07 +00:00
|
|
|
import HTTPure.Headers as Headers
|
2017-07-10 10:17:13 +00:00
|
|
|
import HTTPure.Response as Response
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
import Test.HTTPure.TestHelpers as TestHelpers
|
|
|
|
import Test.HTTPure.TestHelpers ((?=))
|
2017-07-14 06:28:57 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
sendSpec :: TestHelpers.Test
|
2017-07-14 06:28:57 +00:00
|
|
|
sendSpec = Spec.describe "send" do
|
2017-07-19 18:59:55 +00:00
|
|
|
Spec.it "writes the headers" do
|
2018-08-27 04:54:04 +00:00
|
|
|
header <- do
|
|
|
|
httpResponse <- EffectClass.liftEffect $ TestHelpers.mockResponse
|
|
|
|
Response.send httpResponse $ mockResponse unit
|
2017-10-26 21:19:30 +00:00
|
|
|
pure $ TestHelpers.getResponseHeader "Test" httpResponse
|
2017-07-19 18:59:55 +00:00
|
|
|
header ?= "test"
|
|
|
|
Spec.it "writes the status" do
|
2018-08-27 04:54:04 +00:00
|
|
|
status <- do
|
|
|
|
httpResponse <- EffectClass.liftEffect $ TestHelpers.mockResponse
|
|
|
|
Response.send httpResponse $ mockResponse unit
|
2017-10-26 21:19:30 +00:00
|
|
|
pure $ TestHelpers.getResponseStatus httpResponse
|
2017-09-26 06:08:07 +00:00
|
|
|
status ?= 123
|
2017-07-19 18:59:55 +00:00
|
|
|
Spec.it "writes the body" do
|
2018-08-27 04:54:04 +00:00
|
|
|
body <- do
|
|
|
|
httpResponse <- EffectClass.liftEffect $ TestHelpers.mockResponse
|
|
|
|
Response.send httpResponse $ mockResponse unit
|
2017-10-26 21:19:30 +00:00
|
|
|
pure $ TestHelpers.getResponseBody httpResponse
|
2017-07-19 18:59:55 +00:00
|
|
|
body ?= "test"
|
2017-09-26 06:08:07 +00:00
|
|
|
where
|
2017-09-29 14:25:31 +00:00
|
|
|
mockHeaders = Headers.header "Test" "test"
|
2018-08-27 04:54:04 +00:00
|
|
|
mockResponse _ =
|
2018-08-20 02:50:07 +00:00
|
|
|
{ status: 123
|
|
|
|
, headers: mockHeaders
|
2018-08-27 04:54:04 +00:00
|
|
|
, writeBody: \response -> Aff.makeAff \done -> do
|
|
|
|
stream <- pure $ HTTP.responseAsStream response
|
|
|
|
_ <- Stream.writeString stream Encoding.UTF8 "test" $ pure unit
|
|
|
|
_ <- Stream.end stream $ pure unit
|
|
|
|
done $ Either.Right unit
|
|
|
|
pure Aff.nonCanceler
|
2018-08-20 02:50:07 +00:00
|
|
|
}
|
2017-09-26 06:08:07 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
responseFunctionSpec :: TestHelpers.Test
|
2017-09-26 06:08:07 +00:00
|
|
|
responseFunctionSpec = Spec.describe "response" do
|
2017-09-29 14:49:46 +00:00
|
|
|
Spec.it "has the right status" do
|
|
|
|
resp <- Response.response 123 "test"
|
|
|
|
resp.status ?= 123
|
2018-08-30 22:01:49 +00:00
|
|
|
Spec.it "has only default headers" do
|
2017-09-29 14:49:46 +00:00
|
|
|
resp <- Response.response 123 "test"
|
2018-08-30 22:01:49 +00:00
|
|
|
defaultHeaders <- EffectClass.liftEffect $ Body.defaultHeaders "test"
|
|
|
|
resp.headers ?= defaultHeaders
|
2018-08-27 04:54:04 +00:00
|
|
|
Spec.it "has the right writeBody function" do
|
|
|
|
body <- do
|
|
|
|
resp <- Response.response 123 "test"
|
|
|
|
httpResponse <- EffectClass.liftEffect $ TestHelpers.mockResponse
|
|
|
|
resp.writeBody httpResponse
|
|
|
|
pure $ TestHelpers.getResponseBody httpResponse
|
|
|
|
body ?= "test"
|
2017-09-29 14:49:46 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
response'Spec :: TestHelpers.Test
|
2017-09-29 14:49:46 +00:00
|
|
|
response'Spec = Spec.describe "response'" do
|
2017-09-26 06:08:07 +00:00
|
|
|
Spec.it "has the right status" do
|
|
|
|
resp <- mockResponse
|
2017-09-27 19:34:00 +00:00
|
|
|
resp.status ?= 123
|
2017-09-26 06:08:07 +00:00
|
|
|
Spec.it "has the right headers" do
|
|
|
|
resp <- mockResponse
|
2018-08-30 22:01:49 +00:00
|
|
|
defaultHeaders <- EffectClass.liftEffect $ Body.defaultHeaders "test"
|
|
|
|
resp.headers ?= defaultHeaders <> mockHeaders
|
2018-08-27 04:54:04 +00:00
|
|
|
Spec.it "has the right writeBody function" do
|
|
|
|
body <- do
|
|
|
|
resp <- mockResponse
|
|
|
|
httpResponse <- EffectClass.liftEffect $ TestHelpers.mockResponse
|
|
|
|
resp.writeBody httpResponse
|
|
|
|
pure $ TestHelpers.getResponseBody httpResponse
|
|
|
|
body ?= "test"
|
2017-09-26 06:08:07 +00:00
|
|
|
where
|
2017-09-29 14:25:31 +00:00
|
|
|
mockHeaders = Headers.header "Test" "test"
|
2017-09-29 14:49:46 +00:00
|
|
|
mockResponse = Response.response' 123 mockHeaders "test"
|
2017-09-26 06:08:07 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
emptyResponseSpec :: TestHelpers.Test
|
2017-09-29 14:49:46 +00:00
|
|
|
emptyResponseSpec = Spec.describe "emptyResponse" do
|
|
|
|
Spec.it "has the right status" do
|
|
|
|
resp <- Response.emptyResponse 123
|
|
|
|
resp.status ?= 123
|
2018-08-30 22:01:49 +00:00
|
|
|
Spec.it "has only default headers" do
|
2017-09-29 14:49:46 +00:00
|
|
|
resp <- Response.emptyResponse 123
|
2018-08-30 22:01:49 +00:00
|
|
|
defaultHeaders <- EffectClass.liftEffect $ Body.defaultHeaders ""
|
|
|
|
resp.headers ?= defaultHeaders
|
2018-08-27 04:54:04 +00:00
|
|
|
Spec.it "has the right writeBody function" do
|
|
|
|
body <- do
|
|
|
|
resp <- Response.emptyResponse 123
|
|
|
|
httpResponse <- EffectClass.liftEffect $ TestHelpers.mockResponse
|
|
|
|
resp.writeBody httpResponse
|
|
|
|
pure $ TestHelpers.getResponseBody httpResponse
|
|
|
|
body ?= ""
|
2017-09-29 14:49:46 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
emptyResponse'Spec :: TestHelpers.Test
|
2017-09-29 14:49:46 +00:00
|
|
|
emptyResponse'Spec = Spec.describe "emptyResponse'" do
|
2017-09-26 06:08:07 +00:00
|
|
|
Spec.it "has the right status" do
|
|
|
|
resp <- mockResponse
|
2017-09-27 19:34:00 +00:00
|
|
|
resp.status ?= 123
|
2017-09-26 06:08:07 +00:00
|
|
|
Spec.it "has the right headers" do
|
|
|
|
resp <- mockResponse
|
2018-08-30 22:01:49 +00:00
|
|
|
defaultHeaders <- EffectClass.liftEffect $ Body.defaultHeaders ""
|
|
|
|
resp.headers ?= mockHeaders <> defaultHeaders
|
2018-08-27 04:54:04 +00:00
|
|
|
Spec.it "has the right writeBody function" do
|
|
|
|
body <- do
|
|
|
|
resp <- mockResponse
|
|
|
|
httpResponse <- EffectClass.liftEffect $ TestHelpers.mockResponse
|
|
|
|
resp.writeBody httpResponse
|
|
|
|
pure $ TestHelpers.getResponseBody httpResponse
|
|
|
|
body ?= ""
|
2017-09-26 06:08:07 +00:00
|
|
|
where
|
2017-09-29 14:25:31 +00:00
|
|
|
mockHeaders = Headers.header "Test" "test"
|
2017-09-29 14:49:46 +00:00
|
|
|
mockResponse = Response.emptyResponse' 123 mockHeaders
|
2017-07-10 10:17:13 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
responseSpec :: TestHelpers.Test
|
2017-07-14 06:28:57 +00:00
|
|
|
responseSpec = Spec.describe "Response" do
|
|
|
|
sendSpec
|
2017-09-26 06:08:07 +00:00
|
|
|
responseFunctionSpec
|
|
|
|
response'Spec
|
2017-09-29 14:49:46 +00:00
|
|
|
emptyResponseSpec
|
|
|
|
emptyResponse'Spec
|