diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2016-11-01 15:24:11 +0000 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-11-01 17:01:30 +0000 |
| commit | b2c54afe1451f93e1fbbad257a151d8425cd308d (patch) | |
| tree | 8a5df30c71c5c51b644311b5bc79a144b0f752d5 /src/net/http/httptest | |
| parent | 3c2f607274b2284826ea887fa9d2ef62817df608 (diff) | |
| download | go-b2c54afe1451f93e1fbbad257a151d8425cd308d.tar.xz | |
net/http, net/http/httptest: make http2's TrailerPrefix work for http1
Go's http1 implementation originally had a mechanism to send HTTP
trailers based on pre-declaring the trailer keys whose values you'd
later let after the header was written.
http2 copied the same mechanism, but it was found to be unsufficient
for gRPC's wire protocol. A second trailer mechanism was added later
(but only to http2) for handlers that want to send a trailer without
knowing in advance they'd need to.
Copy the same mechanism back to http1 and document it.
Fixes #15754
Change-Id: I8c40d55e28b0e5b7087d3d1a904a392c56ee1f9b
Reviewed-on: https://go-review.googlesource.com/32479
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/net/http/httptest')
| -rw-r--r-- | src/net/http/httptest/recorder.go | 11 | ||||
| -rw-r--r-- | src/net/http/httptest/recorder_test.go | 2 |
2 files changed, 13 insertions, 0 deletions
diff --git a/src/net/http/httptest/recorder.go b/src/net/http/httptest/recorder.go index 24653031bd..5f1aa6af47 100644 --- a/src/net/http/httptest/recorder.go +++ b/src/net/http/httptest/recorder.go @@ -203,6 +203,17 @@ func (rw *ResponseRecorder) Result() *http.Response { res.Trailer[k] = vv2 } } + for k, vv := range rw.HeaderMap { + if !strings.HasPrefix(k, http.TrailerPrefix) { + continue + } + if res.Trailer == nil { + res.Trailer = make(http.Header) + } + for _, v := range vv { + res.Trailer.Add(strings.TrimPrefix(k, http.TrailerPrefix), v) + } + } return res } diff --git a/src/net/http/httptest/recorder_test.go b/src/net/http/httptest/recorder_test.go index ff9b9911a8..9afba4e556 100644 --- a/src/net/http/httptest/recorder_test.go +++ b/src/net/http/httptest/recorder_test.go @@ -207,6 +207,7 @@ func TestRecorder(t *testing.T) { w.Header().Set("Trailer-A", "valuea") w.Header().Set("Trailer-C", "valuec") w.Header().Set("Trailer-NotDeclared", "should be omitted") + w.Header().Set("Trailer:Trailer-D", "with prefix") }, check( hasStatus(200), @@ -216,6 +217,7 @@ func TestRecorder(t *testing.T) { hasTrailer("Trailer-A", "valuea"), hasTrailer("Trailer-C", "valuec"), hasNotTrailers("Non-Trailer", "Trailer-B", "Trailer-NotDeclared"), + hasTrailer("Trailer-D", "with prefix"), ), }, { |
