-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add RequestLimitMiddleware for Service.MaxRequestSize config (#321
) fixes edgexfoundry/edgex-go#3612 Signed-off-by: Ginny Guan <[email protected]>
- Loading branch information
1 parent
960b4ca
commit 42b690d
Showing
3 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright (C) 2022 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v2/common" | ||
commonDTO "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestRequestLimitMiddleware(t *testing.T) { | ||
lc := logger.NewMockClient() | ||
payload := make([]byte, 2048) | ||
tests := []struct { | ||
name string | ||
sizeLimit int64 | ||
errorExpected bool | ||
}{ | ||
{"Valid unlimited size", int64(0), false}, | ||
{"Valid size", int64(2), false}, | ||
{"Invalid size", int64(1), true}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
middleware := RequestLimitMiddleware(testCase.sizeLimit, lc) | ||
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
|
||
reader := strings.NewReader(string(payload)) | ||
req, err := http.NewRequest(http.MethodPost, "/", reader) | ||
require.NoError(t, err) | ||
|
||
recorder := httptest.NewRecorder() | ||
handler.ServeHTTP(recorder, req) | ||
resp := recorder.Result() | ||
|
||
if testCase.errorExpected { | ||
var res commonDTO.BaseResponse | ||
err = json.Unmarshal(recorder.Body.Bytes(), &res) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, http.StatusRequestEntityTooLarge, resp.StatusCode, "http status code is not as expected") | ||
assert.Equal(t, common.ContentTypeJSON, resp.Header.Get(common.ContentType), "http header Content-Type is not as expected") | ||
assert.Equal(t, http.StatusRequestEntityTooLarge, int(res.StatusCode), "Response status code not as expected") | ||
assert.NotEmpty(t, res.Message, "Response message doesn't contain the error message") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters