diff --git a/docs/Responses.md b/docs/Responses.md index b98cb8d..8073ac8 100644 --- a/docs/Responses.md +++ b/docs/Responses.md @@ -55,30 +55,29 @@ codes. ## Setting Response Headers If you need to return response headers, you can do so using the prime versions -of the response helpers. These functions take an `HTTPure.Headers` object. You -can construct an `HTTPure.Headers` in a few ways: +of the response helpers. These functions take an `HTTPure.ResponseHeaders` object. You +can construct an `HTTPure.ResponseHeaders` in a few ways: -- `HTTPure.empty` - Construct an empty `HTTPure.Headers` +- `HTTPure.empty` - Construct an empty `HTTPure.ResponseHeaders` - `HTTPure.header` - Given a string with a header name and a string with a - value, construct a singleton `HTTPure.Headers`. For instance: + value, construct a singleton `HTTPure.ResponseHeaders`. For instance: ```purescript -headers = HTTPure.header "X-My-Header" "value" +headers = HTTPure.header "Content-Type" "application/json" ``` -- `HTTPure.headers` - Construct a `HTTPure.Headers` from an `Array` of `Tuples` - of two `Strings`, where the first `String` is the header name and the second - `String` is the header value. For instance: +- `HTTPure.headers` - Construct a `HTTPure.Headers` from a record: ```purescript -headers = HTTPure.headers - [ Tuple "X-Header-A" "valueA" - , Tuple "X-Header-B" "valueB" - ] +headers = HTTPure.headers { + "Content-Type": "application/json", + "Set-Cookie": ["cookie-value-1", "cookie-value-2"], -- note: you can set multiple headers by using an Array + "X-My-CustomHeader": "some-value" +} ``` -Because `HTTPure.Headers` has an instance of `Semigroup`, you can also append -`HTTPure.Headers` objects: +Because `HTTPure.ResponseHeaders` has an instance of `Semigroup`, you can also append +`HTTPure.ResponseHeaders` objects: ```purescript headers = diff --git a/src/HTTPurple/Headers.purs b/src/HTTPurple/Headers.purs index 2f41774..b472ce1 100644 --- a/src/HTTPurple/Headers.purs +++ b/src/HTTPurple/Headers.purs @@ -71,13 +71,17 @@ newtype ResponseHeaders = ResponseHeaders (Map CaseInsensitiveString (Array Stri instance Semigroup ResponseHeaders where append (ResponseHeaders a) (ResponseHeaders b) = ResponseHeaders $ union b a --- | Allow a `RequestHeaders` to be represented as a string. This string is formatted +-- | Allow a `ResponseHeaders` to be represented as a string. This string is formatted -- | in HTTP headers format. instance Show ResponseHeaders where show (ResponseHeaders headers') = foldMapWithIndex showField headers' <> "\n" where showField key value = Array.foldMap (\v -> unwrap key <> ": " <> v <> "\n") value +-- | Compare two `ResponseHeaders` objects by comparing the underlying `Objects`. +instance Eq ResponseHeaders where + eq (ResponseHeaders a) (ResponseHeaders b) = eq a b + -- | Get the headers out of a HTTP `Request` object. read :: Request -> RequestHeaders read = requestHeaders >>> fold insertField Map.empty >>> RequestHeaders diff --git a/test/Test/HTTPurple/RequestSpec.purs b/test/Test/HTTPurple/RequestSpec.purs index 9a4d99d..9627287 100644 --- a/test/Test/HTTPurple/RequestSpec.purs +++ b/test/Test/HTTPurple/RequestSpec.purs @@ -12,7 +12,7 @@ import Effect.Aff (Aff) import Effect.Exception (error) import Foreign.Object (singleton) import HTTPurple.Body (toString) -import HTTPurple.Headers (headers) +import HTTPurple.Headers (headers, mkRequestHeaders) import HTTPurple.Method (Method(Post)) import HTTPurple.Request (fromHTTPRequest, fullPath) import HTTPurple.Version (Version(HTTP1_1)) @@ -50,7 +50,7 @@ fromHTTPRequestSpec = mock.query ?= singleton "a" "b" it "contains the correct headers" do mock <- mockRequest' # getRight - mock.headers ?= headers mockHeaders + mock.headers ?= mkRequestHeaders mockHeaders it "contains the correct body" do mockBody <- mockRequest' # getRight >>= (_.body >>> toString) mockBody ?= "body" diff --git a/test/Test/HTTPurple/ResponseSpec.purs b/test/Test/HTTPurple/ResponseSpec.purs index 3004559..5a24a40 100644 --- a/test/Test/HTTPurple/ResponseSpec.purs +++ b/test/Test/HTTPurple/ResponseSpec.purs @@ -6,19 +6,12 @@ import Data.Either (Either(Right)) import Effect.Aff (makeAff, nonCanceler) import Effect.Class (liftEffect) import HTTPurple.Body (defaultHeaders) -import HTTPurple.Headers (header) +import HTTPurple.Headers (header, toResponseHeaders) import HTTPurple.Response (emptyResponse, emptyResponse', response, response', send) import Node.Encoding (Encoding(UTF8)) import Node.HTTP (responseAsStream) import Node.Stream (end, writeString) -import Test.HTTPurple.TestHelpers - ( Test - , getResponseBody - , getResponseHeader - , getResponseStatus - , mockResponse - , (?=) - ) +import Test.HTTPurple.TestHelpers (Test, getResponseBody, getResponseHeader, getResponseStatus, mockResponse, (?=)) import Test.Spec (describe, it) sendSpec :: Test @@ -62,7 +55,7 @@ responseFunctionSpec = it "has only default headers" do resp <- response 123 "test" defaultHeaders' <- liftEffect $ defaultHeaders "test" - resp.headers ?= defaultHeaders' + resp.headers ?= toResponseHeaders defaultHeaders' it "has the right writeBody function" do body <- do resp <- response 123 "test" @@ -83,7 +76,7 @@ response'Spec = it "has the right headers" do resp <- mockResponse' defaultHeaders' <- liftEffect $ defaultHeaders "test" - resp.headers ?= defaultHeaders' <> mockHeaders + resp.headers ?= toResponseHeaders defaultHeaders' <> mockHeaders it "has the right writeBody function" do body <- do resp <- mockResponse' @@ -101,7 +94,7 @@ emptyResponseSpec = it "has only default headers" do resp <- emptyResponse 123 defaultHeaders' <- liftEffect $ defaultHeaders "" - resp.headers ?= defaultHeaders' + resp.headers ?= toResponseHeaders defaultHeaders' it "has the right writeBody function" do body <- do resp <- emptyResponse 123 @@ -122,7 +115,7 @@ emptyResponse'Spec = it "has the right headers" do resp <- mockResponse' defaultHeaders' <- liftEffect $ defaultHeaders "" - resp.headers ?= mockHeaders <> defaultHeaders' + resp.headers ?= mockHeaders <> toResponseHeaders defaultHeaders' it "has the right writeBody function" do body <- do resp <- mockResponse'