2017-10-26 21:19:30 +00:00
|
|
|
module Test.HTTPure.BodySpec where
|
2017-07-14 06:28:57 +00:00
|
|
|
|
2017-07-18 05:31:46 +00:00
|
|
|
import Prelude
|
2017-07-14 06:28:57 +00:00
|
|
|
|
2018-07-08 23:16:48 +00:00
|
|
|
import Effect.Class as EffectClass
|
2018-08-25 04:49:28 +00:00
|
|
|
import Node.Buffer as Buffer
|
|
|
|
import Node.Encoding as Encoding
|
2017-07-14 06:28:57 +00:00
|
|
|
import Test.Spec as Spec
|
2017-07-17 23:42:13 +00:00
|
|
|
|
|
|
|
import HTTPure.Body as Body
|
2018-08-30 22:01:49 +00:00
|
|
|
import HTTPure.Headers as Headers
|
2017-07-14 06:28:57 +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
|
|
|
readSpec :: TestHelpers.Test
|
2017-07-18 05:25:14 +00:00
|
|
|
readSpec = Spec.describe "read" do
|
2017-09-26 06:08:07 +00:00
|
|
|
Spec.it "is the body of the Request" do
|
2017-10-26 21:19:30 +00:00
|
|
|
request <- TestHelpers.mockRequest "GET" "" "test" []
|
2017-07-18 05:25:14 +00:00
|
|
|
body <- Body.read request
|
2017-07-19 05:36:56 +00:00
|
|
|
body ?= "test"
|
2017-07-18 05:25:14 +00:00
|
|
|
|
2018-08-30 22:01:49 +00:00
|
|
|
defaultHeadersSpec :: TestHelpers.Test
|
|
|
|
defaultHeadersSpec = Spec.describe "defaultHeaders" do
|
2018-08-27 04:54:04 +00:00
|
|
|
Spec.describe "String" do
|
2018-08-30 22:01:49 +00:00
|
|
|
Spec.describe "with an ASCII string" do
|
|
|
|
Spec.it "has the correct Content-Length header" do
|
|
|
|
headers <- EffectClass.liftEffect $ Body.defaultHeaders "ascii"
|
|
|
|
headers ?= Headers.header "Content-Length" "5"
|
|
|
|
Spec.describe "with a UTF-8 string" do
|
|
|
|
Spec.it "has the correct Content-Length header" do
|
|
|
|
headers <- EffectClass.liftEffect $ Body.defaultHeaders "\x2603"
|
|
|
|
headers ?= Headers.header "Content-Length" "3"
|
2018-08-27 04:54:04 +00:00
|
|
|
Spec.describe "Buffer" do
|
2018-08-30 22:01:49 +00:00
|
|
|
Spec.it "has the correct Content-Length header" do
|
|
|
|
buf <- EffectClass.liftEffect $ Buffer.fromString "foobar" Encoding.UTF8
|
|
|
|
headers <- EffectClass.liftEffect $ Body.defaultHeaders buf
|
|
|
|
headers ?= Headers.header "Content-Length" "6"
|
|
|
|
Spec.describe "Readable" do
|
|
|
|
Spec.it "specifies the Transfer-Encoding header" do
|
|
|
|
let body = TestHelpers.stringToStream "test"
|
|
|
|
headers <- EffectClass.liftEffect $ Body.defaultHeaders body
|
|
|
|
headers ?= Headers.header "Transfer-Encoding" "chunked"
|
2018-08-25 04:49:28 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
writeSpec :: TestHelpers.Test
|
2017-07-17 23:42:13 +00:00
|
|
|
writeSpec = Spec.describe "write" do
|
2018-08-27 04:54:04 +00:00
|
|
|
Spec.describe "String" do
|
|
|
|
Spec.it "writes the String to the Response body" do
|
|
|
|
body <- do
|
|
|
|
resp <- EffectClass.liftEffect TestHelpers.mockResponse
|
|
|
|
Body.write "test" resp
|
|
|
|
pure $ TestHelpers.getResponseBody resp
|
|
|
|
body ?= "test"
|
|
|
|
Spec.describe "Buffer" do
|
|
|
|
Spec.it "writes the Buffer to the Response body" do
|
|
|
|
body <- do
|
|
|
|
resp <- EffectClass.liftEffect TestHelpers.mockResponse
|
|
|
|
buf <- EffectClass.liftEffect $ Buffer.fromString "test" Encoding.UTF8
|
|
|
|
Body.write buf resp
|
|
|
|
pure $ TestHelpers.getResponseBody resp
|
|
|
|
body ?= "test"
|
2018-08-30 22:01:49 +00:00
|
|
|
Spec.describe "Readable" do
|
|
|
|
Spec.it "pipes the input stream to the Response body" do
|
|
|
|
body <- do
|
|
|
|
resp <- EffectClass.liftEffect TestHelpers.mockResponse
|
|
|
|
Body.write (TestHelpers.stringToStream "test") resp
|
|
|
|
pure $ TestHelpers.getResponseBody resp
|
|
|
|
body ?= "test"
|
2017-07-17 23:42:13 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
bodySpec :: TestHelpers.Test
|
2017-07-14 06:28:57 +00:00
|
|
|
bodySpec = Spec.describe "Body" do
|
2018-08-30 22:01:49 +00:00
|
|
|
defaultHeadersSpec
|
2017-07-18 05:25:14 +00:00
|
|
|
readSpec
|
2017-07-17 23:42:13 +00:00
|
|
|
writeSpec
|