@@ -65,82 +65,82 @@ When vendoring is used, `v1` branch has to be specified. Two choices here:
6565### Simple Example:
6666``` go
6767func 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
9595func 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
168168import (
169- // ...
170- " github.com/jarcoal/httpmock"
169+ // ...
170+ " github.com/jarcoal/httpmock"
171171)
172172// ...
173173var _ = BeforeSuite (func () {
174- // block all HTTP requests
175- httpmock.Activate ()
174+ // block all HTTP requests
175+ httpmock.Activate ()
176176})
177177
178178var _ = BeforeEach (func () {
179- // remove any mocks
180- httpmock.Reset ()
179+ // remove any mocks
180+ httpmock.Reset ()
181181})
182182
183183var _ = AfterSuite (func () {
184- httpmock.DeactivateAndReset ()
184+ httpmock.DeactivateAndReset ()
185185})
186186
187187
188188// article_test.go
189189
190190import (
191- // ...
192- " github.com/jarcoal/httpmock"
191+ // ...
192+ " github.com/jarcoal/httpmock"
193193)
194194
195195var _ = 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
209209import (
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// ...
215215var _ = 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
220220var _ = BeforeEach (func () {
221- // remove any mocks
222- httpmock.Reset ()
221+ // remove any mocks
222+ httpmock.Reset ()
223223})
224224
225225var _ = AfterSuite (func () {
226- httpmock.DeactivateAndReset ()
226+ httpmock.DeactivateAndReset ()
227227})
228228
229229
230230// article_test.go
231231
232232import (
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
238238var _ = 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