Set the Content-Length header based on the body contents (#93)

This commit is contained in:
Connor Prussin 2018-07-10 22:23:28 -07:00 committed by GitHub
parent 4bcbbd95f8
commit 4c9acefaea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -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.

View File

@ -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