diff options
author | Wolfy-J <[email protected]> | 2018-06-11 14:57:47 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-11 14:57:47 +0300 |
commit | 846cec64f177a9cfef016a8225c4fae0faa29a0c (patch) | |
tree | a0dc1df41ab25793a2dd5ada80f3ca5de4615e7d /service/http/response_test.go | |
parent | 78fb74cfa5c0156f70c095a4f2cfee9851a9dc0f (diff) |
more tests for http
Diffstat (limited to 'service/http/response_test.go')
-rw-r--r-- | service/http/response_test.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/service/http/response_test.go b/service/http/response_test.go new file mode 100644 index 00000000..dfc08104 --- /dev/null +++ b/service/http/response_test.go @@ -0,0 +1,70 @@ +package http + +import ( + "testing" + "github.com/spiral/roadrunner" + "github.com/stretchr/testify/assert" + "net/http" + "bytes" +) + +type testWriter struct { + h http.Header + buf bytes.Buffer + wroteHeader bool + code int +} + +func (tw *testWriter) Header() http.Header { return tw.h } + +func (tw *testWriter) Write(p []byte) (int, error) { + if !tw.wroteHeader { + tw.WriteHeader(http.StatusOK) + } + + return tw.buf.Write(p) +} + +func (tw *testWriter) WriteHeader(code int) { tw.wroteHeader = true; tw.code = code } + +func TestNewResponse_Error(t *testing.T) { + r, err := NewResponse(&roadrunner.Payload{Context: []byte(`invalid payload`)}) + assert.Error(t, err) + assert.Nil(t, r) +} + +func TestNewResponse_Write(t *testing.T) { + r, err := NewResponse(&roadrunner.Payload{ + Context: []byte(`{"headers":{"key":["value"]},"status": 301}`), + Body: []byte(`sample body`), + }) + + 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, 301, w.code) + assert.Equal(t, "value", w.h.Get("key")) + assert.Equal(t, "sample body", w.buf.String()) +} + +func TestNewResponse_Stream(t *testing.T) { + r, err := NewResponse(&roadrunner.Payload{ + Context: []byte(`{"headers":{"key":["value"]},"status": 301}`), + }) + + r.body = &bytes.Buffer{} + r.body.(*bytes.Buffer).WriteString("hello world") + + 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, 301, w.code) + assert.Equal(t, "value", w.h.Get("key")) + assert.Equal(t, "hello world", w.buf.String()) +} |