Skip to content

Commit 053a927

Browse files
authored
Merge pull request #101 from maxatome/file
Add File type + style + increase coverage
2 parents ee05193 + 0703ec5 commit 053a927

16 files changed

Lines changed: 1353 additions & 800 deletions

.travis.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ matrix:
1616
- go: 1.15.x
1717
env:
1818
- USE_LINTER=1
19+
- GO_TEST_OPTS="-covermode=atomic -coverprofile=coverage.out"
1920
install:
2021
- wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $GOPATH/bin v1.30.0
2122
after_success:
@@ -28,10 +29,22 @@ matrix:
2829

2930
script:
3031
- export GORACE="halt_on_error=1"
31-
- go test -race -covermode=atomic -coverprofile=coverage.out ./...
32+
- go test -race $GO_TEST_OPTS ./...
3233
- >
3334
if [ "$USE_LINTER" = 1 ]; then
34-
golangci-lint run -E gofmt -E golint -E maligned -E misspell -E prealloc -E unconvert -E whitespace ./...;
35+
golangci-lint run --max-issues-per-linter 0 \
36+
--max-same-issues 0 \
37+
-E exportloopref \
38+
-E gocritic \
39+
-E godot \
40+
-E goimports \
41+
-E golint \
42+
-E maligned \
43+
-E misspell \
44+
-E prealloc \
45+
-E unconvert \
46+
-E whitespace \
47+
./...;
3548
fi
3649
3750
notifications:

README.md

Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -65,82 +65,82 @@ When vendoring is used, `v1` branch has to be specified. Two choices here:
6565
### Simple Example:
6666
```go
6767
func TestFetchArticles(t *testing.T) {
68-
httpmock.Activate()
69-
defer httpmock.DeactivateAndReset()
68+
httpmock.Activate()
69+
defer httpmock.DeactivateAndReset()
7070

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

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

79-
// do stuff that makes a request to articles
80-
...
79+
// do stuff that makes a request to articles
80+
...
8181

82-
// get count info
83-
httpmock.GetTotalCallCount()
82+
// get count info
83+
httpmock.GetTotalCallCount()
8484

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

9393
### Advanced Example:
9494
```go
9595
func TestFetchArticles(t *testing.T) {
96-
httpmock.Activate()
97-
defer httpmock.DeactivateAndReset()
98-
99-
// our database of articles
100-
articles := make([]map[string]interface{}, 0)
101-
102-
// mock to list out the articles
103-
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
104-
func(req *http.Request) (*http.Response, error) {
105-
resp, err := httpmock.NewJsonResponse(200, articles)
106-
if err != nil {
107-
return httpmock.NewStringResponse(500, ""), nil
108-
}
109-
return resp, nil
110-
},
111-
)
112-
113-
// return an article related to the request with the help of regexp submatch (\d+)
114-
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
115-
func(req *http.Request) (*http.Response, error) {
116-
// Get ID from request
117-
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
118-
return httpmock.NewJsonResponse(200, map[string]interface{}{
119-
"id": id,
120-
"name": "My Great Article",
121-
})
122-
},
123-
)
124-
125-
// mock to add a new article
126-
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
127-
func(req *http.Request) (*http.Response, error) {
128-
article := make(map[string]interface{})
129-
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
130-
return httpmock.NewStringResponse(400, ""), nil
131-
}
132-
133-
articles = append(articles, article)
134-
135-
resp, err := httpmock.NewJsonResponse(200, article)
136-
if err != nil {
137-
return httpmock.NewStringResponse(500, ""), nil
138-
}
139-
return resp, nil
140-
},
141-
)
142-
143-
// do stuff that adds and checks articles
96+
httpmock.Activate()
97+
defer httpmock.DeactivateAndReset()
98+
99+
// our database of articles
100+
articles := make([]map[string]interface{}, 0)
101+
102+
// mock to list out the articles
103+
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
104+
func(req *http.Request) (*http.Response, error) {
105+
resp, err := httpmock.NewJsonResponse(200, articles)
106+
if err != nil {
107+
return httpmock.NewStringResponse(500, ""), nil
108+
}
109+
return resp, nil
110+
},
111+
)
112+
113+
// return an article related to the request with the help of regexp submatch (\d+)
114+
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
115+
func(req *http.Request) (*http.Response, error) {
116+
// Get ID from request
117+
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
118+
return httpmock.NewJsonResponse(200, map[string]interface{}{
119+
"id": id,
120+
"name": "My Great Article",
121+
})
122+
},
123+
)
124+
125+
// mock to add a new article
126+
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
127+
func(req *http.Request) (*http.Response, error) {
128+
article := make(map[string]interface{})
129+
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
130+
return httpmock.NewStringResponse(400, ""), nil
131+
}
132+
133+
articles = append(articles, article)
134+
135+
resp, err := httpmock.NewJsonResponse(200, article)
136+
if err != nil {
137+
return httpmock.NewStringResponse(500, ""), nil
138+
}
139+
return resp, nil
140+
},
141+
)
142+
143+
// do stuff that adds and checks articles
144144
}
145145
```
146146

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

168168
import (
169-
// ...
170-
"github.com/jarcoal/httpmock"
169+
// ...
170+
"github.com/jarcoal/httpmock"
171171
)
172172
// ...
173173
var _ = BeforeSuite(func() {
174-
// block all HTTP requests
175-
httpmock.Activate()
174+
// block all HTTP requests
175+
httpmock.Activate()
176176
})
177177

178178
var _ = BeforeEach(func() {
179-
// remove any mocks
180-
httpmock.Reset()
179+
// remove any mocks
180+
httpmock.Reset()
181181
})
182182

183183
var _ = AfterSuite(func() {
184-
httpmock.DeactivateAndReset()
184+
httpmock.DeactivateAndReset()
185185
})
186186

187187

188188
// article_test.go
189189

190190
import (
191-
// ...
192-
"github.com/jarcoal/httpmock"
191+
// ...
192+
"github.com/jarcoal/httpmock"
193193
)
194194

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

200-
// do stuff that makes a request to articles.json
201-
})
200+
// do stuff that makes a request to articles.json
201+
})
202202
})
203203
```
204204

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

209209
import (
210-
// ...
211-
"github.com/jarcoal/httpmock"
212-
"github.com/go-resty/resty"
210+
// ...
211+
"github.com/jarcoal/httpmock"
212+
"github.com/go-resty/resty"
213213
)
214214
// ...
215215
var _ = BeforeSuite(func() {
216-
// block all HTTP requests
217-
httpmock.ActivateNonDefault(resty.DefaultClient.GetClient())
216+
// block all HTTP requests
217+
httpmock.ActivateNonDefault(resty.DefaultClient.GetClient())
218218
})
219219

220220
var _ = BeforeEach(func() {
221-
// remove any mocks
222-
httpmock.Reset()
221+
// remove any mocks
222+
httpmock.Reset()
223223
})
224224

225225
var _ = AfterSuite(func() {
226-
httpmock.DeactivateAndReset()
226+
httpmock.DeactivateAndReset()
227227
})
228228

229229

230230
// article_test.go
231231

232232
import (
233-
// ...
234-
"github.com/jarcoal/httpmock"
235-
"github.com/go-resty/resty"
233+
// ...
234+
"github.com/jarcoal/httpmock"
235+
"github.com/go-resty/resty"
236236
)
237237

238238
var _ = Describe("Articles", func() {
239-
It("returns a list of articles", func() {
240-
fixture := `{"status":{"message": "Your message", "code": 200}}`
241-
responder := httpmock.NewStringResponder(200, fixture)
242-
fakeUrl := "https://api.mybiz.com/articles.json"
243-
httpmock.RegisterResponder("GET", fakeUrl, responder)
244-
245-
// fetch the article into struct
246-
articleObject := &models.Article{}
247-
_, err := resty.R().SetResult(articleObject).Get(fakeUrl)
248-
249-
// do stuff with the article object ...
250-
})
239+
It("returns a list of articles", func() {
240+
fixture := `{"status":{"message": "Your message", "code": 200}}`
241+
responder := httpmock.NewStringResponder(200, fixture)
242+
fakeUrl := "https://api.mybiz.com/articles.json"
243+
httpmock.RegisterResponder("GET", fakeUrl, responder)
244+
245+
// fetch the article into struct
246+
articleObject := &models.Article{}
247+
_, err := resty.R().SetResult(articleObject).Get(fakeUrl)
248+
249+
// do stuff with the article object ...
250+
})
251251
})
252252
```

0 commit comments

Comments
 (0)