diff options
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")) +} |