Skip to content

Commit 5983139

Browse files
Trim whitespace before JSON sniffing.
Co-authored-by: danmoz <danmoz@users.noreply.github.com>
1 parent ed09444 commit 5983139

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎req_resp.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func parseReply(data []byte, format string) (*Reply, error) {
5858
}, nil
5959
}
6060

61-
if len(data) > 0 && data[0] == '{' {
61+
trimmed := strings.TrimSpace(string(data))
62+
if len(trimmed) > 0 && trimmed[0] == '{' {
6263
var envelope struct {
6364
Type string `json:"type"`
6465
}

‎req_resp_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ func TestParseReplyHTTPJSON(t *testing.T) {
248248
}
249249
}
250250

251+
func TestParseReplyHTTPJSONWithLeadingWhitespace(t *testing.T) {
252+
reply, err := parseReply([]byte(" \n\t{\"type\":\"HTTPJSON-REP\",\"meta\":{\"status\":201},\"body\":\"created\"}"), eventFormatHTTPJSON)
253+
if err != nil {
254+
t.Fatalf("parseReply() error = %v", err)
255+
}
256+
if reply.Meta.Status != http.StatusCreated || reply.Body != "created" {
257+
t.Fatalf("reply = %#v, want status 201 and created body", reply)
258+
}
259+
}
260+
251261
func TestParseReplyRejectsMalformedHTTPJSONEnvelope(t *testing.T) {
252262
_, err := parseReply([]byte(`{"type":"HTTPJSON-REP","meta":{"headers":{"x-test":"scalar"}}}`), eventFormatHTTPJSON)
253263
if err == nil || !strings.Contains(err.Error(), "decode HTTPJSON response") {

0 commit comments

Comments
 (0)