Skip to content

Commit

Permalink
fix golangci-lint problems
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Mar 11, 2019
1 parent 7617bfc commit de0b781
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 16 deletions.
2 changes: 2 additions & 0 deletions js/common/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (

// if a fieldName is the key of this map exactly than the value for the given key should be used as
// the name of the field in js
//nolint: gochecknoglobals
var fieldNameExceptions = map[string]string{
"OCSP": "ocsp",
}
Expand Down Expand Up @@ -77,6 +78,7 @@ func FieldName(t reflect.Type, f reflect.StructField) string {

// if a methodName is the key of this map exactly than the value for the given key should be used as
// the name of the method in js
//nolint: gochecknoglobals
var methodNameExceptions = map[string]string{
"JSON": "json",
"HTML": "html",
Expand Down
1 change: 1 addition & 0 deletions js/modules/k6/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestTagURL(t *testing.T) {
`http://localhost/anything/${1+1}/${1+2}/`: {"http://localhost/anything/2/3/", "http://localhost/anything/${}/${}/"},
}
for expr, data := range testdata {
expr, data := expr, data
t.Run("expr="+expr, func(t *testing.T) {
tag, err := httpext.NewURL(data.u, data.n)
require.NoError(t, err)
Expand Down
6 changes: 5 additions & 1 deletion js/modules/k6/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ func (h *HTTP) Request(ctx context.Context, method string, url goja.Value, args
return responseFromHttpext(resp), nil
}

func (h *HTTP) parseRequest(ctx context.Context, method string, reqURL httpext.URL, body interface{}, params goja.Value) (*httpext.ParsedHTTPRequest, error) {
//TODO break this function up
//nolint: gocyclo
func (h *HTTP) parseRequest(
ctx context.Context, method string, reqURL httpext.URL, body interface{}, params goja.Value,
) (*httpext.ParsedHTTPRequest, error) {
rt := common.GetRuntime(ctx)
state := lib.GetState(ctx)
if state == nil {
Expand Down
21 changes: 13 additions & 8 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ func assertRequestMetricsEmitted(t *testing.T, sampleContainers []stats.SampleCo
assert.True(t, seenReceiving, "url %s didn't emit Receiving", url)
}

func newRuntime(t *testing.T) (*testutils.HTTPMultiBin, *lib.State, chan stats.SampleContainer, *goja.Runtime, *context.Context) {
func newRuntime(
t *testing.T,
) (*testutils.HTTPMultiBin, *lib.State, chan stats.SampleContainer, *goja.Runtime, *context.Context) {
tb := testutils.NewHTTPMultiBin(t)

root, err := lib.NewGroup("", nil)
Expand Down Expand Up @@ -456,10 +458,10 @@ func TestRequestAndBatch(t *testing.T) {
}(state.Options.Throw)
state.Options.Throw = null.BoolFrom(false)

tb.Mux.HandleFunc("/no-location-redirect", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tb.Mux.HandleFunc("/no-location-redirect", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(302)
}))
tb.Mux.HandleFunc("/bad-location-redirect", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tb.Mux.HandleFunc("/bad-location-redirect", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Location", "h\t:/") // \n is forbidden
w.WriteHeader(302)
}))
Expand Down Expand Up @@ -517,11 +519,13 @@ func TestRequestAndBatch(t *testing.T) {
expectedScriptError: sr(`GoError: Get HTTPBIN_URL/no-location-redirect: 302 response missing Location header`),
},
{
name: "Bad location redirect",
expectedErrorCode: 0,
expectedErrorMsg: "",
script: `let res = http.request("GET", "HTTPBIN_URL/bad-location-redirect");`,
expectedScriptError: sr("GoError: Get HTTPBIN_URL/bad-location-redirect: failed to parse Location header \"h\\t:/\": parse h\t:/: first path segment in URL cannot contain colon"),
name: "Bad location redirect",
expectedErrorCode: 0,
expectedErrorMsg: "",
script: `let res = http.request("GET", "HTTPBIN_URL/bad-location-redirect");`,
expectedScriptError: sr(
"GoError: Get HTTPBIN_URL/bad-location-redirect:failed to parse Location header" +
" \"h\\t:/\": parse h\t:/: first path segment in URL cannot contain colon"),
},
{
name: "Missing protocol",
Expand All @@ -539,6 +543,7 @@ func TestRequestAndBatch(t *testing.T) {
}

for _, testCase := range testCases {
testCase := testCase
// clear the Samples
stats.GetBufferedSamples(samples)
t.Run(testCase.name, func(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ func (u *VU) RunOnce(ctx context.Context) error {
return err
}

func (u *VU) runFn(ctx context.Context, group *lib.Group, fn goja.Callable, args ...goja.Value) (goja.Value, *lib.State, error) {
func (u *VU) runFn(
ctx context.Context, group *lib.Group, fn goja.Callable, args ...goja.Value,
) (goja.Value, *lib.State, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return goja.Undefined(), nil, err
Expand Down
1 change: 1 addition & 0 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ func TestVUIntegrationGroups(t *testing.T) {

testdata := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range testdata {
r := r
t.Run(name, func(t *testing.T) {
vu, err := r.newVU(make(chan stats.SampleContainer, 100))
if !assert.NoError(t, err) {
Expand Down
9 changes: 7 additions & 2 deletions lib/netext/httpext/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func stdCookiesToHTTPRequestCookies(cookies []*http.Cookie) map[string][]*HTTPRe
}

// MakeRequest makes http request for tor the provided ParsedHTTPRequest
//TODO break this function up
//nolint: gocyclo
func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error) {
state := lib.GetState(ctx)

Expand Down Expand Up @@ -180,7 +182,9 @@ func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error
if l > 0 {
url = via[0].URL
}
state.Logger.WithFields(log.Fields{"url": url.String()}).Warnf("Stopped after %d redirects and returned the redirection; pass { redirects: n } in request params or set global maxRedirects to silence this", l)
state.Logger.WithFields(log.Fields{"url": url.String()}).Warnf(
"Stopped after %d redirects and returned the redirection; pass { redirects: n }"+
" in request params or set global maxRedirects to silence this", l)
}
return http.ErrUseLastResponse
}
Expand All @@ -189,7 +193,8 @@ func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error
},
}

// if digest authentication option is passed, make an initial request to get the authentication params to compute the authorization header
// if digest authentication option is passed, make an initial request
// to get the authentication params to compute the authorization header
if preq.Auth == "digest" {
username := preq.URL.u.User.Username()
password, _ := preq.URL.u.User.Password()
Expand Down
1 change: 1 addition & 0 deletions lib/netext/httpext/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

// ResponseType is used in the request to specify how the response body should be treated
// The conversion and validation methods are auto-generated with https://github.com/alvaroloes/enumer:
//nolint: lll
//go:generate enumer -type=ResponseType -transform=snake -json -text -trimprefix ResponseType -output response_type_gen.go
type ResponseType uint

Expand Down
7 changes: 6 additions & 1 deletion lib/netext/httpext/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ func TestTracerError(t *testing.T) {
req, err := http.NewRequest("GET", srv.URL+"/get", nil)
require.NoError(t, err)

_, err = http.DefaultTransport.RoundTrip(req.WithContext(httptrace.WithClientTrace(context.Background(), tracer.Trace())))
_, err = http.DefaultTransport.RoundTrip(
req.WithContext(
httptrace.WithClientTrace(
context.Background(),
tracer.Trace())))

assert.Error(t, err)

assert.Len(t, tracer.protoErrors, 1)
Expand Down
12 changes: 9 additions & 3 deletions lib/netext/httpext/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ var _ http.RoundTripper = &transport{}

// NewTransport returns a new Transport wrapping around the provide Roundtripper and will send
// samples on the provided channel adding the tags in accordance to the Options provided
func newTransport(roundTripper http.RoundTripper, samplesCh chan<- stats.SampleContainer, options *lib.Options, tags map[string]string) *transport {
func newTransport(
roundTripper http.RoundTripper,
samplesCh chan<- stats.SampleContainer,
options *lib.Options,
tags map[string]string,
) *transport {
return &transport{
roundTripper: roundTripper,
tags: tags,
Expand All @@ -77,7 +82,7 @@ func (t *transport) TLSInfo() netext.TLSInfo {
// RoundTrip is the implementation of http.RoundTripper
func (t *transport) RoundTrip(req *http.Request) (res *http.Response, err error) {
if t.roundTripper == nil {
return nil, errors.New("No roundtrip defined")
return nil, errors.New("no roundtrip defined")
}

tags := map[string]string{}
Expand Down Expand Up @@ -134,7 +139,8 @@ func (t *transport) RoundTrip(req *http.Request) (res *http.Response, err error)
}
}
if t.options.SystemTags["ip"] && trail.ConnRemoteAddr != nil {
if ip, _, err := net.SplitHostPort(trail.ConnRemoteAddr.String()); err == nil {
var ip string
if ip, _, err = net.SplitHostPort(trail.ConnRemoteAddr.String()); err == nil {
tags["ip"] = ip
}
}
Expand Down

0 comments on commit de0b781

Please sign in to comment.