purescript-httpurple/test/Test/HTTPure/RequestSpec.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

81 lines
2.8 KiB
Haskell

module Test.HTTPure.RequestSpec where
import Prelude
import Data.Tuple as Tuple
import Foreign.Object as Object
import Test.Spec as Spec
import HTTPure.Headers as Headers
import HTTPure.Method as Method
import HTTPure.Request as Request
import HTTPure.Version as Version
import Test.HTTPure.TestHelpers as TestHelpers
import Test.HTTPure.TestHelpers ((?=))
fromHTTPRequestSpec :: TestHelpers.Test
fromHTTPRequestSpec = Spec.describe "fromHTTPRequest" do
Spec.it "contains the correct method" do
mock <- mockRequest
mock.method ?= Method.Post
Spec.it "contains the correct path" do
mock <- mockRequest
mock.path ?= [ "test" ]
Spec.it "contains the correct query" do
mock <- mockRequest
mock.query ?= Object.singleton "a" "b"
Spec.it "contains the correct headers" do
mock <- mockRequest
mock.headers ?= Headers.headers mockHeaders
Spec.it "contains the correct body" do
mock <- mockRequest
mock.body ?= "body"
Spec.it "contains the correct httpVersion" do
mock <- mockRequest
mock.httpVersion ?= Version.HTTP1_1
where
mockHeaders = [ Tuple.Tuple "Test" "test" ]
mockHTTPRequest =
TestHelpers.mockRequest "1.1" "POST" "/test?a=b" "body" mockHeaders
mockRequest = mockHTTPRequest >>= Request.fromHTTPRequest
fullPathSpec :: TestHelpers.Test
fullPathSpec = Spec.describe "fullPath" do
Spec.describe "without query parameters" do
Spec.it "is correct" do
mock <- mockRequest "/foo/bar"
Request.fullPath mock ?= "/foo/bar"
Spec.describe "with empty path segments" do
Spec.it "strips the empty segments" do
mock <- mockRequest "//foo////bar/"
Request.fullPath mock ?= "/foo/bar"
Spec.describe "with only query parameters" do
Spec.it "is correct" do
mock <- mockRequest "?a=b&c=d"
Request.fullPath mock ?= "/?a=b&c=d"
Spec.describe "with only empty query parameters" do
Spec.it "is has the default value of '' for the empty parameters" do
mock <- mockRequest "?a"
Request.fullPath mock ?= "/?a="
Spec.describe "with query parameters that have special characters" do
Spec.it "percent encodes query params" do
mock <- mockRequest "?a=%3Fx%3Dtest"
Request.fullPath mock ?= "/?a=%3Fx%3Dtest"
Spec.describe "with empty query parameters" do
Spec.it "strips out the empty arameters" do
mock <- mockRequest "?a=b&&&"
Request.fullPath mock ?= "/?a=b"
Spec.describe "with a mix of segments and query parameters" do
Spec.it "is correct" do
mock <- mockRequest "/foo///bar/?&a=b&&c"
Request.fullPath mock ?= "/foo/bar?a=b&c="
where
mockHTTPRequest path = TestHelpers.mockRequest "" "POST" path "body" []
mockRequest path = mockHTTPRequest path >>= Request.fromHTTPRequest
requestSpec :: TestHelpers.Test
requestSpec = Spec.describe "Request" do
fromHTTPRequestSpec
fullPathSpec