diff options
author | Valery Piashchynski <[email protected]> | 2020-02-07 15:52:57 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-02-07 15:52:57 +0300 |
commit | 785e58f8bea7eb9052babc1dd0e94859328728e5 (patch) | |
tree | 8e301f947211d0a515f79d2b33e85598eb11766c /service/http | |
parent | 1f9a12a7b3ab745ee39afe2bbc4a19d21fbe7d91 (diff) |
Update README.md (remove travis, replace with github actions)
Fix innefectual usage of maps
Update headers (canonical usage)
Add golangci-lint check to github actions and go 1.13
Diffstat (limited to 'service/http')
-rw-r--r-- | service/http/constants.go | 6 | ||||
-rw-r--r-- | service/http/response.go | 29 | ||||
-rw-r--r-- | service/http/response_test.go | 25 | ||||
-rw-r--r-- | service/http/ssl_test.go | 2 |
4 files changed, 38 insertions, 24 deletions
diff --git a/service/http/constants.go b/service/http/constants.go new file mode 100644 index 00000000..a25f52a4 --- /dev/null +++ b/service/http/constants.go @@ -0,0 +1,6 @@ +package http + +import "net/http" + +var http2pushHeaderKey = http.CanonicalHeaderKey("http2-push") +var trailerHeaderKey = http.CanonicalHeaderKey("trailer") diff --git a/service/http/response.go b/service/http/response.go index aafaed13..16434a7c 100644 --- a/service/http/response.go +++ b/service/http/response.go @@ -33,7 +33,8 @@ func NewResponse(p *roadrunner.Payload) (*Response, error) { // Write writes response headers, status and body into ResponseWriter. func (r *Response) Write(w http.ResponseWriter) error { - p, h := handlePushHeaders(r.Headers) + // INFO map is the reference type in golang + p := handlePushHeaders(r.Headers) if pusher, ok := w.(http.Pusher); ok { for _, v := range p { err := pusher.Push(v, nil) @@ -43,7 +44,7 @@ func (r *Response) Write(w http.ResponseWriter) error { } } - h = handleTrailers(h) + handleTrailers(r.Headers) for n, h := range r.Headers { for _, v := range h { w.Header().Add(n, v) @@ -68,26 +69,24 @@ func (r *Response) Write(w http.ResponseWriter) error { return nil } -func handlePushHeaders(h map[string][]string) ([]string, map[string][]string) { +func handlePushHeaders(h map[string][]string) []string { var p []string - pushHeader, ok := h["http2-push"] + pushHeader, ok := h[http2pushHeaderKey] if !ok { - return p, h + return p } - for _, v := range pushHeader { - p = append(p, v) - } + p = append(p, pushHeader...) - delete(h, "http2-push") + delete(h, http2pushHeaderKey) - return p, h + return p } -func handleTrailers(h map[string][]string) map[string][]string { - trailers, ok := h["trailer"] +func handleTrailers(h map[string][]string) { + trailers, ok := h[trailerHeaderKey] if !ok { - return h + return } for _, tr := range trailers { @@ -101,7 +100,5 @@ func handleTrailers(h map[string][]string) map[string][]string { } } - delete(h, "trailer") - - return h + delete(h, trailerHeaderKey) } diff --git a/service/http/response_test.go b/service/http/response_test.go index ad524567..1f394276 100644 --- a/service/http/response_test.go +++ b/service/http/response_test.go @@ -71,6 +71,11 @@ func TestNewResponse_Stream(t *testing.T) { Context: []byte(`{"headers":{"key":["value"]},"status": 301}`), }) + // r is pointer, so, it might be nil + if r == nil { + t.Fatal("response is nil") + } + r.body = &bytes.Buffer{} r.body.(*bytes.Buffer).WriteString("hello world") @@ -90,6 +95,11 @@ func TestNewResponse_StreamError(t *testing.T) { Context: []byte(`{"headers":{"key":["value"]},"status": 301}`), }) + // r is pointer, so, it might be nil + if r == nil { + t.Fatal("response is nil") + } + r.body = &bytes.Buffer{} r.body.(*bytes.Buffer).WriteString("hello world") @@ -102,7 +112,7 @@ func TestNewResponse_StreamError(t *testing.T) { func TestWrite_HandlesPush(t *testing.T) { r, err := NewResponse(&roadrunner.Payload{ - Context: []byte(`{"headers":{"http2-push":["/test.js"],"content-type":["text/html"]},"status": 200}`), + Context: []byte(`{"headers":{"Http2-Push":["/test.js"],"content-type":["text/html"]},"status": 200}`), }) assert.NoError(t, err) @@ -111,13 +121,13 @@ func TestWrite_HandlesPush(t *testing.T) { w := &testWriter{h: http.Header(make(map[string][]string))} assert.NoError(t, r.Write(w)) - assert.Nil(t, w.h["http2-push"]) + 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}`), + Context: []byte(`{"headers":{"Trailer":["foo, bar", "baz"],"foo":["test"],"bar":["demo"]},"status": 200}`), }) assert.NoError(t, err) @@ -126,9 +136,10 @@ func TestWrite_HandlesTrailers(t *testing.T) { 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.Nil(t, w.h[trailerHeaderKey]) + assert.Nil(t, w.h["foo"]) //nolint:golint,staticcheck + assert.Nil(t, w.h["baz"]) //nolint:golint,staticcheck + assert.Equal(t, "test", w.h.Get("Trailer:foo")) assert.Equal(t, "demo", w.h.Get("Trailer:bar")) } @@ -136,7 +147,7 @@ func TestWrite_HandlesTrailers(t *testing.T) { 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}`), + `{"headers":{"Trailer":["foo\t,bar , baz"],"foo":["a"],"bar":["b"],"baz":["c"]},"status": 200}`), }) assert.NoError(t, err) diff --git a/service/http/ssl_test.go b/service/http/ssl_test.go index c9b4d090..49bba6cb 100644 --- a/service/http/ssl_test.go +++ b/service/http/ssl_test.go @@ -247,7 +247,7 @@ func Test_SSL_Service_Push(t *testing.T) { b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) - assert.Equal(t, "", r.Header.Get("http2-push")) + assert.Equal(t, "", r.Header.Get("Http2-Push")) assert.NoError(t, err) assert.Equal(t, 201, r.StatusCode) |