summaryrefslogtreecommitdiff
path: root/service/http/response_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'service/http/response_test.go')
-rw-r--r--service/http/response_test.go63
1 files changed, 61 insertions, 2 deletions
diff --git a/service/http/response_test.go b/service/http/response_test.go
index f885be7f..ad524567 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,53 @@ 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"))
+}
+
+func TestWrite_HandlesHandlesWhitespacesInTrailer(t *testing.T) {
+ r, err := NewResponse(&roadrunner.Payload{
+ Context: []byte(
+ `{"headers":{"trailer":["foo\t,bar , baz"],"foo":["a"],"bar":["b"],"baz":["c"]},"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.Equal(t, "a", w.h.Get("Trailer:foo"))
+ assert.Equal(t, "b", w.h.Get("Trailer:bar"))
+ assert.Equal(t, "c", w.h.Get("Trailer:baz"))
+}