diff options
author | Garry Filakhtov <[email protected]> | 2019-11-23 22:54:46 +1100 |
---|---|---|
committer | Garry Filakhtov <[email protected]> | 2019-11-23 23:32:44 +1100 |
commit | 8f93aa61da6e66cbd13de96ef53e476556d391a7 (patch) | |
tree | d90152357098325521ab4e93f59f98cc151d5df2 /service/http/response_test.go | |
parent | b81f2b121eb3a49372662d0bc9c19c53366f33fc (diff) |
Provide support for HTTP/2 trailers
Neither PHP nor PSR-7 do not natively support HTTP trailers. Golang
provides support and this commit enables trailers emulation. When PHP
sends a "Trailer" header in response, supplying a comma separated list
of headers they will be converted by RoadRunner to HTTP/2 trailers.
Diffstat (limited to 'service/http/response_test.go')
-rw-r--r-- | service/http/response_test.go | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/service/http/response_test.go b/service/http/response_test.go index f885be7f..7ba9d8d4 100644 --- a/service/http/response_test.go +++ b/service/http/response_test.go @@ -3,10 +3,11 @@ package http import ( "bytes" "errors" - "github.com/spiral/roadrunner" - "github.com/stretchr/testify/assert" "net/http" "testing" + + "github.com/spiral/roadrunner" + "github.com/stretchr/testify/assert" ) type testWriter struct { @@ -15,6 +16,8 @@ type testWriter struct { wroteHeader bool code int err error + pushErr error + pushes []string } func (tw *testWriter) Header() http.Header { return tw.h } @@ -34,6 +37,12 @@ func (tw *testWriter) Write(p []byte) (int, error) { func (tw *testWriter) WriteHeader(code int) { tw.wroteHeader = true; tw.code = code } +func (tw *testWriter) Push(target string, opts *http.PushOptions) error { + tw.pushes = append(tw.pushes, target) + + return tw.pushErr +} + func TestNewResponse_Error(t *testing.T) { r, err := NewResponse(&roadrunner.Payload{Context: []byte(`invalid payload`)}) assert.Error(t, err) @@ -90,3 +99,36 @@ func TestNewResponse_StreamError(t *testing.T) { w := &testWriter{h: http.Header(make(map[string][]string)), err: errors.New("error")} assert.Error(t, r.Write(w)) } + +func TestWrite_HandlesPush(t *testing.T) { + r, err := NewResponse(&roadrunner.Payload{ + Context: []byte(`{"headers":{"http2-push":["/test.js"],"content-type":["text/html"]},"status": 200}`), + }) + + assert.NoError(t, err) + assert.NotNil(t, r) + + w := &testWriter{h: http.Header(make(map[string][]string))} + assert.NoError(t, r.Write(w)) + + assert.Nil(t, w.h["http2-push"]) + assert.Equal(t, []string{"/test.js"}, w.pushes) +} + +func TestWrite_HandlesTrailers(t *testing.T) { + r, err := NewResponse(&roadrunner.Payload{ + Context: []byte(`{"headers":{"trailer":["foo, bar", "baz"],"foo":["test"],"bar":["demo"]},"status": 200}`), + }) + + assert.NoError(t, err) + assert.NotNil(t, r) + + w := &testWriter{h: http.Header(make(map[string][]string))} + assert.NoError(t, r.Write(w)) + + assert.Nil(t, w.h["trailer"]) + assert.Nil(t, w.h["foo"]) + assert.Nil(t, w.h["baz"]) + assert.Equal(t, "test", w.h.Get("Trailer:foo")) + assert.Equal(t, "demo", w.h.Get("Trailer:bar")) +} |