• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..24-Nov-2021-

httpmock-61bcb58a0752/H04-Mar-2018-1,308882

.gitignoreH A D24-Nov-2021252 2317

.travis.ymlH A D24-Nov-2021100 129

LICENSEH A D24-Nov-20211.1 KiB2116

README.mdH A D24-Nov-20212.8 KiB11789

doc.goH A D24-Nov-20211.5 KiB571

env.goH A D24-Nov-2021127 128

response.goH A D24-Nov-20213.9 KiB13596

transport.goH A D24-Nov-20219.7 KiB328183

README.md

1# httpmock [![Build Status](https://travis-ci.org/jarcoal/httpmock.png?branch=master)](https://travis-ci.org/jarcoal/httpmock)
2
3Easy mocking of http responses from external resources.
4
5## Install
6
7Uses gopkg to read from `v1` branch:
8
9    go get gopkg.in/jarcoal/httpmock.v1
10
11You can also use vendoring for the v1 branch if you feel so inclined.
12
13Currently supports Go 1.7 but also works with 1.6 for now.
14
15### Simple Example:
16```go
17func TestFetchArticles(t *testing.T) {
18	httpmock.Activate()
19	defer httpmock.DeactivateAndReset()
20
21	httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
22		httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
23
24  // get count info
25  httpmock.GetTotalCallCount()
26
27  // get the amount of calls for the registered responder
28  info := httpmock.GetCallCountInfo()
29  info["GET https://api.mybiz.com/articles.json"] // number of GET calls made to https://api.mybiz.com/articles.json
30
31	// do stuff that makes a request to articles.json
32}
33```
34
35### Advanced Example:
36```go
37func TestFetchArticles(t *testing.T) {
38	httpmock.Activate()
39	defer httpmock.DeactivateAndReset()
40
41	// our database of articles
42	articles := make([]map[string]interface{}, 0)
43
44	// mock to list out the articles
45	httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
46		func(req *http.Request) (*http.Response, error) {
47			resp, err := httpmock.NewJsonResponse(200, articles)
48			if err != nil {
49				return httpmock.NewStringResponse(500, ""), nil
50			}
51			return resp, nil
52		},
53	)
54
55	// mock to add a new article
56	httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles.json",
57		func(req *http.Request) (*http.Response, error) {
58			article := make(map[string]interface{})
59			if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
60				return httpmock.NewStringResponse(400, ""), nil
61			}
62
63			articles = append(articles, article)
64
65			resp, err := httpmock.NewJsonResponse(200, article)
66			if err != nil {
67				return httpmock.NewStringResponse(500, ""), nil
68			}
69			return resp, nil
70		},
71	)
72
73	// do stuff that adds and checks articles
74}
75```
76
77### [Ginkgo](https://onsi.github.io/ginkgo/) Example:
78```go
79// article_suite_test.go
80
81import (
82	// ...
83	"github.com/jarcoal/httpmock"
84)
85// ...
86var _ = BeforeSuite(func() {
87	// block all HTTP requests
88	httpmock.Activate()
89})
90
91var _ = BeforeEach(func() {
92	// remove any mocks
93	httpmock.Reset()
94})
95
96var _ = AfterSuite(func() {
97	httpmock.DeactivateAndReset()
98})
99
100
101// article_test.go
102
103import (
104	// ...
105	"github.com/jarcoal/httpmock"
106)
107
108var _ = Describe("Articles", func() {
109	It("returns a list of articles", func() {
110		httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
111			httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
112
113		// do stuff that makes a request to articles.json
114	})
115})
116```
117