Skip to content

Commit

Permalink
Add tests for unexpected raw responses (#774)
Browse files Browse the repository at this point in the history
This adds tests for unexpected values such as:

* Unmapped HTTP code
* Nonexistent HTTP code
* Invalid Content-Type
* Invalid Content-Encoding

---------

Co-authored-by: Josh Humphries <[email protected]>
  • Loading branch information
smaye81 and jhump authored Feb 15, 2024
1 parent 208a0b5 commit 926b534
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Unexpected Responses
mode: TEST_MODE_CLIENT
relevantCompressions:
- COMPRESSION_IDENTITY
relevantCodecs:
- CODEC_PROTO
testCases:
- request:
testName: unmapped HTTP status code
service: connectrpc.conformance.v1.ConformanceService
method: Unary
streamType: STREAM_TYPE_UNARY
requestMessages:
- "@type": type.googleapis.com/connectrpc.conformance.v1.UnaryRequest
responseDefinition:
rawResponse:
statusCode: 418 # I'm a teapot
expectedResponse:
error:
# unknown
code: 2
- request:
testName: nonexistent HTTP status code
service: connectrpc.conformance.v1.ConformanceService
method: Unary
streamType: STREAM_TYPE_UNARY
requestMessages:
- "@type": type.googleapis.com/connectrpc.conformance.v1.UnaryRequest
responseDefinition:
rawResponse:
statusCode: 800
expectedResponse:
error:
# unknown
code: 2
# TODO - Fixed in https://github.com/connectrpc/connect-go/pull/679
# Uncomment when released
# - request:
# testName: unexpected content type
# service: connectrpc.conformance.v1.ConformanceService
# method: Unary
# streamType: STREAM_TYPE_UNARY
# requestMessages:
# - "@type": type.googleapis.com/connectrpc.conformance.v1.UnaryRequest
# responseDefinition:
# rawResponse:
# headers:
# - name: Content-Type
# value: ["junk"]
# expectedResponse:
# error:
# # internal
# code: 13
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,4 @@ testCases:
error:
# unavailable
code: 14
- request:
testName: im-a-teapot
service: connectrpc.conformance.v1.ConformanceService
method: Unary
streamType: STREAM_TYPE_UNARY
requestMessages:
- "@type": type.googleapis.com/connectrpc.conformance.v1.UnaryRequest
responseDefinition:
rawResponse:
statusCode: 418
expectedResponse:
error:
# unknown
code: 2

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Connect Unexpected Responses
mode: TEST_MODE_CLIENT
relevantProtocols:
- PROTOCOL_CONNECT
relevantCompressions:
- COMPRESSION_IDENTITY
relevantCodecs:
- CODEC_JSON
testCases:
- request:
testName: unexpected content encoding
service: connectrpc.conformance.v1.ConformanceService
method: Unary
streamType: STREAM_TYPE_UNARY
requestMessages:
- "@type": type.googleapis.com/connectrpc.conformance.v1.UnaryRequest
responseDefinition:
rawResponse:
unary:
text: "{\"payload\": {\"data\": \"dGVzdCByZXNwb25zZQ\"}}"
headers:
- name: content-type
value: [ "application/json" ]
- name: content-encoding
value: ["foo"]
expectedResponse:
error:
# internal
code: 13
7 changes: 6 additions & 1 deletion internal/app/referenceserver/raw_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ func (r *rawResponseWriter) finish(feedback *feedbackPrinter) {
for _, hdr := range resp.Trailers {
r.respWriter.Header().Add("Trailer", hdr.Name)
}
r.respWriter.WriteHeader(int(resp.StatusCode))
statusCode := int(resp.StatusCode)
// If no status code was specified in the raw response, default to 200
if statusCode == 0 {
statusCode = 200
}
r.respWriter.WriteHeader(statusCode)
switch contents := resp.Body.(type) {
case *v1.RawHTTPResponse_Unary:
_ = internal.WriteRawMessageContents(contents.Unary, r.respWriter)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proto/connectrpc/conformance/v1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ message StreamContents {
// custom responses with odd properties (including certain kinds of
// malformed responses) to test edge cases in clients.
message RawHTTPResponse {
// If status code is not specified, it will default to a 200 response code.
uint32 status_code = 1;
repeated Header headers = 2;
oneof body {
Expand Down

0 comments on commit 926b534

Please sign in to comment.