chore: fix lint errors (#2520)

master
Kevin Wan 2 years ago committed by GitHub
parent d4c9fd2aff
commit 05a5de7c6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

1
.gitignore vendored

@ -22,6 +22,7 @@ go.work.sum
# gitlab ci
.cache
.golangci.yml
# vim auto backup file
*~

@ -1,64 +0,0 @@
linters-settings:
errcheck:
check-type-assertions: true
goconst:
min-len: 2
min-occurrences: 3
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
govet:
check-shadowing: true
enable:
- fieldalignment
nolintlint:
require-explanation: true
require-specific: true
staticcheck:
checks:
- all
- '-SA5008'
linters:
disable-all: true
enable:
- bodyclose
- depguard
- dogsled
- dupl
- errcheck
- exportloopref
- exhaustive
- goconst
- gocritic
- gofmt
- goimports
- gomnd
- gocyclo
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nolintlint
- nakedret
- prealloc
- predeclared
- revive
- staticcheck
- stylecheck
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- whitespace
- wsl
run:
issues-exit-code: 1

@ -1,6 +1,7 @@
package internal
import (
"net/http"
"net/http/httptest"
"testing"
@ -8,13 +9,13 @@ import (
)
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))

@ -3,6 +3,7 @@ package internal
import (
"bytes"
"encoding/json"
"io"
"net/http"
"github.com/fullstorydev/grpcurl"
@ -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
}
@ -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
}

@ -1,6 +1,7 @@
package internal
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
@ -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)
@ -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)

@ -1,6 +1,7 @@
package internal
import (
"net/http"
"net/http/httptest"
"testing"
"time"
@ -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)
}

@ -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)
@ -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)

@ -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)
@ -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)
@ -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)
@ -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)

@ -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)
@ -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)
@ -55,7 +55,7 @@ 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)
}
@ -63,7 +63,7 @@ func TestBreakerHandler_4XX(t *testing.T) {
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 {
@ -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 {

@ -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")
@ -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)
}))
@ -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)

@ -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")
@ -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)
}))
@ -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)
}))

@ -32,13 +32,13 @@ func TestMaxConnsHandler(t *testing.T) {
for i := 0; i < conns; i++ {
go func() {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
handler.ServeHTTP(httptest.NewRecorder(), req)
}()
}
waitGroup.Wait()
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.StatusServiceUnavailable, resp.Code)
@ -65,14 +65,14 @@ func TestWithoutMaxConnsHandler(t *testing.T) {
for i := 0; i < conns; i++ {
go func() {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
req.Header.Set(key, value)
handler.ServeHTTP(httptest.NewRecorder(), req)
}()
}
waitGroup.Wait()
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.StatusOK, resp.Code)

@ -16,7 +16,7 @@ func TestMetricHandler(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
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.StatusOK, resp.Code)

@ -15,7 +15,7 @@ func TestPromMetricHandler_Disabled(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
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.StatusOK, resp.Code)
@ -31,7 +31,7 @@ func TestPromMetricHandler_Enabled(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
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.StatusOK, resp.Code)

@ -19,7 +19,7 @@ func TestWithPanic(t *testing.T) {
panic("whatever")
}))
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.StatusInternalServerError, resp.Code)
@ -29,7 +29,7 @@ func TestWithoutPanic(t *testing.T) {
handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
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.StatusOK, resp.Code)

@ -28,7 +28,7 @@ func TestSheddingHandlerAccept(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)
@ -47,7 +47,7 @@ func TestSheddingHandlerFail(t *testing.T) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
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.StatusServiceUnavailable, resp.Code)
@ -63,7 +63,7 @@ func TestSheddingHandlerReject(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
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.StatusServiceUnavailable, resp.Code)
@ -76,7 +76,7 @@ func TestSheddingHandlerNoShedding(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
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.StatusOK, resp.Code)

@ -22,7 +22,7 @@ func TestTimeout(t *testing.T) {
time.Sleep(time.Minute)
}))
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.StatusServiceUnavailable, resp.Code)
@ -34,7 +34,7 @@ func TestWithinTimeout(t *testing.T) {
time.Sleep(time.Millisecond)
}))
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.StatusOK, resp.Code)
@ -48,7 +48,7 @@ func TestWithTimeoutTimedout(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
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.StatusServiceUnavailable, resp.Code)
@ -60,7 +60,7 @@ func TestWithoutTimeout(t *testing.T) {
time.Sleep(100 * time.Millisecond)
}))
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.StatusOK, resp.Code)
@ -72,7 +72,7 @@ func TestTimeoutPanic(t *testing.T) {
panic("foo")
}))
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
resp := httptest.NewRecorder()
assert.Panics(t, func() {
handler.ServeHTTP(resp, req)
@ -85,7 +85,7 @@ func TestTimeoutWebsocket(t *testing.T) {
time.Sleep(time.Millisecond * 10)
}))
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
req.Header.Set(headerUpgrade, valueWebsocket)
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
@ -100,7 +100,7 @@ func TestTimeoutWroteHeaderTwice(t *testing.T) {
w.WriteHeader(http.StatusOK)
}))
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.StatusOK, resp.Code)
@ -112,7 +112,7 @@ func TestTimeoutWriteBadCode(t *testing.T) {
w.WriteHeader(1000)
}))
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
resp := httptest.NewRecorder()
assert.Panics(t, func() {
handler.ServeHTTP(resp, req)
@ -125,7 +125,7 @@ func TestTimeoutClientClosed(t *testing.T) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
ctx, cancel := context.WithCancel(context.Background())
req = req.WithContext(ctx)
cancel()

@ -19,7 +19,7 @@ func TestParseForm(t *testing.T) {
Percent float64 `form:"percent,optional"`
}
r, err := http.NewRequest(http.MethodGet, "/a?name=hello&age=18&percent=3.4", nil)
r, err := http.NewRequest(http.MethodGet, "/a?name=hello&age=18&percent=3.4", http.NoBody)
assert.Nil(t, err)
assert.Nil(t, Parse(r, &v))
assert.Equal(t, "hello", v.Name)
@ -33,7 +33,7 @@ func TestParseForm_Error(t *testing.T) {
Age int `form:"age"`
}
r := httptest.NewRequest(http.MethodGet, "/a?name=hello;", nil)
r := httptest.NewRequest(http.MethodGet, "/a?name=hello;", http.NoBody)
assert.NotNil(t, ParseForm(r, &v))
}
@ -82,7 +82,7 @@ func TestParsePath(t *testing.T) {
Age int `path:"age"`
}
r := httptest.NewRequest(http.MethodGet, "/", nil)
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
r = pathvar.WithVars(r, map[string]string{
"name": "foo",
"age": "18",
@ -99,7 +99,7 @@ func TestParsePath_Error(t *testing.T) {
Age int `path:"age"`
}
r := httptest.NewRequest(http.MethodGet, "/", nil)
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
r = pathvar.WithVars(r, map[string]string{
"name": "foo",
})
@ -138,7 +138,7 @@ func TestParseFormOutOfRange(t *testing.T) {
}
for _, test := range tests {
r, err := http.NewRequest(http.MethodGet, test.url, nil)
r, err := http.NewRequest(http.MethodGet, test.url, http.NoBody)
assert.Nil(t, err)
err = Parse(r, &v)
@ -218,7 +218,7 @@ func TestParseJsonBody(t *testing.T) {
Age int `json:"age,optional"`
}
r := httptest.NewRequest(http.MethodGet, "/", nil)
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
assert.Nil(t, Parse(r, &v))
assert.Equal(t, "", v.Name)
assert.Equal(t, 0, v.Age)
@ -231,7 +231,7 @@ func TestParseRequired(t *testing.T) {
Percent float64 `form:"percent"`
}{}
r, err := http.NewRequest(http.MethodGet, "/a?name=hello", nil)
r, err := http.NewRequest(http.MethodGet, "/a?name=hello", http.NoBody)
assert.Nil(t, err)
assert.NotNil(t, Parse(r, &v))
}
@ -241,7 +241,7 @@ func TestParseOptions(t *testing.T) {
Position int8 `form:"pos,options=1|2"`
}{}
r, err := http.NewRequest(http.MethodGet, "/a?pos=4", nil)
r, err := http.NewRequest(http.MethodGet, "/a?pos=4", http.NoBody)
assert.Nil(t, err)
assert.NotNil(t, Parse(r, &v))
}
@ -258,7 +258,7 @@ func TestParseHeaders(t *testing.T) {
XForwardedFor string `header:"X-Forwarded-For,optional"`
AnonymousStruct
}{}
request, err := http.NewRequest("POST", "/", nil)
request, err := http.NewRequest("POST", "/", http.NoBody)
if err != nil {
t.Fatal(err)
}
@ -287,13 +287,13 @@ func TestParseHeaders_Error(t *testing.T) {
Age int `header:"age"`
}{}
r := httptest.NewRequest("POST", "/", nil)
r := httptest.NewRequest("POST", "/", http.NoBody)
r.Header.Set("name", "foo")
assert.NotNil(t, Parse(r, &v))
}
func BenchmarkParseRaw(b *testing.B) {
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil)
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", http.NoBody)
if err != nil {
b.Fatal(err)
}
@ -318,7 +318,7 @@ func BenchmarkParseRaw(b *testing.B) {
}
func BenchmarkParseAuto(b *testing.B) {
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil)
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", http.NoBody)
if err != nil {
b.Fatal(err)
}

@ -65,7 +65,7 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
for _, method := range methods {
test := test
t.Run(test.name+"-handler", func(t *testing.T) {
r := httptest.NewRequest(method, "http://localhost", nil)
r := httptest.NewRequest(method, "http://localhost", http.NoBody)
r.Header.Set(originHeader, test.reqOrigin)
w := httptest.NewRecorder()
handler := NotAllowedHandler(nil, test.origins...)
@ -78,7 +78,7 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
assert.Equal(t, test.expect, w.Header().Get(allowOrigin))
})
t.Run(test.name+"-handler-custom", func(t *testing.T) {
r := httptest.NewRequest(method, "http://localhost", nil)
r := httptest.NewRequest(method, "http://localhost", http.NoBody)
r.Header.Set(originHeader, test.reqOrigin)
w := httptest.NewRecorder()
handler := NotAllowedHandler(func(w http.ResponseWriter) {
@ -100,7 +100,7 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
for _, method := range methods {
test := test
t.Run(test.name+"-middleware", func(t *testing.T) {
r := httptest.NewRequest(method, "http://localhost", nil)
r := httptest.NewRequest(method, "http://localhost", http.NoBody)
r.Header.Set(originHeader, test.reqOrigin)
w := httptest.NewRecorder()
handler := Middleware(nil, test.origins...)(func(w http.ResponseWriter, r *http.Request) {
@ -115,7 +115,7 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
assert.Equal(t, test.expect, w.Header().Get(allowOrigin))
})
t.Run(test.name+"-middleware-custom", func(t *testing.T) {
r := httptest.NewRequest(method, "http://localhost", nil)
r := httptest.NewRequest(method, "http://localhost", http.NoBody)
r.Header.Set(originHeader, test.reqOrigin)
w := httptest.NewRecorder()
handler := Middleware(func(header http.Header) {

@ -14,7 +14,7 @@ func TestParseHeaders(t *testing.T) {
Baz int `header:"baz"`
Qux bool `header:"qux,default=true"`
}
r := httptest.NewRequest(http.MethodGet, "/any", nil)
r := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
r.Header.Set("foo", "bar")
r.Header.Set("baz", "1")
assert.Nil(t, ParseHeaders(r.Header, &val))
@ -29,7 +29,7 @@ func TestParseHeadersMulti(t *testing.T) {
Baz int `header:"baz"`
Qux bool `header:"qux,default=true"`
}
r := httptest.NewRequest(http.MethodGet, "/any", nil)
r := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
r.Header.Set("foo", "bar")
r.Header.Add("foo", "bar1")
r.Header.Set("baz", "1")
@ -43,7 +43,7 @@ func TestParseHeadersArrayInt(t *testing.T) {
var val struct {
Foo []int `header:"foo"`
}
r := httptest.NewRequest(http.MethodGet, "/any", nil)
r := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
r.Header.Set("foo", "1")
r.Header.Add("foo", "2")
assert.Nil(t, ParseHeaders(r.Header, &val))

@ -13,7 +13,7 @@ import (
func TestInfo(t *testing.T) {
collector := new(LogCollector)
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
req = req.WithContext(context.WithValue(req.Context(), LogContext, collector))
Info(req, "first")
Infof(req, "second %s", "third")
@ -35,7 +35,7 @@ func TestError(t *testing.T) {
logx.SetWriter(o)
}()
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
Error(req, "first")
Errorf(req, "second %s", "third")
val := buf.String()

@ -11,7 +11,7 @@ import (
)
func TestHeaderOnceResponseWriter_Flush(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cw := NewHeaderOnceResponseWriter(w)
cw.Header().Set("X-Test", "test")

@ -9,7 +9,7 @@ import (
)
func TestWithCodeResponseWriter(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cw := &WithCodeResponseWriter{Writer: w}

@ -255,7 +255,7 @@ func TestWithPrefix(t *testing.T) {
},
}
WithPrefix("/api")(&fr)
var vals []string
vals := make([]string, 0, len(fr.routes))
for _, r := range fr.routes {
vals = append(vals, r.Path)
}
@ -510,7 +510,7 @@ func TestServer_WithChain(t *testing.T) {
)
rt := router.NewRouter()
assert.Nil(t, server.ngin.bindRoutes(rt))
req, err := http.NewRequest(http.MethodGet, "/", nil)
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
assert.Nil(t, err)
rt.ServeHTTP(httptest.NewRecorder(), req)
assert.Equal(t, int32(5), atomic.LoadInt32(&called))
@ -531,7 +531,7 @@ func TestServer_WithCors(t *testing.T) {
Router: r,
middleware: cors.Middleware(nil, "*"),
}
req := httptest.NewRequest(http.MethodOptions, "/", nil)
req := httptest.NewRequest(http.MethodOptions, "/", http.NoBody)
cr.ServeHTTP(httptest.NewRecorder(), req)
assert.Equal(t, int32(0), atomic.LoadInt32(&called))
}

@ -31,7 +31,7 @@ func TestTokenParser(t *testing.T) {
}
for _, pair := range keys {
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)
@ -50,7 +50,7 @@ func TestTokenParser_Expired(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)

@ -11,12 +11,13 @@ type directBuilder struct{}
func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (
resolver.Resolver, error) {
var addrs []resolver.Address
endpoints := strings.FieldsFunc(targets.GetEndpoints(target), func(r rune) bool {
return r == EndpointSepChar
})
endpoints = subset(endpoints, subsetSize)
addrs := make([]resolver.Address, 0, len(endpoints))
for _, val := range subset(endpoints, subsetSize) {
for _, val := range endpoints {
addrs = append(addrs, resolver.Address{
Addr: val,
})

@ -115,7 +115,7 @@ func (h *EventHandler) Update(endpoints *v1.Endpoints) {
}
func (h *EventHandler) notify() {
var targets []string
targets := make([]string, 0, len(h.endpoints))
for k := range h.endpoints {
targets = append(targets, k)

Loading…
Cancel
Save