1/* 2HTTPmock provides tools for mocking HTTP responses. 3 4Simple Example: 5 func TestFetchArticles(t *testing.T) { 6 httpmock.Activate() 7 defer httpmock.DeactivateAndReset() 8 9 httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json", 10 httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`)) 11 12 // do stuff that makes a request to articles.json 13 } 14 15Advanced Example: 16 func TestFetchArticles(t *testing.T) { 17 httpmock.Activate() 18 defer httpmock.DeactivateAndReset() 19 20 // our database of articles 21 articles := make([]map[string]interface{}, 0) 22 23 // mock to list out the articles 24 httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json", 25 func(req *http.Request) (*http.Response, error) { 26 resp, err := httpmock.NewJsonResponse(200, articles) 27 if err != nil { 28 return httpmock.NewStringResponse(500, ""), nil 29 } 30 return resp 31 }, 32 ) 33 34 // mock to add a new article 35 httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles.json", 36 func(req *http.Request) (*http.Response, error) { 37 article := make(map[string]interface{}) 38 if err := json.NewDecoder(req.Body).Decode(&article); err != nil { 39 return httpmock.NewStringResponse(400, ""), nil 40 } 41 42 articles = append(articles, article) 43 44 resp, err := httpmock.NewJsonResponse(200, article) 45 if err != nil { 46 return httpmock.NewStringResponse(500, ""), nil 47 } 48 return resp, nil 49 }, 50 ) 51 52 // do stuff that adds and checks articles 53 } 54 55*/ 56package httpmock 57