Skip to content

Commit ee13561

Browse files
authored
docs: update resty example (#160)
* docs: update resty example Fixes issue #150. Switches the example to use resty/v2, which no longer supports resty.DefaultClient. It additionally uses a json responder instead of a text responder to set the content-type header correctly, as referenced in #150 (comment)
1 parent 1ebd59a commit ee13561

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

README.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,17 @@ var _ = Describe("Articles", func() {
214214
import (
215215
// ...
216216
"github.com/jarcoal/httpmock"
217-
"github.com/go-resty/resty"
217+
"github.com/go-resty/resty/v2"
218218
)
219219
// ...
220+
221+
// global client (using resty.New() creates a new transport each time,
222+
// so you need to use the same one here and when making the request)
223+
var client = resty.New()
224+
220225
var _ = BeforeSuite(func() {
221226
// block all HTTP requests
222-
httpmock.ActivateNonDefault(resty.DefaultClient.GetClient())
227+
httpmock.ActivateNonDefault(client.GetClient())
223228
})
224229

225230
var _ = BeforeEach(func() {
@@ -237,18 +242,26 @@ var _ = AfterSuite(func() {
237242
import (
238243
// ...
239244
"github.com/jarcoal/httpmock"
240-
"github.com/go-resty/resty"
241245
)
242246

247+
type Article struct {
248+
Status struct {
249+
Message string `json:"message"`
250+
Code int `json:"code"`
251+
} `json:"status"`
252+
}
253+
243254
var _ = Describe("Articles", func() {
244255
It("returns a list of articles", func() {
245256
fixture := `{"status":{"message": "Your message", "code": 200}}`
246-
responder := httpmock.NewStringResponder(200, fixture)
257+
// have to use NewJsonResponder to get an application/json content-type
258+
// alternatively, create a go object instead of using json.RawMessage
259+
responder, _ := httpmock.NewJsonResponder(200, json.RawMessage(`{"status":{"message": "Your message", "code": 200}}`)
247260
fakeUrl := "https://api.mybiz.com/articles.json"
248261
httpmock.RegisterResponder("GET", fakeUrl, responder)
249262

250263
// fetch the article into struct
251-
articleObject := &models.Article{}
264+
articleObject := &Article{}
252265
_, err := resty.R().SetResult(articleObject).Get(fakeUrl)
253266

254267
// do stuff with the article object ...

0 commit comments

Comments
 (0)