Set the Content-Length header based on the body contents (#93)
This commit is contained in:
parent
4bcbbd95f8
commit
4c9acefaea
@ -78,6 +78,7 @@ module HTTPure.Response
|
|||||||
|
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
|
import Data.String as String
|
||||||
import Effect as Effect
|
import Effect as Effect
|
||||||
import Effect.Aff as Aff
|
import Effect.Aff as Aff
|
||||||
import Node.HTTP as HTTP
|
import Node.HTTP as HTTP
|
||||||
@ -104,8 +105,10 @@ type Response =
|
|||||||
send :: HTTP.Response -> Response -> Effect.Effect Unit
|
send :: HTTP.Response -> Response -> Effect.Effect Unit
|
||||||
send httpresponse { status, headers, body } = do
|
send httpresponse { status, headers, body } = do
|
||||||
Status.write httpresponse $ status
|
Status.write httpresponse $ status
|
||||||
Headers.write httpresponse $ headers
|
Headers.write httpresponse $ headers <> contentLength
|
||||||
Body.write httpresponse $ body
|
Body.write httpresponse $ body
|
||||||
|
where
|
||||||
|
contentLength = Headers.header "Content-Length" $ show $ String.length body
|
||||||
|
|
||||||
-- | For custom response statuses or providing a body for response codes that
|
-- | For custom response statuses or providing a body for response codes that
|
||||||
-- | don't typically send one.
|
-- | don't typically send one.
|
||||||
|
@ -19,6 +19,12 @@ sendSpec = Spec.describe "send" do
|
|||||||
Response.send httpResponse mockResponse
|
Response.send httpResponse mockResponse
|
||||||
pure $ TestHelpers.getResponseHeader "Test" httpResponse
|
pure $ TestHelpers.getResponseHeader "Test" httpResponse
|
||||||
header ?= "test"
|
header ?= "test"
|
||||||
|
Spec.it "sets the Content-Length header" do
|
||||||
|
header <- EffectClass.liftEffect do
|
||||||
|
httpResponse <- TestHelpers.mockResponse
|
||||||
|
Response.send httpResponse mockResponse
|
||||||
|
pure $ TestHelpers.getResponseHeader "Content-Length" httpResponse
|
||||||
|
header ?= "4"
|
||||||
Spec.it "writes the status" do
|
Spec.it "writes the status" do
|
||||||
status <- EffectClass.liftEffect do
|
status <- EffectClass.liftEffect do
|
||||||
httpResponse <- TestHelpers.mockResponse
|
httpResponse <- TestHelpers.mockResponse
|
||||||
|
Loading…
Reference in New Issue
Block a user