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 Data.String as String
import Effect as Effect
import Effect.Aff as Aff
import Node.HTTP as HTTP
@ -104,8 +105,10 @@ type Response =
send :: HTTP.Response -> Response -> Effect.Effect Unit
send httpresponse { status, headers, body } = do
Status.write httpresponse $ status
Headers.write httpresponse $ headers
Headers.write httpresponse $ headers <> contentLength
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
-- | don't typically send one.

View File

@ -19,6 +19,12 @@ sendSpec = Spec.describe "send" do
Response.send httpResponse mockResponse
pure $ TestHelpers.getResponseHeader "Test" httpResponse
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
status <- EffectClass.liftEffect do
httpResponse <- TestHelpers.mockResponse