Skip to content

Commit 42da94f

Browse files
ldebruijnldebruijn
andauthored
feat(gql-parsing): Only attempt to parse request body if content leng… (#40)
Fix panic when trying to read request body without there being one. --------- Co-authored-by: ldebruijn <[email protected]>
1 parent cd40f0f commit 42da94f

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

internal/business/gql/gql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ type PersistedQuery struct {
2222
}
2323

2424
func ParseRequestPayload(r *http.Request) ([]RequestData, error) {
25+
if r.ContentLength < 1 {
26+
return []RequestData{}, nil
27+
}
28+
2529
body, err := io.ReadAll(r.Body)
2630
if err != nil {
2731
return []RequestData{}, err

internal/business/gql/gql_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ func TestParseRequestPayload(t *testing.T) {
9797
},
9898
wantErr: false,
9999
},
100+
{
101+
name: "Handles request without body gracefully",
102+
args: args{
103+
r: func() *http.Request {
104+
return httptest.NewRequest("POST", "/graphql", nil)
105+
}(),
106+
},
107+
want: []RequestData{},
108+
wantErr: false,
109+
},
100110
}
101111
for _, tt := range tests {
102112
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)