Skip to content

Commit

Permalink
Add gRPC tests for trailers-only (#904)
Browse files Browse the repository at this point in the history
This mirrors the gRPC-Web test cases that also handle different forms of
trailers in responses.

These weren't originally included because I didn't think they would
work: I thought that the "net/http" server would encode these on the
wire in HTTP/2 using a HEADER frame (_without_ END_STREAM flag) followed
by an empty DATA frame (with END_STREAM flag). But the gRPC spec (and
Google's implementations) require a trailers-only response in HTTP/2 be
a single HEADER frame with END_STREAM flag set.

However, I was mistaken: the Go "net/http" server does exactly what we
need it to. It would only use two frames (i.e. a second, empty DATA
frame) if the handler flushes the `http.ResponseWriter` before it
returns. So I didn't have to actually touch the reference server for
this to work. 🎉
  • Loading branch information
jhump authored Aug 2, 2024
1 parent 5ecd3bb commit ad21d7f
Showing 1 changed file with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
name: gRPC Trailers
mode: TEST_MODE_CLIENT
relevantProtocols:
- PROTOCOL_GRPC
relevantHttpVersions:
- HTTP_VERSION_2
relevantCodecs:
- CODEC_PROTO
# These tests verify that a gRPC client can handle both normal responses
# with trailers and trailers-only responses (trailers in headers).
testCases:
# Trailers and status are separate from headers (no other response messages)
- request:
testName: trailers-at-end/expected
streamType: STREAM_TYPE_UNARY
requestMessages:
- "@type": type.googleapis.com/connectrpc.conformance.v1.UnaryRequest
responseDefinition:
rawResponse:
status_code: 200
headers:
- name: content-type
value: [ "application/grpc" ]
- name: x-custom-header
value: [ "foo" ]
trailers:
- name: grpc-status
value: [ "9" ]
- name: grpc-message
value: [ "error" ]
- name: x-custom-trailer
value: [ "bing" ]
expectedResponse:
responseHeaders:
- name: x-custom-header
value: [ "foo" ]
error:
code: 9
message: error
responseTrailers:
- name: x-custom-trailer
value: [ "bing" ]
- request:
testName: trailers-at-end/duplicate-metadata
streamType: STREAM_TYPE_UNARY
requestMessages:
- "@type": type.googleapis.com/connectrpc.conformance.v1.UnaryRequest
responseDefinition:
rawResponse:
status_code: 200
headers:
- name: content-type
value: [ "application/grpc" ]
- name: x-custom-header
value: [ "foo", "bar", "baz" ]
trailers:
- name: grpc-status
value: [ "9" ]
- name: grpc-message
value: [ "error" ]
- name: x-custom-trailer
value: [ "bing", "quuz" ]
expectedResponse:
responseHeaders:
- name: x-custom-header
value: [ "foo", "bar", "baz" ]
error:
code: 9
message: error
responseTrailers:
- name: x-custom-trailer
value: [ "bing", "quuz" ]

# Trailers-only responses, where status and trailers are in HTTP headers
- request:
testName: trailers-only/expected
streamType: STREAM_TYPE_UNARY
requestMessages:
- "@type": type.googleapis.com/connectrpc.conformance.v1.UnaryRequest
responseDefinition:
rawResponse:
status_code: 200
headers:
- name: content-type
value: [ "application/grpc" ]
- name: x-custom-trailer
value: [ "bing" ]
- name: grpc-status
value: [ "9" ]
- name: grpc-message
value: [ "error" ]
# a "magic header" is required to get reference server to emit
# valid HTTP/2 trailers-only frames (must have end-stream flag
# on header frame; no data frame allowed)
- name: x-connect-trailers-only
value: ["1"]
expectedResponse:
error:
code: 9
message: error
responseTrailers:
- name: x-custom-trailer
value: [ "bing" ]
- request:
testName: trailers-only/duplicate-metadata
streamType: STREAM_TYPE_UNARY
requestMessages:
- "@type": type.googleapis.com/connectrpc.conformance.v1.UnaryRequest
responseDefinition:
rawResponse:
status_code: 200
headers:
- name: content-type
value: [ "application/grpc" ]
- name: x-custom-trailer
value: [ "bing", "quuz" ]
- name: grpc-status
value: [ "9" ]
- name: grpc-message
value: [ "error" ]
# a "magic header" is required to get reference server to emit
# valid HTTP/2 trailers-only frames (must have end-stream flag
# on header frame; no data frame allowed)
- name: x-connect-trailers-only
value: ["1"]
expectedResponse:
error:
code: 9
message: error
responseTrailers:
- name: x-custom-trailer
value: [ "bing", "quuz" ]

0 comments on commit ad21d7f

Please sign in to comment.