Skip to content

Commit 13beba8

Browse files
authored
Merge pull request #67 from maxatome/v1
Use golangci-lint + go.mod
2 parents a8ac0a5 + 2da24c2 commit 13beba8

File tree

8 files changed

+113
-80
lines changed

8 files changed

+113
-80
lines changed

.travis.yml

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
language: go
2+
go_import_path: github.com/jarcoal/httpmock
23

3-
go:
4-
- "1.6"
5-
- "1.7"
6-
- "1.8"
7-
- "1.9"
8-
- "1.10"
9-
- "1.11"
10-
- "1.12"
4+
sudo: false
5+
6+
matrix:
7+
include:
8+
- go: 1.7.x
9+
- go: 1.8.x
10+
- go: 1.9.x
11+
- go: 1.10.x
12+
- go: 1.11.x
13+
- go: 1.12.x
14+
env:
15+
- USE_LINTER=1
16+
install:
17+
- >
18+
version=1.15.0; name=golangci-lint-$version-linux-amd64;
19+
wget -q -O - https://github.com/golangci/golangci-lint/releases/download/v$version/$name.tar.gz |
20+
tar -zxvf - -C $GOPATH/bin &&
21+
mv $GOPATH/bin/$name/golangci-lint $GOPATH/bin
22+
after_success:
23+
- go get github.com/mattn/goveralls
24+
- goveralls -coverprofile=coverage.out -service=travis-ci
25+
- go: master
26+
allow_failures:
27+
- go: master
28+
fast_finish: true
29+
30+
script:
31+
- export GORACE="halt_on_error=1"
32+
- go test -race -covermode=atomic -coverprofile=coverage.out ./...
33+
- >
34+
if [ "$USE_LINTER" = 1 ]; then
35+
golangci-lint run -E gofmt -E golint -E maligned -E prealloc -E unconvert ./...;
36+
fi
1137
1238
notifications:
1339
email: false

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
HTTPmock provides tools for mocking HTTP responses.
2+
Package httpmock provides tools for mocking HTTP responses.
33
44
Simple Example:
55
func TestFetchArticles(t *testing.T) {

env.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
var envVarName = "GONOMOCKS"
88

9+
// Disabled allows to test whether httpmock is enabled or not. It
10+
// depends on GONOMOCKS environment variable.
911
func Disabled() bool {
1012
return os.Getenv(envVarName) != ""
1113
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/jarcoal/httpmock
2+
3+
go 1.7

response.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func NewBytesResponder(status int, body []byte) Responder {
6363

6464
// NewJsonResponse creates an *http.Response with a body that is a json encoded representation of
6565
// the given interface{}. Also accepts an http status code.
66-
func NewJsonResponse(status int, body interface{}) (*http.Response, error) {
66+
func NewJsonResponse(status int, body interface{}) (*http.Response, error) { // nolint: golint
6767
encoded, err := json.Marshal(body)
6868
if err != nil {
6969
return nil, err
@@ -75,7 +75,7 @@ func NewJsonResponse(status int, body interface{}) (*http.Response, error) {
7575

7676
// NewJsonResponder creates a Responder from a given body (as an interface{} that is encoded to
7777
// json) and status code.
78-
func NewJsonResponder(status int, body interface{}) (Responder, error) {
78+
func NewJsonResponder(status int, body interface{}) (Responder, error) { // nolint: golint
7979
resp, err := NewJsonResponse(status, body)
8080
if err != nil {
8181
return nil, err
@@ -93,7 +93,7 @@ func NewJsonResponder(status int, body interface{}) (Responder, error) {
9393
// "/test/path",
9494
// NewJSONResponderOrPanic(200, &MyBody),
9595
// )
96-
func NewJsonResponderOrPanic(status int, body interface{}) Responder {
96+
func NewJsonResponderOrPanic(status int, body interface{}) Responder { // nolint: golint
9797
responder, err := NewJsonResponder(status, body)
9898
if err != nil {
9999
panic(err)
@@ -103,7 +103,7 @@ func NewJsonResponderOrPanic(status int, body interface{}) Responder {
103103

104104
// NewXmlResponse creates an *http.Response with a body that is an xml encoded representation
105105
// of the given interface{}. Also accepts an http status code.
106-
func NewXmlResponse(status int, body interface{}) (*http.Response, error) {
106+
func NewXmlResponse(status int, body interface{}) (*http.Response, error) { // nolint: golint
107107
encoded, err := xml.Marshal(body)
108108
if err != nil {
109109
return nil, err
@@ -115,7 +115,7 @@ func NewXmlResponse(status int, body interface{}) (*http.Response, error) {
115115

116116
// NewXmlResponder creates a Responder from a given body (as an interface{} that is encoded to xml)
117117
// and status code.
118-
func NewXmlResponder(status int, body interface{}) (Responder, error) {
118+
func NewXmlResponder(status int, body interface{}) (Responder, error) { // nolint: golint
119119
resp, err := NewXmlResponse(status, body)
120120
if err != nil {
121121
return nil, err
@@ -133,7 +133,7 @@ func NewXmlResponder(status int, body interface{}) (Responder, error) {
133133
// "/test/path",
134134
// NewXmlResponderOrPanic(200, &MyBody),
135135
// )
136-
func NewXmlResponderOrPanic(status int, body interface{}) Responder {
136+
func NewXmlResponderOrPanic(status int, body interface{}) Responder { // nolint: golint
137137
responder, err := NewXmlResponder(status, body)
138138
if err != nil {
139139
panic(err)
@@ -160,12 +160,12 @@ type dummyReadCloser struct {
160160
func (d *dummyReadCloser) Read(p []byte) (n int, err error) {
161161
n, err = d.body.Read(p)
162162
if err == io.EOF {
163-
d.body.Seek(0, 0)
163+
d.body.Seek(0, 0) // nolint: errcheck
164164
}
165165
return n, err
166166
}
167167

168168
func (d *dummyReadCloser) Close() error {
169-
d.body.Seek(0, 0)
169+
d.body.Seek(0, 0) // nolint: errcheck
170170
return nil
171171
}

response_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func TestResponderFromResponse(t *testing.T) {
1414
responder := ResponderFromResponse(NewStringResponse(200, "hello world"))
1515

16-
req, err := http.NewRequest(http.MethodGet, testUrl, nil)
16+
req, err := http.NewRequest(http.MethodGet, testURL, nil)
1717
if err != nil {
1818
t.Fatal("Error creating request")
1919
}
@@ -22,8 +22,8 @@ func TestResponderFromResponse(t *testing.T) {
2222
t.Error("Error should be nil")
2323
}
2424

25-
testUrlWithQuery := testUrl + "?a=1"
26-
req, err = http.NewRequest(http.MethodGet, testUrlWithQuery, nil)
25+
testURLWithQuery := testURL + "?a=1"
26+
req, err = http.NewRequest(http.MethodGet, testURLWithQuery, nil)
2727
if err != nil {
2828
t.Fatal("Error creating request")
2929
}
@@ -38,11 +38,11 @@ func TestResponderFromResponse(t *testing.T) {
3838

3939
// Request should be non-nil and different for each response
4040
if response1.Request != nil && response2.Request != nil {
41-
if response1.Request.URL.String() != testUrl {
42-
t.Errorf("Expected request url %s, got: %s", testUrl, response1.Request.URL.String())
41+
if response1.Request.URL.String() != testURL {
42+
t.Errorf("Expected request url %s, got: %s", testURL, response1.Request.URL.String())
4343
}
44-
if response2.Request.URL.String() != testUrlWithQuery {
45-
t.Errorf("Expected request url %s, got: %s", testUrlWithQuery, response2.Request.URL.String())
44+
if response2.Request.URL.String() != testURLWithQuery {
45+
t.Errorf("Expected request url %s, got: %s", testURLWithQuery, response2.Request.URL.String())
4646
}
4747
} else {
4848
t.Error("response.Request should not be nil")
@@ -151,7 +151,7 @@ func TestNewXmlResponse(t *testing.T) {
151151

152152
func TestNewErrorResponder(t *testing.T) {
153153
responder := NewErrorResponder(errors.New("oh no"))
154-
req, err := http.NewRequest(http.MethodGet, testUrl, nil)
154+
req, err := http.NewRequest(http.MethodGet, testURL, nil)
155155
if err != nil {
156156
t.Fatal("Error creating request")
157157
}

transport.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
"sync"
1212
)
1313

14-
// Responders are callbacks that receive and http request and return a mocked response.
14+
// Responder is a callback that receives and http request and returns
15+
// a mocked response.
1516
type Responder func(*http.Request) (*http.Response, error)
1617

1718
// NoResponderFound is returned when no responders are found for a given HTTP method and URL.
18-
var NoResponderFound = errors.New("no responder found")
19+
var NoResponderFound = errors.New("no responder found") // nolint: golint
1920

2021
// ConnectionFailure is a responder that returns a connection failure. This is the default
2122
// responder, and is called when no other matching responder is found.
@@ -137,7 +138,8 @@ func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
137138
}
138139

139140
func runCancelable(responder Responder, req *http.Request) (*http.Response, error) {
140-
if req.Cancel == nil {
141+
// TODO: replace req.Cancel by ctx
142+
if req.Cancel == nil { // nolint: staticcheck
141143
return responder(req)
142144
}
143145

@@ -154,7 +156,8 @@ func runCancelable(responder Responder, req *http.Request) (*http.Response, erro
154156

155157
go func() {
156158
select {
157-
case <-req.Cancel:
159+
// TODO: req.Cancel replace by ctx
160+
case <-req.Cancel: // nolint: staticcheck
158161
resultch <- result{
159162
response: nil,
160163
err: errors.New("request canceled"),
@@ -189,10 +192,10 @@ func runCancelable(responder Responder, req *http.Request) (*http.Response, erro
189192
return r.response, r.err
190193
}
191194

192-
// do nothing with timeout
195+
// CancelRequest does nothing with timeout.
193196
func (m *MockTransport) CancelRequest(req *http.Request) {}
194197

195-
// responderForKey returns a responder for a given key
198+
// responderForKey returns a responder for a given key.
196199
func (m *MockTransport) responderForKey(key string) Responder {
197200
m.mu.RLock()
198201
defer m.mu.RUnlock()
@@ -266,13 +269,11 @@ func sortedQuery(m url.Values) string {
266269
sort.Strings(keys)
267270

268271
var b bytes.Buffer
269-
var values []string
272+
var values []string // nolint: prealloc
270273

271274
for _, k := range keys {
272275
// Do not alter the passed url.Values
273-
for _, v := range m[k] {
274-
values = append(values, v)
275-
}
276+
values = append(values, m[k]...)
276277
sort.Strings(values)
277278

278279
k = url.QueryEscape(k)

0 commit comments

Comments
 (0)