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