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 | |
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')
-rw-r--r-- | service/container_test.go | 2 | ||||
-rw-r--r-- | service/gzip/service.go | 2 | ||||
-rw-r--r-- | service/gzip/service_test.go | 53 | ||||
-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 |
7 files changed, 65 insertions, 54 deletions
diff --git a/service/container_test.go b/service/container_test.go index ad4c1e64..9860777f 100644 --- a/service/container_test.go +++ b/service/container_test.go @@ -457,7 +457,7 @@ func TestContainer_NoInit(t *testing.T) { } type testInitD struct { - c *testInitC + c *testInitC //nolint:golint,unused,structcheck } type DCfg struct { diff --git a/service/gzip/service.go b/service/gzip/service.go index 86069c2f..bb6bcfcd 100644 --- a/service/gzip/service.go +++ b/service/gzip/service.go @@ -16,7 +16,7 @@ type Service struct { func (s *Service) Init(cfg *Config, r *rrhttp.Service) (bool, error) { s.cfg = cfg - if s.cfg.Enable == false { + if !s.cfg.Enable { return false, nil } diff --git a/service/gzip/service_test.go b/service/gzip/service_test.go index 6a2ee382..9801860f 100644 --- a/service/gzip/service_test.go +++ b/service/gzip/service_test.go @@ -7,16 +7,13 @@ import ( "github.com/spiral/roadrunner/service" rrhttp "github.com/spiral/roadrunner/service/http" "github.com/stretchr/testify/assert" - "io/ioutil" - "net/http" - "os" "testing" ) type testCfg struct { gzip string httpCfg string - static string + //static string target string } @@ -34,24 +31,24 @@ func (cfg *testCfg) Unmarshal(out interface{}) error { return json.Unmarshal([]byte(cfg.target), out) } -func get(url string) (string, *http.Response, error) { - r, err := http.Get(url) - if err != nil { - return "", nil, err - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - return "", nil, err - } - - err = r.Body.Close() - if err != nil { - return "", nil, err - } - - return string(b), r, err -} +//func get(url string) (string, *http.Response, error) { +// r, err := http.Get(url) +// if err != nil { +// return "", nil, err +// } +// +// b, err := ioutil.ReadAll(r.Body) +// if err != nil { +// return "", nil, err +// } +// +// err = r.Body.Close() +// if err != nil { +// return "", nil, err +// } +// +// return string(b), r, err +//} func Test_Disabled(t *testing.T) { logger, _ := test.NewNullLogger() @@ -115,9 +112,9 @@ func Test_Disabled(t *testing.T) { // //header should contain content-encoding:gzip because content-length > gziphandler.DefaultMinSize // } -func tmpDir() string { - p := os.TempDir() - r, _ := json.Marshal(p) - - return string(r) -} +//func tmpDir() string { +// p := os.TempDir() +// r, _ := json.Marshal(p) +// +// return string(r) +//} 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) |