Skip to content

Commit

Permalink
Update content length after replacing request body (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
borodiychuk authored Nov 20, 2022
1 parent cadbdda commit 657743e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions openapi3filter/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ func ValidateRequestBody(ctx context.Context, input *RequestValidationInput, req
}
// Put the data back into the input
req.Body = ioutil.NopCloser(bytes.NewReader(data))
req.ContentLength = int64(len(data))
}

return nil
Expand Down
50 changes: 41 additions & 9 deletions openapi3filter/validate_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ paths:
properties:
subCategory:
type: string
category:
type: string
default: Sweets
responses:
'201':
description: Created
Expand Down Expand Up @@ -95,25 +98,38 @@ components:

type testRequestBody struct {
SubCategory string `json:"subCategory"`
Category string `json:"category,omitempty"`
}
type args struct {
requestBody *testRequestBody
url string
apiKey string
}
tests := []struct {
name string
args args
expectedErr error
name string
args args
expectedModification bool
expectedErr error
}{
{
name: "Valid request",
name: "Valid request with all fields set",
args: args{
requestBody: &testRequestBody{SubCategory: "Chocolate", Category: "Food"},
url: "/category?category=cookies",
apiKey: "SomeKey",
},
expectedModification: false,
expectedErr: nil,
},
{
name: "Valid request without certain fields",
args: args{
requestBody: &testRequestBody{SubCategory: "Chocolate"},
url: "/category?category=cookies",
apiKey: "SomeKey",
},
expectedErr: nil,
expectedModification: true,
expectedErr: nil,
},
{
name: "Invalid operation params",
Expand All @@ -122,7 +138,8 @@ components:
url: "/category?invalidCategory=badCookie",
apiKey: "SomeKey",
},
expectedErr: &RequestError{},
expectedModification: false,
expectedErr: &RequestError{},
},
{
name: "Invalid request body",
Expand All @@ -131,7 +148,8 @@ components:
url: "/category?category=cookies",
apiKey: "SomeKey",
},
expectedErr: &RequestError{},
expectedModification: false,
expectedErr: &RequestError{},
},
{
name: "Invalid security",
Expand All @@ -140,7 +158,8 @@ components:
url: "/category?category=cookies",
apiKey: "",
},
expectedErr: &SecurityRequirementsError{},
expectedModification: false,
expectedErr: &SecurityRequirementsError{},
},
{
name: "Invalid request body and security",
Expand All @@ -149,16 +168,19 @@ components:
url: "/category?category=cookies",
apiKey: "",
},
expectedErr: &SecurityRequirementsError{},
expectedModification: false,
expectedErr: &SecurityRequirementsError{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var requestBody io.Reader
var originalBodySize int
if tc.args.requestBody != nil {
testingBody, err := json.Marshal(tc.args.requestBody)
require.NoError(t, err)
requestBody = bytes.NewReader(testingBody)
originalBodySize = len(testingBody)
}
req, err := http.NewRequest(http.MethodPost, tc.args.url, requestBody)
require.NoError(t, err)
Expand All @@ -180,6 +202,16 @@ components:
}
err = ValidateRequest(context.Background(), validationInput)
assert.IsType(t, tc.expectedErr, err, "ValidateRequest(): error = %v, expectedError %v", err, tc.expectedErr)
if tc.expectedErr != nil {
return
}
body, err := io.ReadAll(validationInput.Request.Body)
contentLen := int(validationInput.Request.ContentLength)
bodySize := len(body)
assert.NoError(t, err, "unable to read request body: %v", err)
assert.Equal(t, contentLen, bodySize, "expect ContentLength %d to equal body size %d", contentLen, bodySize)
bodyModified := originalBodySize != bodySize
assert.Equal(t, bodyModified, tc.expectedModification, "expect request body modification happened: %t, expected %t", bodyModified, tc.expectedModification)
})
}
}

0 comments on commit 657743e

Please sign in to comment.