I working on a client that connects to a server (offering a REST endpoint). The client needs to make a chain of 11 different requests to complete an action (it's for a rococo backup system).
I'm writing my client in Golang and I also want to write my mocks in Golang. What I'm unclear about is how a test called func TestTheWorld
would call into the client's func main()
, to test completion of the chain of 11 requests. Would I compile my client to a binary and then call the binary (and it's environment variables) from the tests?
I'm looking at the Advanced Example in jarcoal/httpmock (but I could use another library). At the end the example says // do stuff that adds and checks articles
, is that where I would call my client?
I've pasted the Advanced Example below, for future reference.
func TestFetchArticles(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// our database of articles
articles := make([]map[string]interface{}, 0)
// mock to list out the articles
httpmock.RegisterResponder("GET", "http://ift.tt/2xbzGwb",
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(200, articles)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)
// mock to add a new article
httpmock.RegisterResponder("POST", "http://ift.tt/2xbzGwb",
func(req *http.Request) (*http.Response, error) {
article := make(map[string]interface{})
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
articles = append(articles, article)
resp, err := httpmock.NewJsonResponse(200, article)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)
// do stuff that adds and checks articles
}
Aucun commentaire:
Enregistrer un commentaire