Skip to content

Commit ac2099d

Browse files
authored
Merge pull request #74 from maxatome/v1
Update doc
2 parents b7f110b + c35c2f3 commit ac2099d

File tree

2 files changed

+73
-48
lines changed

2 files changed

+73
-48
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestFetchArticles(t *testing.T) {
7373
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
7474

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

7979
// do stuff that makes a request to articles
@@ -86,7 +86,7 @@ func TestFetchArticles(t *testing.T) {
8686
info := httpmock.GetCallCountInfo()
8787
info["GET https://api.mybiz.com/articles"] // number of GET calls made to https://api.mybiz.com/articles
8888
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>
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

@@ -111,7 +111,7 @@ func TestFetchArticles(t *testing.T) {
111111
)
112112

113113
// 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`,
114+
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
115115
func(req *http.Request) (*http.Response, error) {
116116
// Get ID from request
117117
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch

doc.go

+70-45
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,80 @@
22
Package httpmock provides tools for mocking HTTP responses.
33
44
Simple Example:
5-
func TestFetchArticles(t *testing.T) {
6-
httpmock.Activate()
7-
defer httpmock.DeactivateAndReset()
5+
func TestFetchArticles(t *testing.T) {
6+
httpmock.Activate()
7+
defer httpmock.DeactivateAndReset()
88
9-
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
10-
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
9+
// Exact URL match
10+
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
11+
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
1112
12-
// do stuff that makes a request to articles.json
13-
}
13+
// Regexp match (could use httpmock.RegisterRegexpResponder instead)
14+
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/\d+\z`,
15+
httpmock.NewStringResponder(200, `{"id": 1, "name": "My Great Article"}`))
16+
17+
// do stuff that makes a request to articles
18+
19+
// get count info
20+
httpmock.GetTotalCallCount()
21+
22+
// get the amount of calls for the registered responder
23+
info := httpmock.GetCallCountInfo()
24+
info["GET https://api.mybiz.com/articles"] // number of GET calls made to https://api.mybiz.com/articles
25+
info["GET https://api.mybiz.com/articles/id/12"] // number of GET calls made to https://api.mybiz.com/articles/id/12
26+
info[`GET =~^https://api\.mybiz\.com/articles/id/\d+\z`] // number of GET calls made to https://api.mybiz.com/articles/id/<any-number>
27+
}
1428
1529
Advanced Example:
16-
func TestFetchArticles(t *testing.T) {
17-
httpmock.Activate()
18-
defer httpmock.DeactivateAndReset()
19-
20-
// our database of articles
21-
articles := make([]map[string]interface{}, 0)
22-
23-
// mock to list out the articles
24-
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
25-
func(req *http.Request) (*http.Response, error) {
26-
resp, err := httpmock.NewJsonResponse(200, articles)
27-
if err != nil {
28-
return httpmock.NewStringResponse(500, ""), nil
29-
}
30-
return resp
31-
},
32-
)
33-
34-
// mock to add a new article
35-
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles.json",
36-
func(req *http.Request) (*http.Response, error) {
37-
article := make(map[string]interface{})
38-
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
39-
return httpmock.NewStringResponse(400, ""), nil
40-
}
41-
42-
articles = append(articles, article)
43-
44-
resp, err := httpmock.NewJsonResponse(200, article)
45-
if err != nil {
46-
return httpmock.NewStringResponse(500, ""), nil
47-
}
48-
return resp, nil
49-
},
50-
)
51-
52-
// do stuff that adds and checks articles
53-
}
30+
func TestFetchArticles(t *testing.T) {
31+
httpmock.Activate()
32+
defer httpmock.DeactivateAndReset()
33+
34+
// our database of articles
35+
articles := make([]map[string]interface{}, 0)
36+
37+
// mock to list out the articles
38+
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
39+
func(req *http.Request) (*http.Response, error) {
40+
resp, err := httpmock.NewJsonResponse(200, articles)
41+
if err != nil {
42+
return httpmock.NewStringResponse(500, ""), nil
43+
}
44+
return resp, nil
45+
},
46+
)
47+
48+
// return an article related to the request with the help of regexp submatch (\d+)
49+
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
50+
func(req *http.Request) (*http.Response, error) {
51+
// Get ID from request
52+
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
53+
return httpmock.NewJsonResponse(200, map[string]interface{}{
54+
"id": id,
55+
"name": "My Great Article",
56+
})
57+
},
58+
)
59+
60+
// mock to add a new article
61+
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
62+
func(req *http.Request) (*http.Response, error) {
63+
article := make(map[string]interface{})
64+
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
65+
return httpmock.NewStringResponse(400, ""), nil
66+
}
67+
68+
articles = append(articles, article)
69+
70+
resp, err := httpmock.NewJsonResponse(200, article)
71+
if err != nil {
72+
return httpmock.NewStringResponse(500, ""), nil
73+
}
74+
return resp, nil
75+
},
76+
)
5477
78+
// do stuff that adds and checks articles
79+
}
5580
*/
5681
package httpmock

0 commit comments

Comments
 (0)