|
2 | 2 | Package httpmock provides tools for mocking HTTP responses.
|
3 | 3 |
|
4 | 4 | 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() |
8 | 8 |
|
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"}]`)) |
11 | 12 |
|
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 | + } |
14 | 28 |
|
15 | 29 | 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 | + ) |
54 | 77 |
|
| 78 | + // do stuff that adds and checks articles |
| 79 | + } |
55 | 80 | */
|
56 | 81 | package httpmock
|
0 commit comments