Skip to content

Commit 834a2b6

Browse files
Merge pull request #44 from suzuki-shunsuke/refactor/add-linters
refactor: add linters
2 parents e2ed285 + fa2b7cf commit 834a2b6

10 files changed

+179
-157
lines changed

.golangci.yml

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
---
22
linters:
3-
enable:
4-
- deadcode
5-
- goconst
6-
- gofmt
7-
- goimports
8-
- golint
9-
- gosimple
10-
- govet
11-
- interfacer
12-
- maligned
13-
- misspell
14-
- nakedret
15-
- staticcheck
16-
- structcheck
17-
- unconvert
18-
- unused
19-
- varcheck
20-
disable-all: true
3+
enable-all: true
4+
disable:
5+
- wsl
6+
- goerr113
7+
run:
8+
skip-files:
9+
- flute/mock_round_tripper_test.go

flute/matcher.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func isMatchService(req *http.Request, service *Service) bool {
1818

1919
// isMatch returns whether the request matches with the matcher.
2020
// If the matcher has multiple conditions, IsMatch returns true if the request meets all conditions.
21-
func isMatch(req *http.Request, matcher *Matcher) (bool, error) {
21+
func isMatch(req *http.Request, matcher *Matcher) (bool, error) { //nolint:gocognit
2222
if matcher == nil {
2323
// SPEC if the matcher is nil, the route matches the request.
2424
return true, nil
@@ -52,9 +52,8 @@ func isMatch(req *http.Request, matcher *Matcher) (bool, error) {
5252
}
5353
}
5454
if matcher.PartOfHeader != nil {
55-
f, err := isMatchPartOfHeader(req, matcher)
56-
if err != nil || !f {
57-
return f, err
55+
if !isMatchPartOfHeader(req, matcher) {
56+
return false, nil
5857
}
5958
}
6059
if matcher.Header != nil {
@@ -63,9 +62,8 @@ func isMatch(req *http.Request, matcher *Matcher) (bool, error) {
6362
}
6463
}
6564
if matcher.PartOfQuery != nil {
66-
f, err := isMatchPartOfQuery(req, matcher)
67-
if err != nil || !f {
68-
return f, err
65+
if !isMatchPartOfQuery(req, matcher) {
66+
return false, nil
6967
}
7068
}
7169
if matcher.Query != nil {
@@ -82,35 +80,35 @@ func isMatch(req *http.Request, matcher *Matcher) (bool, error) {
8280
return true, nil
8381
}
8482

85-
func isMatchPartOfHeader(req *http.Request, matcher *Matcher) (bool, error) {
83+
func isMatchPartOfHeader(req *http.Request, matcher *Matcher) bool {
8684
for k, v := range matcher.PartOfHeader {
8785
a, ok := req.Header[k]
8886
if !ok {
89-
return false, nil
87+
return false
9088
}
9189
if v != nil {
9290
if !reflect.DeepEqual(a, v) {
93-
return false, nil
91+
return false
9492
}
9593
}
9694
}
97-
return true, nil
95+
return true
9896
}
9997

100-
func isMatchPartOfQuery(req *http.Request, matcher *Matcher) (bool, error) {
98+
func isMatchPartOfQuery(req *http.Request, matcher *Matcher) bool {
10199
query := req.URL.Query()
102100
for k, v := range matcher.PartOfQuery {
103101
a, ok := query[k]
104102
if !ok {
105-
return false, nil
103+
return false
106104
}
107105
if v != nil {
108106
if !reflect.DeepEqual(a, v) {
109-
return false, nil
107+
return false
110108
}
111109
}
112110
}
113-
return true, nil
111+
return true
114112
}
115113

116114
func isMatchBodyString(req *http.Request, matcher *Matcher) (bool, error) {

flute/matcher_test.go renamed to flute/matcher_internal_test.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func Test_isMatchService(t *testing.T) {
2828
}
2929

3030
for _, d := range data {
31+
d := d
3132
t.Run(d.title, func(t *testing.T) {
3233
b := isMatchService(&http.Request{
3334
URL: &url.URL{
@@ -46,7 +47,7 @@ func Test_isMatchService(t *testing.T) {
4647
}
4748
}
4849

49-
func Test_isMatch(t *testing.T) {
50+
func Test_isMatch(t *testing.T) { //nolint:funlen
5051
data := []struct {
5152
title string
5253
req *http.Request
@@ -174,6 +175,7 @@ func Test_isMatch(t *testing.T) {
174175
}
175176

176177
for _, d := range data {
178+
d := d
177179
t.Run(d.title, func(t *testing.T) {
178180
b, err := isMatch(d.req, d.matcher)
179181
if d.isErr {
@@ -195,7 +197,6 @@ func Test_isMatchPartOfQuery(t *testing.T) {
195197
title string
196198
req *http.Request
197199
matcher *Matcher
198-
isErr bool
199200
exp bool
200201
}{
201202
{
@@ -239,13 +240,9 @@ func Test_isMatchPartOfQuery(t *testing.T) {
239240
}
240241

241242
for _, d := range data {
243+
d := d
242244
t.Run(d.title, func(t *testing.T) {
243-
b, err := isMatchPartOfQuery(d.req, d.matcher)
244-
if d.isErr {
245-
require.NotNil(t, err)
246-
return
247-
}
248-
require.Nil(t, err)
245+
b := isMatchPartOfQuery(d.req, d.matcher)
249246
if d.exp {
250247
require.True(t, b)
251248
return
@@ -260,7 +257,6 @@ func Test_isMatchPartOfHeader(t *testing.T) {
260257
title string
261258
req *http.Request
262259
matcher *Matcher
263-
isErr bool
264260
exp bool
265261
}{
266262
{
@@ -304,13 +300,9 @@ func Test_isMatchPartOfHeader(t *testing.T) {
304300
}
305301

306302
for _, d := range data {
303+
d := d
307304
t.Run(d.title, func(t *testing.T) {
308-
b, err := isMatchPartOfHeader(d.req, d.matcher)
309-
if d.isErr {
310-
require.NotNil(t, err)
311-
return
312-
}
313-
require.Nil(t, err)
305+
b := isMatchPartOfHeader(d.req, d.matcher)
314306
if d.exp {
315307
require.True(t, b)
316308
return
@@ -320,7 +312,7 @@ func Test_isMatchPartOfHeader(t *testing.T) {
320312
}
321313
}
322314

323-
func Test_isMatchBodyString(t *testing.T) {
315+
func Test_isMatchBodyString(t *testing.T) { //nolint:dupl
324316
data := []struct {
325317
title string
326318
req *http.Request
@@ -354,6 +346,7 @@ func Test_isMatchBodyString(t *testing.T) {
354346
}
355347

356348
for _, d := range data {
349+
d := d
357350
t.Run(d.title, func(t *testing.T) {
358351
b, err := isMatchBodyString(d.req, d.matcher)
359352
if d.isErr {
@@ -370,7 +363,7 @@ func Test_isMatchBodyString(t *testing.T) {
370363
}
371364
}
372365

373-
func Test_isMatchBodyJSONString(t *testing.T) {
366+
func Test_isMatchBodyJSONString(t *testing.T) { //nolint:dupl
374367
data := []struct {
375368
title string
376369
req *http.Request
@@ -404,6 +397,7 @@ func Test_isMatchBodyJSONString(t *testing.T) {
404397
}
405398

406399
for _, d := range data {
400+
d := d
407401
t.Run(d.title, func(t *testing.T) {
408402
b, err := isMatchBodyJSONString(d.req, d.matcher)
409403
if d.isErr {
@@ -454,6 +448,7 @@ func Test_isMatchBodyJSON(t *testing.T) {
454448
}
455449

456450
for _, d := range data {
451+
d := d
457452
t.Run(d.title, func(t *testing.T) {
458453
b, err := isMatchBodyJSON(d.req, d.matcher)
459454
if d.isErr {

flute/response.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func createHTTPResponse(req *http.Request, resp *Response) (*http.Response, erro
2020
if err != nil {
2121
return &http.Response{
2222
Request: req,
23-
StatusCode: 500,
23+
StatusCode: http.StatusInternalServerError,
2424
}, err
2525
}
2626
body = ioutil.NopCloser(strings.NewReader(string(b)))

flute/response_test.go renamed to flute/response_internal_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (*invalidMarshaler) MarshalJSON() ([]byte, error) {
1818
return nil, errors.New("failed to marshal JSON")
1919
}
2020

21-
func Test_createHTTPResponse(t *testing.T) {
21+
func Test_createHTTPResponse(t *testing.T) { //nolint:funlen
2222
data := []struct {
2323
title string
2424
req *http.Request
@@ -92,8 +92,16 @@ func Test_createHTTPResponse(t *testing.T) {
9292
}
9393

9494
for _, d := range data {
95+
d := d
9596
t.Run(d.title, func(t *testing.T) {
9697
resp, err := createHTTPResponse(d.req, d.resp)
98+
var b []byte
99+
if resp != nil && resp.Body != nil {
100+
var err error
101+
b, err = ioutil.ReadAll(resp.Body)
102+
resp.Body.Close()
103+
require.Nil(t, err)
104+
}
97105
if d.isErr {
98106
require.NotNil(t, err)
99107
return
@@ -108,8 +116,6 @@ func Test_createHTTPResponse(t *testing.T) {
108116
require.NotNil(t, resp.Body)
109117

110118
require.Equal(t, d.exp.StatusCode, resp.StatusCode)
111-
b, err := ioutil.ReadAll(resp.Body)
112-
require.Nil(t, err)
113119
require.Equal(t, d.body, string(b))
114120
})
115121
}

flute/tester_test.go renamed to flute/tester_internal_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/stretchr/testify/require"
1111
)
1212

13-
func Test_testRequest(t *testing.T) {
13+
func Test_testRequest(t *testing.T) { //nolint:funlen
1414
data := []struct {
1515
title string
1616
req *http.Request
@@ -107,6 +107,7 @@ func Test_testRequest(t *testing.T) {
107107
}
108108

109109
for _, d := range data {
110+
d := d
110111
t.Run(d.title, func(t *testing.T) {
111112
testRequest(t, d.req, d.service, d.route)
112113
})
@@ -133,6 +134,7 @@ request name: create a user`,
133134
}
134135

135136
for _, d := range data {
137+
d := d
136138
t.Run(d.title, func(t *testing.T) {
137139
require.Equal(t, d.exp, makeMsg(d.msg, d.srv, d.reqName))
138140
})
@@ -171,6 +173,7 @@ func Test_testBodyString(t *testing.T) {
171173
}
172174

173175
for _, d := range data {
176+
d := d
174177
t.Run(d.title, func(t *testing.T) {
175178
testBodyString(t, d.req, d.service, d.route)
176179
})
@@ -201,6 +204,7 @@ func Test_testPath(t *testing.T) {
201204
}
202205

203206
for _, d := range data {
207+
d := d
204208
t.Run(d.title, func(t *testing.T) {
205209
testPath(t, d.req, d.service, d.route)
206210
})
@@ -229,6 +233,7 @@ func Test_testMethod(t *testing.T) {
229233
}
230234

231235
for _, d := range data {
236+
d := d
232237
t.Run(d.title, func(t *testing.T) {
233238
testMethod(t, d.req, d.service, d.route)
234239
})
@@ -269,6 +274,7 @@ func Test_testBodyJSON(t *testing.T) {
269274
}
270275

271276
for _, d := range data {
277+
d := d
272278
t.Run(d.title, func(t *testing.T) {
273279
testBodyJSON(t, d.req, d.service, d.route)
274280
})
@@ -307,6 +313,7 @@ func Test_testBodyJSONString(t *testing.T) {
307313
}
308314

309315
for _, d := range data {
316+
d := d
310317
t.Run(d.title, func(t *testing.T) {
311318
testBodyJSONString(t, d.req, d.service, d.route)
312319
})
@@ -341,6 +348,7 @@ func Test_testPartOfHeader(t *testing.T) {
341348
}
342349

343350
for _, d := range data {
351+
d := d
344352
t.Run(d.title, func(t *testing.T) {
345353
testPartOfHeader(t, d.req, d.service, d.route)
346354
})
@@ -373,6 +381,7 @@ func Test_testPartOfQuery(t *testing.T) {
373381
}
374382

375383
for _, d := range data {
384+
d := d
376385
t.Run(d.title, func(t *testing.T) {
377386
testPartOfQuery(t, d.req, d.service, d.route)
378387
})

flute/transport.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ body:
2828
// RoundTrip traverses the matched route and run the test and returns response.
2929
func (transport *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
3030
for _, service := range transport.Services {
31+
service := service
3132
if !isMatchService(req, &service) {
3233
continue
3334
}
3435
for _, route := range service.Routes {
36+
route := route
3537
b, err := isMatch(req, route.Matcher)
3638
if err != nil {
3739
if transport.T != nil {
@@ -99,7 +101,7 @@ func noMatchedRouteRoundTrip(t *testing.T, req *http.Request) (*http.Response, e
99101
}
100102
return &http.Response{
101103
Request: req,
102-
StatusCode: 404,
104+
StatusCode: http.StatusNotFound,
103105
Body: ioutil.NopCloser(strings.NewReader(`{"message": "no route matches the request"}`)),
104106
}, nil
105107
}

0 commit comments

Comments
 (0)