Skip to content

Commit

Permalink
Merge pull request #101 from maxatome/file
Browse files Browse the repository at this point in the history
Add File type + style + increase coverage
  • Loading branch information
maxatome authored Dec 6, 2020
2 parents ee05193 + 0703ec5 commit 053a927
Show file tree
Hide file tree
Showing 16 changed files with 1,353 additions and 800 deletions.
17 changes: 15 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ matrix:
- go: 1.15.x
env:
- USE_LINTER=1
- GO_TEST_OPTS="-covermode=atomic -coverprofile=coverage.out"
install:
- wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $GOPATH/bin v1.30.0
after_success:
Expand All @@ -28,10 +29,22 @@ matrix:

script:
- export GORACE="halt_on_error=1"
- go test -race -covermode=atomic -coverprofile=coverage.out ./...
- go test -race $GO_TEST_OPTS ./...
- >
if [ "$USE_LINTER" = 1 ]; then
golangci-lint run -E gofmt -E golint -E maligned -E misspell -E prealloc -E unconvert -E whitespace ./...;
golangci-lint run --max-issues-per-linter 0 \
--max-same-issues 0 \
-E exportloopref \
-E gocritic \
-E godot \
-E goimports \
-E golint \
-E maligned \
-E misspell \
-E prealloc \
-E unconvert \
-E whitespace \
./...;
fi
notifications:
Expand Down
204 changes: 102 additions & 102 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,82 +65,82 @@ When vendoring is used, `v1` branch has to be specified. Two choices here:
### Simple Example:
```go
func TestFetchArticles(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.Activate()
defer httpmock.DeactivateAndReset()

// Exact URL match
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
// Exact URL match
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))

// Regexp match (could use httpmock.RegisterRegexpResponder instead)
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/\d+\z`,
httpmock.NewStringResponder(200, `{"id": 1, "name": "My Great Article"}`))
// Regexp match (could use httpmock.RegisterRegexpResponder instead)
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/\d+\z`,
httpmock.NewStringResponder(200, `{"id": 1, "name": "My Great Article"}`))

// do stuff that makes a request to articles
...
// do stuff that makes a request to articles
...

// get count info
httpmock.GetTotalCallCount()
// get count info
httpmock.GetTotalCallCount()

// get the amount of calls for the registered responder
info := httpmock.GetCallCountInfo()
info["GET https://api.mybiz.com/articles"] // number of GET calls made to https://api.mybiz.com/articles
info["GET https://api.mybiz.com/articles/id/12"] // number of GET calls made to https://api.mybiz.com/articles/id/12
info[`GET =~^https://api\.mybiz\.com/articles/id/\d+\z`] // number of GET calls made to https://api.mybiz.com/articles/id/<any-number>
// get the amount of calls for the registered responder
info := httpmock.GetCallCountInfo()
info["GET https://api.mybiz.com/articles"] // number of GET calls made to https://api.mybiz.com/articles
info["GET https://api.mybiz.com/articles/id/12"] // number of GET calls made to https://api.mybiz.com/articles/id/12
info[`GET =~^https://api\.mybiz\.com/articles/id/\d+\z`] // number of GET calls made to https://api.mybiz.com/articles/id/<any-number>
}
```

### Advanced Example:
```go
func TestFetchArticles(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

// our database of articles
articles := make([]map[string]interface{}, 0)

// mock to list out the articles
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(200, articles)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)

// return an article related to the request with the help of regexp submatch (\d+)
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
func(req *http.Request) (*http.Response, error) {
// Get ID from request
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
return httpmock.NewJsonResponse(200, map[string]interface{}{
"id": id,
"name": "My Great Article",
})
},
)

// mock to add a new article
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
func(req *http.Request) (*http.Response, error) {
article := make(map[string]interface{})
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}

articles = append(articles, article)

resp, err := httpmock.NewJsonResponse(200, article)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)

// do stuff that adds and checks articles
httpmock.Activate()
defer httpmock.DeactivateAndReset()

// our database of articles
articles := make([]map[string]interface{}, 0)

// mock to list out the articles
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(200, articles)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)

// return an article related to the request with the help of regexp submatch (\d+)
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
func(req *http.Request) (*http.Response, error) {
// Get ID from request
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
return httpmock.NewJsonResponse(200, map[string]interface{}{
"id": id,
"name": "My Great Article",
})
},
)

// mock to add a new article
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
func(req *http.Request) (*http.Response, error) {
article := make(map[string]interface{})
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}

articles = append(articles, article)

resp, err := httpmock.NewJsonResponse(200, article)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)

// do stuff that adds and checks articles
}
```

Expand All @@ -166,39 +166,39 @@ in the same order, the first match stops the search.
// article_suite_test.go

import (
// ...
"github.com/jarcoal/httpmock"
// ...
"github.com/jarcoal/httpmock"
)
// ...
var _ = BeforeSuite(func() {
// block all HTTP requests
httpmock.Activate()
// block all HTTP requests
httpmock.Activate()
})

var _ = BeforeEach(func() {
// remove any mocks
httpmock.Reset()
// remove any mocks
httpmock.Reset()
})

var _ = AfterSuite(func() {
httpmock.DeactivateAndReset()
httpmock.DeactivateAndReset()
})


// article_test.go

import (
// ...
"github.com/jarcoal/httpmock"
// ...
"github.com/jarcoal/httpmock"
)

var _ = Describe("Articles", func() {
It("returns a list of articles", func() {
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
It("returns a list of articles", func() {
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))

// do stuff that makes a request to articles.json
})
// do stuff that makes a request to articles.json
})
})
```

Expand All @@ -207,46 +207,46 @@ var _ = Describe("Articles", func() {
// article_suite_test.go

import (
// ...
"github.com/jarcoal/httpmock"
"github.com/go-resty/resty"
// ...
"github.com/jarcoal/httpmock"
"github.com/go-resty/resty"
)
// ...
var _ = BeforeSuite(func() {
// block all HTTP requests
httpmock.ActivateNonDefault(resty.DefaultClient.GetClient())
// block all HTTP requests
httpmock.ActivateNonDefault(resty.DefaultClient.GetClient())
})

var _ = BeforeEach(func() {
// remove any mocks
httpmock.Reset()
// remove any mocks
httpmock.Reset()
})

var _ = AfterSuite(func() {
httpmock.DeactivateAndReset()
httpmock.DeactivateAndReset()
})


// article_test.go

import (
// ...
"github.com/jarcoal/httpmock"
"github.com/go-resty/resty"
// ...
"github.com/jarcoal/httpmock"
"github.com/go-resty/resty"
)

var _ = Describe("Articles", func() {
It("returns a list of articles", func() {
fixture := `{"status":{"message": "Your message", "code": 200}}`
responder := httpmock.NewStringResponder(200, fixture)
fakeUrl := "https://api.mybiz.com/articles.json"
httpmock.RegisterResponder("GET", fakeUrl, responder)

// fetch the article into struct
articleObject := &models.Article{}
_, err := resty.R().SetResult(articleObject).Get(fakeUrl)

// do stuff with the article object ...
})
It("returns a list of articles", func() {
fixture := `{"status":{"message": "Your message", "code": 200}}`
responder := httpmock.NewStringResponder(200, fixture)
fakeUrl := "https://api.mybiz.com/articles.json"
httpmock.RegisterResponder("GET", fakeUrl, responder)

// fetch the article into struct
articleObject := &models.Article{}
_, err := resty.R().SetResult(articleObject).Get(fakeUrl)

// do stuff with the article object ...
})
})
```
Loading

0 comments on commit 053a927

Please sign in to comment.