-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
json handler: return full ratelimit service response as json (#148)
Previously an HTTP POST to /json would only return an HTTP status code, not all the other details supported by grpc ratelimit responses. With this change an HTTP POST to /json receives the full proto3 response encoded as json by jsonpb. It seems unlikely that anyone would be parsing the text "over limit" from the HTTP body instead of just reading the 429 response code, but for anyone doing that this would be a breaking change. Signed-off-by: David Weitzman <[email protected]>
- Loading branch information
Showing
6 changed files
with
196 additions
and
11 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,86 @@ | ||
package server_test | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" | ||
|
||
"github.com/envoyproxy/ratelimit/src/server" | ||
mock_v2 "github.com/envoyproxy/ratelimit/test/mocks/rls" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func assertHttpResponse(t *testing.T, | ||
handler http.HandlerFunc, | ||
requestBody string, | ||
expectedStatusCode int, | ||
expectedContentType string, | ||
expectedResponseBody string) { | ||
|
||
t.Helper() | ||
assert := assert.New(t) | ||
|
||
req := httptest.NewRequest("METHOD_NOT_CHECKED", "/path_not_checked", strings.NewReader(requestBody)) | ||
w := httptest.NewRecorder() | ||
handler(w, req) | ||
|
||
resp := w.Result() | ||
actualBody, _ := ioutil.ReadAll(resp.Body) | ||
assert.Equal(expectedContentType, resp.Header.Get("Content-Type")) | ||
assert.Equal(expectedStatusCode, resp.StatusCode) | ||
assert.Equal(expectedResponseBody, string(actualBody)) | ||
} | ||
|
||
func TestJsonHandler(t *testing.T) { | ||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
|
||
rls := mock_v2.NewMockRateLimitServiceServer(controller) | ||
handler := server.NewJsonHandler(rls) | ||
|
||
// Missing request body | ||
assertHttpResponse(t, handler, "", 400, "text/plain; charset=utf-8", "EOF\n") | ||
|
||
// Request body is not valid json | ||
assertHttpResponse(t, handler, "}", 400, "text/plain; charset=utf-8", "invalid character '}' looking for beginning of value\n") | ||
|
||
// Unknown response code | ||
rls.EXPECT().ShouldRateLimit(nil, &pb.RateLimitRequest{ | ||
Domain: "foo", | ||
}).Return(&pb.RateLimitResponse{}, nil) | ||
assertHttpResponse(t, handler, `{"domain": "foo"}`, 500, "application/json", "{}") | ||
|
||
// ratelimit service error | ||
rls.EXPECT().ShouldRateLimit(nil, &pb.RateLimitRequest{ | ||
Domain: "foo", | ||
}).Return(nil, fmt.Errorf("some error")) | ||
assertHttpResponse(t, handler, `{"domain": "foo"}`, 400, "text/plain; charset=utf-8", "some error\n") | ||
|
||
// json unmarshaling error | ||
rls.EXPECT().ShouldRateLimit(nil, &pb.RateLimitRequest{ | ||
Domain: "foo", | ||
}).Return(nil, nil) | ||
assertHttpResponse(t, handler, `{"domain": "foo"}`, 500, "text/plain; charset=utf-8", "error marshaling proto3 to json: Marshal called with nil\n") | ||
|
||
// successful request, not rate limited | ||
rls.EXPECT().ShouldRateLimit(nil, &pb.RateLimitRequest{ | ||
Domain: "foo", | ||
}).Return(&pb.RateLimitResponse{ | ||
OverallCode: pb.RateLimitResponse_OK, | ||
}, nil) | ||
assertHttpResponse(t, handler, `{"domain": "foo"}`, 200, "application/json", `{"overallCode":"OK"}`) | ||
|
||
// successful request, rate limited | ||
rls.EXPECT().ShouldRateLimit(nil, &pb.RateLimitRequest{ | ||
Domain: "foo", | ||
}).Return(&pb.RateLimitResponse{ | ||
OverallCode: pb.RateLimitResponse_OVER_LIMIT, | ||
}, nil) | ||
assertHttpResponse(t, handler, `{"domain": "foo"}`, 429, "application/json", `{"overallCode":"OVER_LIMIT"}`) | ||
} |