purescript-httpurple/test/Test/HTTPure/BodySpec.purs
Hardy Jones c208dffb7b Add HTTP version (#137)
* v0.8.1

* Add the HTTP version to `Request`

The `node-http` `Request` has the HTTP version on it.
We can make it available in our `Request` for consumers.

We went the naive approach first and typed it as a string.
There is some structure to the version,
so we could attempt to parse it.
It's unclear what we would do if parsing failed though.

Maybe we want something like:
```PureScript
data Version
  = Known { major :: Int, minor :: Int }
  | Unknown String
```

That would allow us to parse the known format,
and fallback to accepting anything else.
It's definitely something to discuss anyway.

* Make HTTP version its own data type

There are only a handful of HTTP versions that are commonly used.
We can talk about those explicitly and fallback to any arbitrary version.

The changes here try to follow the patterns elsewhere in the code base.
Hopefully, it's not too far off.
2019-04-25 10:13:04 -07:00

75 lines
2.7 KiB
Haskell

module Test.HTTPure.BodySpec where
import Prelude
import Effect.Class as EffectClass
import Node.Buffer as Buffer
import Node.Encoding as Encoding
import Test.Spec as Spec
import HTTPure.Body as Body
import HTTPure.Headers as Headers
import Test.HTTPure.TestHelpers as TestHelpers
import Test.HTTPure.TestHelpers ((?=))
readSpec :: TestHelpers.Test
readSpec = Spec.describe "read" do
Spec.it "is the body of the Request" do
request <- TestHelpers.mockRequest "" "GET" "" "test" []
body <- Body.read request
body ?= "test"
defaultHeadersSpec :: TestHelpers.Test
defaultHeadersSpec = Spec.describe "defaultHeaders" do
Spec.describe "String" do
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"
Spec.describe "Buffer" do
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"
writeSpec :: TestHelpers.Test
writeSpec = Spec.describe "write" do
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"
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"
bodySpec :: TestHelpers.Test
bodySpec = Spec.describe "Body" do
defaultHeadersSpec
readSpec
writeSpec