Skip to content

Commit

Permalink
Merge pull request #67 from maxatome/v1
Browse files Browse the repository at this point in the history
 Use golangci-lint + go.mod
  • Loading branch information
maxatome authored Mar 16, 2019
2 parents a8ac0a5 + 2da24c2 commit 13beba8
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 80 deletions.
42 changes: 34 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
language: go
go_import_path: github.com/jarcoal/httpmock

go:
- "1.6"
- "1.7"
- "1.8"
- "1.9"
- "1.10"
- "1.11"
- "1.12"
sudo: false

matrix:
include:
- go: 1.7.x
- go: 1.8.x
- go: 1.9.x
- go: 1.10.x
- go: 1.11.x
- go: 1.12.x
env:
- USE_LINTER=1
install:
- >
version=1.15.0; name=golangci-lint-$version-linux-amd64;
wget -q -O - https://github.com/golangci/golangci-lint/releases/download/v$version/$name.tar.gz |
tar -zxvf - -C $GOPATH/bin &&
mv $GOPATH/bin/$name/golangci-lint $GOPATH/bin
after_success:
- go get github.com/mattn/goveralls
- goveralls -coverprofile=coverage.out -service=travis-ci
- go: master
allow_failures:
- go: master
fast_finish: true

script:
- export GORACE="halt_on_error=1"
- go test -race -covermode=atomic -coverprofile=coverage.out ./...
- >
if [ "$USE_LINTER" = 1 ]; then
golangci-lint run -E gofmt -E golint -E maligned -E prealloc -E unconvert ./...;
fi
notifications:
email: false
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
HTTPmock provides tools for mocking HTTP responses.
Package httpmock provides tools for mocking HTTP responses.
Simple Example:
func TestFetchArticles(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

var envVarName = "GONOMOCKS"

// Disabled allows to test whether httpmock is enabled or not. It
// depends on GONOMOCKS environment variable.
func Disabled() bool {
return os.Getenv(envVarName) != ""
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/jarcoal/httpmock

go 1.7
16 changes: 8 additions & 8 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewBytesResponder(status int, body []byte) Responder {

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

// NewJsonResponder creates a Responder from a given body (as an interface{} that is encoded to
// json) and status code.
func NewJsonResponder(status int, body interface{}) (Responder, error) {
func NewJsonResponder(status int, body interface{}) (Responder, error) { // nolint: golint
resp, err := NewJsonResponse(status, body)
if err != nil {
return nil, err
Expand All @@ -93,7 +93,7 @@ func NewJsonResponder(status int, body interface{}) (Responder, error) {
// "/test/path",
// NewJSONResponderOrPanic(200, &MyBody),
// )
func NewJsonResponderOrPanic(status int, body interface{}) Responder {
func NewJsonResponderOrPanic(status int, body interface{}) Responder { // nolint: golint
responder, err := NewJsonResponder(status, body)
if err != nil {
panic(err)
Expand All @@ -103,7 +103,7 @@ func NewJsonResponderOrPanic(status int, body interface{}) Responder {

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

// NewXmlResponder creates a Responder from a given body (as an interface{} that is encoded to xml)
// and status code.
func NewXmlResponder(status int, body interface{}) (Responder, error) {
func NewXmlResponder(status int, body interface{}) (Responder, error) { // nolint: golint
resp, err := NewXmlResponse(status, body)
if err != nil {
return nil, err
Expand All @@ -133,7 +133,7 @@ func NewXmlResponder(status int, body interface{}) (Responder, error) {
// "/test/path",
// NewXmlResponderOrPanic(200, &MyBody),
// )
func NewXmlResponderOrPanic(status int, body interface{}) Responder {
func NewXmlResponderOrPanic(status int, body interface{}) Responder { // nolint: golint
responder, err := NewXmlResponder(status, body)
if err != nil {
panic(err)
Expand All @@ -160,12 +160,12 @@ type dummyReadCloser struct {
func (d *dummyReadCloser) Read(p []byte) (n int, err error) {
n, err = d.body.Read(p)
if err == io.EOF {
d.body.Seek(0, 0)
d.body.Seek(0, 0) // nolint: errcheck
}
return n, err
}

func (d *dummyReadCloser) Close() error {
d.body.Seek(0, 0)
d.body.Seek(0, 0) // nolint: errcheck
return nil
}
16 changes: 8 additions & 8 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestResponderFromResponse(t *testing.T) {
responder := ResponderFromResponse(NewStringResponse(200, "hello world"))

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

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

// Request should be non-nil and different for each response
if response1.Request != nil && response2.Request != nil {
if response1.Request.URL.String() != testUrl {
t.Errorf("Expected request url %s, got: %s", testUrl, response1.Request.URL.String())
if response1.Request.URL.String() != testURL {
t.Errorf("Expected request url %s, got: %s", testURL, response1.Request.URL.String())
}
if response2.Request.URL.String() != testUrlWithQuery {
t.Errorf("Expected request url %s, got: %s", testUrlWithQuery, response2.Request.URL.String())
if response2.Request.URL.String() != testURLWithQuery {
t.Errorf("Expected request url %s, got: %s", testURLWithQuery, response2.Request.URL.String())
}
} else {
t.Error("response.Request should not be nil")
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestNewXmlResponse(t *testing.T) {

func TestNewErrorResponder(t *testing.T) {
responder := NewErrorResponder(errors.New("oh no"))
req, err := http.NewRequest(http.MethodGet, testUrl, nil)
req, err := http.NewRequest(http.MethodGet, testURL, nil)
if err != nil {
t.Fatal("Error creating request")
}
Expand Down
21 changes: 11 additions & 10 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"sync"
)

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

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

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

func runCancelable(responder Responder, req *http.Request) (*http.Response, error) {
if req.Cancel == nil {
// TODO: replace req.Cancel by ctx
if req.Cancel == nil { // nolint: staticcheck
return responder(req)
}

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

go func() {
select {
case <-req.Cancel:
// TODO: req.Cancel replace by ctx
case <-req.Cancel: // nolint: staticcheck
resultch <- result{
response: nil,
err: errors.New("request canceled"),
Expand Down Expand Up @@ -189,10 +192,10 @@ func runCancelable(responder Responder, req *http.Request) (*http.Response, erro
return r.response, r.err
}

// do nothing with timeout
// CancelRequest does nothing with timeout.
func (m *MockTransport) CancelRequest(req *http.Request) {}

// responderForKey returns a responder for a given key
// responderForKey returns a responder for a given key.
func (m *MockTransport) responderForKey(key string) Responder {
m.mu.RLock()
defer m.mu.RUnlock()
Expand Down Expand Up @@ -266,13 +269,11 @@ func sortedQuery(m url.Values) string {
sort.Strings(keys)

var b bytes.Buffer
var values []string
var values []string // nolint: prealloc

for _, k := range keys {
// Do not alter the passed url.Values
for _, v := range m[k] {
values = append(values, v)
}
values = append(values, m[k]...)
sort.Strings(values)

k = url.QueryEscape(k)
Expand Down
Loading

0 comments on commit 13beba8

Please sign in to comment.