Skip to content

Commit

Permalink
chore: fix lint errors (#2520)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Oct 16, 2022
1 parent d4c9fd2 commit 05a5de7
Show file tree
Hide file tree
Showing 27 changed files with 117 additions and 148 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go.work.sum

# gitlab ci
.cache
.golangci.yml

# vim auto backup file
*~
Expand Down
64 changes: 0 additions & 64 deletions .golangci.yml

This file was deleted.

5 changes: 3 additions & 2 deletions gateway/internal/headerprocessor_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package internal

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuildHeadersNoValue(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req := httptest.NewRequest("GET", "/", http.NoBody)
req.Header.Add("a", "b")
assert.Nil(t, ProcessHeaders(req.Header))
}

func TestBuildHeadersWithValues(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req := httptest.NewRequest("GET", "/", http.NoBody)
req.Header.Add("grpc-metadata-a", "b")
req.Header.Add("grpc-metadata-b", "b")
assert.ElementsMatch(t, []string{"gateway-A:b", "gateway-B:b"}, ProcessHeaders(req.Header))
Expand Down
38 changes: 33 additions & 5 deletions gateway/internal/requestparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"bytes"
"encoding/json"
"io"
"net/http"

"github.com/fullstorydev/grpcurl"
Expand All @@ -22,16 +23,18 @@ func NewRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.Req
for k, v := range vars {
params[k] = v
}
if len(params) == 0 {
return grpcurl.NewJSONRequestParser(r.Body, resolver), nil
}

if r.ContentLength == 0 {
body, ok := getBody(r)
if !ok {
return buildJsonRequestParser(params, resolver)
}

if len(params) == 0 {
return grpcurl.NewJSONRequestParser(body, resolver), nil
}

m := make(map[string]interface{})
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
if err := json.NewDecoder(body).Decode(&m); err != nil {
return nil, err
}

Expand All @@ -51,3 +54,28 @@ func buildJsonRequestParser(m map[string]interface{}, resolver jsonpb.AnyResolve

return grpcurl.NewJSONRequestParser(&buf, resolver), nil
}

func getBody(r *http.Request) (io.Reader, bool) {
if r.Body == nil {
return nil, false
}

if r.ContentLength == 0 {
return nil, false
}

if r.ContentLength > 0 {
return r.Body, true
}

var buf bytes.Buffer
if _, err := io.Copy(&buf, r.Body); err != nil {
return nil, false
}

if buf.Len() > 0 {
return &buf, true
}

return nil, false
}
9 changes: 5 additions & 4 deletions gateway/internal/requestparser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"net/http"
"net/http/httptest"
"strings"
"testing"
Expand All @@ -10,14 +11,14 @@ import (
)

func TestNewRequestParserNoVar(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req := httptest.NewRequest("GET", "/", http.NoBody)
parser, err := NewRequestParser(req, nil)
assert.Nil(t, err)
assert.NotNil(t, parser)
}

func TestNewRequestParserWithVars(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req := httptest.NewRequest("GET", "/", http.NoBody)
req = pathvar.WithVars(req, map[string]string{"a": "b"})
parser, err := NewRequestParser(req, nil)
assert.Nil(t, err)
Expand Down Expand Up @@ -48,14 +49,14 @@ func TestNewRequestParserWithVarsWithWrongBody(t *testing.T) {
}

func TestNewRequestParserWithForm(t *testing.T) {
req := httptest.NewRequest("GET", "/val?a=b", nil)
req := httptest.NewRequest("GET", "/val?a=b", http.NoBody)
parser, err := NewRequestParser(req, nil)
assert.Nil(t, err)
assert.NotNil(t, parser)
}

func TestNewRequestParserWithBadForm(t *testing.T) {
req := httptest.NewRequest("GET", "/val?a%1=b", nil)
req := httptest.NewRequest("GET", "/val?a%1=b", http.NoBody)
parser, err := NewRequestParser(req, nil)
assert.NotNil(t, err)
assert.Nil(t, parser)
Expand Down
5 changes: 3 additions & 2 deletions gateway/internal/timeout_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"net/http"
"net/http/httptest"
"testing"
"time"
Expand All @@ -9,14 +10,14 @@ import (
)

func TestGetTimeout(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req := httptest.NewRequest("GET", "/", http.NoBody)
req.Header.Set(grpcTimeoutHeader, "1s")
timeout := GetTimeout(req.Header, time.Second*5)
assert.Equal(t, time.Second, timeout)
}

func TestGetTimeoutDefault(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req := httptest.NewRequest("GET", "/", http.NoBody)
timeout := GetTimeout(req.Header, time.Second*5)
assert.Equal(t, time.Second*5, timeout)
}
4 changes: 2 additions & 2 deletions rest/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestEngine_notFoundHandler(t *testing.T) {
defer ts.Close()

client := ts.Client()
err := func(ctx context.Context) error {
err := func(_ context.Context) error {
req, err := http.NewRequest("GET", ts.URL+"/bad", http.NoBody)
assert.Nil(t, err)
res, err := client.Do(req)
Expand All @@ -260,7 +260,7 @@ func TestEngine_notFoundHandlerNotNil(t *testing.T) {
defer ts.Close()

client := ts.Client()
err := func(ctx context.Context) error {
err := func(_ context.Context) error {
req, err := http.NewRequest("GET", ts.URL+"/bad", http.NoBody)
assert.Nil(t, err)
res, err := client.Do(req)
Expand Down
8 changes: 4 additions & 4 deletions rest/handler/authhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestAuthHandlerFailed(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
handler := Authorize("B63F477D-BBA3-4E52-96D3-C0034C27694A", WithUnauthorizedCallback(
func(w http.ResponseWriter, r *http.Request, err error) {
assert.NotNil(t, err)
Expand All @@ -33,7 +33,7 @@ func TestAuthHandlerFailed(t *testing.T) {

func TestAuthHandler(t *testing.T) {
const key = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
token, err := buildToken(key, map[string]interface{}{
"key": "value",
}, 3600)
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestAuthHandlerWithPrevSecret(t *testing.T) {
key = "14F17379-EB8F-411B-8F12-6929002DCA76"
prevKey = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
)
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
token, err := buildToken(key, map[string]interface{}{
"key": "value",
}, 3600)
Expand All @@ -83,7 +83,7 @@ func TestAuthHandlerWithPrevSecret(t *testing.T) {
}

func TestAuthHandler_NilError(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
resp := httptest.NewRecorder()
assert.NotPanics(t, func() {
unauthorized(resp, req, nil, nil)
Expand Down
12 changes: 6 additions & 6 deletions rest/handler/breakerhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestBreakerHandlerAccept(t *testing.T) {
assert.Nil(t, err)
}))

req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
req.Header.Set("X-Test", "test")
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
Expand All @@ -41,7 +41,7 @@ func TestBreakerHandlerFail(t *testing.T) {
w.WriteHeader(http.StatusBadGateway)
}))

req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusBadGateway, resp.Code)
Expand All @@ -55,15 +55,15 @@ func TestBreakerHandler_4XX(t *testing.T) {
}))

for i := 0; i < 1000; i++ {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
}

const tries = 100
var pass int
for i := 0; i < tries; i++ {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
if resp.Code == http.StatusBadRequest {
Expand All @@ -82,14 +82,14 @@ func TestBreakerHandlerReject(t *testing.T) {
}))

for i := 0; i < 1000; i++ {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
}

var drops int
for i := 0; i < 100; i++ {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
if resp.Code == http.StatusServiceUnavailable {
Expand Down
6 changes: 3 additions & 3 deletions rest/handler/cryptionhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func init() {
}

func TestCryptionHandlerGet(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/any", nil)
req := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(respText))
w.Header().Set("X-Test", "test")
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestCryptionHandlerPostBadEncryption(t *testing.T) {
}

func TestCryptionHandlerWriteHeader(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/any", nil)
req := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
Expand All @@ -90,7 +90,7 @@ func TestCryptionHandlerWriteHeader(t *testing.T) {
}

func TestCryptionHandlerFlush(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/any", nil)
req := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(respText))
flusher, ok := w.(http.Flusher)
Expand Down
6 changes: 3 additions & 3 deletions rest/handler/loghandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestLogHandler(t *testing.T) {
}

for _, logHandler := range handlers {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Context().Value(internal.LogContext).(*internal.LogCollector).Append("anything")
w.Header().Set("X-Test", "test")
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestLogHandlerSlow(t *testing.T) {
}

for _, logHandler := range handlers {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(defaultSlowThreshold + time.Millisecond*50)
}))
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestWrapStatusCodeWithColor(t *testing.T) {
func BenchmarkLogHandler(b *testing.B) {
b.ReportAllocs()

req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
handler := LogHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
Expand Down
Loading

0 comments on commit 05a5de7

Please sign in to comment.