Skip to content

Commit

Permalink
Removing content length from JSON helper (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotirios Mantziaris authored Sep 16, 2022
1 parent b975aa1 commit c471d13
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 41 deletions.
27 changes: 0 additions & 27 deletions client/http/encoding/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ func FromResponse(ctx context.Context, rsp *http.Response, payload interface{})
}
}()

err = validateContentLengthHeader(rsp, len(buf))
if err != nil {
log.FromContext(ctx).Warn(err)
}

return json.DecodeRaw(buf, payload)
}

Expand All @@ -77,25 +72,3 @@ func validateContentTypeHeader(rsp *http.Response) error {
return fmt.Errorf("invalid content type provided: %s", header[0])
}
}

func validateContentLengthHeader(rsp *http.Response, length int) error {
header, ok := rsp.Header[encoding.ContentLengthHeader]
if !ok {
return nil
}

if len(header) == 0 {
return nil
}

expected, err := strconv.Atoi(header[0])
if err != nil {
return fmt.Errorf("failed to convert content length to int: %w", err)
}

if expected != length {
return fmt.Errorf("expected content length is %d but got %d", expected, length)
}

return nil
}
17 changes: 7 additions & 10 deletions client/http/encoding/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@ func TestFromResponse(t *testing.T) {
require.NoError(t, err)

type args struct {
contentType *string
contentLength string
payload []byte
contentType *string
payload []byte
}
tests := map[string]struct {
args args
expectedErr string
}{
"success ": {args: args{contentType: stringPointer(json.Type), contentLength: "20", payload: buf}},
"success with charset": {args: args{contentType: stringPointer(json.TypeCharset), contentLength: "20", payload: buf}},
"success with invalid content length": {args: args{contentType: stringPointer(json.Type), contentLength: "20", payload: buf}},
"success, invalid content length value": {args: args{contentType: stringPointer(json.Type), contentLength: "a", payload: buf}},
"failure, wrong content type": {args: args{contentType: stringPointer("text/plain"), contentLength: "20", payload: buf}, expectedErr: "invalid content type provided: text/plain"},
"failure, empty content type": {args: args{contentType: stringPointer(""), contentLength: "20", payload: buf}, expectedErr: "invalid content type provided: "},
"success ": {args: args{contentType: stringPointer(json.Type), payload: buf}},
"success with charset": {args: args{contentType: stringPointer(json.TypeCharset), payload: buf}},
"success with invalid content length": {args: args{contentType: stringPointer(json.Type), payload: buf}},
"failure, wrong content type": {args: args{contentType: stringPointer("text/plain"), payload: buf}, expectedErr: "invalid content type provided: text/plain"},
"failure, empty content type": {args: args{contentType: stringPointer(""), payload: buf}, expectedErr: "invalid content type provided: "},
}
for name, tt := range tests {
tt := tt
Expand All @@ -60,7 +58,6 @@ func TestFromResponse(t *testing.T) {
rsp.Header().Set(encoding.ContentTypeHeader, *tt.args.contentType)
}

rsp.Header().Set(encoding.ContentLengthHeader, tt.args.contentLength)
_, err := rsp.Write(tt.args.payload)
require.NoError(t, err)

Expand Down
2 changes: 0 additions & 2 deletions component/http/v2/encoding/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package json
import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/beatlabs/patron/encoding"
Expand Down Expand Up @@ -53,7 +52,6 @@ func WriteResponse(w http.ResponseWriter, status int, payload interface{}) error

w.WriteHeader(status)
w.Header().Add(encoding.ContentTypeHeader, json.Type)
w.Header().Add(encoding.ContentLengthHeader, strconv.FormatInt(int64(len(buf)), 10))

_, err = w.Write(buf)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions component/http/v2/encoding/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"

"github.com/beatlabs/patron/encoding"
Expand Down Expand Up @@ -89,7 +88,6 @@ func TestWriteResponse(t *testing.T) {
assert.Equal(t, http.StatusOK, rsp.Code)
assert.Equal(t, expectedBuf, rsp.Body.Bytes())
assert.Equal(t, json.Type, rsp.Header().Get(encoding.ContentTypeHeader))
assert.Equal(t, strconv.FormatInt(int64(len(expectedBuf)), 10), rsp.Header().Get(encoding.ContentLengthHeader))
}

func TestValidateAcceptHeader(t *testing.T) {
Expand Down

0 comments on commit c471d13

Please sign in to comment.