From 2673bd4b0b5e2d091e1548bb432ffa96336eacbb Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Sat, 25 Aug 2018 07:49:28 +0300 Subject: [PATCH] Fix Content-Length for non-ASCII response bodies (#103) Fixes #101 --- src/HTTPure/Body.purs | 3 +-- test/Test/HTTPure/BodySpec.purs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/HTTPure/Body.purs b/src/HTTPure/Body.purs index ee57547..73413e1 100644 --- a/src/HTTPure/Body.purs +++ b/src/HTTPure/Body.purs @@ -8,7 +8,6 @@ module HTTPure.Body import Prelude import Data.Either as Either -import Data.String as String import Effect as Effect import Effect.Aff as Aff import Effect.Ref as Ref @@ -47,5 +46,5 @@ write response body = void do -- | Get the size of the body in bytes size :: Body -> Effect.Effect Int -size (StringBody body) = pure $ String.length body +size (StringBody body) = Buffer.fromString body Encoding.UTF8 >>= Buffer.size size (BinaryBody body) = Buffer.size body diff --git a/test/Test/HTTPure/BodySpec.purs b/test/Test/HTTPure/BodySpec.purs index c3c36cc..a54dcab 100644 --- a/test/Test/HTTPure/BodySpec.purs +++ b/test/Test/HTTPure/BodySpec.purs @@ -3,6 +3,8 @@ module Test.HTTPure.BodySpec where import Prelude import Effect.Class as EffectClass +import Node.Buffer as Buffer +import Node.Encoding as Encoding import Test.Spec as Spec import HTTPure.Body as Body @@ -17,6 +19,22 @@ readSpec = Spec.describe "read" do body <- Body.read request body ?= "test" +sizeSpec :: TestHelpers.Test +sizeSpec = Spec.describe "size" do + Spec.it "returns the correct size for ASCII string body" do + size <- EffectClass.liftEffect $ Body.size $ Body.StringBody "ascii" + size ?= 5 + + Spec.it "returns the correct size for UTF-8 string body" do + size <- EffectClass.liftEffect $ Body.size $ Body.StringBody "\x2603" -- snowman + size ?= 3 + + Spec.it "returns the correct size for binary body" do + size <- EffectClass.liftEffect do + buf <- Buffer.fromString "foobar" Encoding.UTF8 + Body.size $ Body.BinaryBody buf + size ?= 6 + writeSpec :: TestHelpers.Test writeSpec = Spec.describe "write" do Spec.it "writes the string to the Response body" do @@ -29,4 +47,5 @@ writeSpec = Spec.describe "write" do bodySpec :: TestHelpers.Test bodySpec = Spec.describe "Body" do readSpec + sizeSpec writeSpec