top

HTTPmock provides tools for mocking HTTP responses.

Simple Example:

func TestFetchArticles(t *testing.T) {
	httpmock.Activate()
	defer httpmock.DeactivateAndReset()

	httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
		httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))

	// do stuff that makes a request to articles.json
}

Advanced Example:

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", "https://api.mybiz.com/articles.json",
		func(req *http.Request) (*http.Response, error) {
			resp, err := httpmock.NewJsonResponse(200, articles)
			if err != nil {
				return httpmock.NewStringResponse(500, ""), nil
			}
			return resp
		},
	)

	// mock to add a new article
	httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles.json",
		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
}

Imported by 4 package(s)

  1. github.com/andrewdruzhinin/go-russianpost/example
  2. github.com/hieven/go-instagram/testUtils
  3. github.com/lytics/go-lytics/mock
  4. github.com/stellar/go/support/http/httptest

Imported only in test by 76 package(s)

  1. github.com/AfterShip/aftership-sdk-go/impl
  2. github.com/HowTheStoryEnds/WG
  3. github.com/Komly/GoMusic/vkapi
  4. github.com/NeilGarb/onesky-go
  5. github.com/Receiptful/go-shopify
  6. github.com/SebastianCzoch/akismet-go
  7. github.com/SebastianCzoch/onesky-go
  8. github.com/SebastianCzoch/smsapi-go
  9. github.com/Staples-Inc/snap-plugin-collector-nginx/nginx
  10. github.com/Urbint/ingest-proto
  11. github.com/alphagov/metadata-api/content
  12. github.com/andrewdruzhinin/go-anti-captcha/anticaptcha
  13. github.com/andrewnester/gopd
  14. github.com/apriendeau/go-marathon
  15. github.com/bernerdschaefer/clearbit
  16. github.com/byrnedo/tictochimp/models/mailchimp
  17. github.com/byrnedo/tictochimp/models/tictail
  18. github.com/cameronstanley/go-reddit
  19. github.com/cardigann/cardigann/indexer
  20. github.com/corybuecker/steam-stats-fetcher/actions
  21. github.com/corybuecker/steam-stats-fetcher/steam
  22. github.com/corybuecker/steam-stats/actions
  23. github.com/corybuecker/steam-stats/steam
  24. github.com/corybuecker/steamfetcher/actions
  25. github.com/corybuecker/steamfetcher/steam
  26. github.com/corybuecker/wikipediasearch
  27. github.com/dsmontoya/gitso
  28. github.com/hieven/go-instagram/models
  29. github.com/jcgay/chtignore
  30. github.com/jinarusha/kstock
  31. github.com/kpurdon/go-api-example/internal/repos
  32. github.com/kucuny/delibird
  33. github.com/laibulle/go-xee
  34. github.com/laibulle/gonest
  35. github.com/laibulle/pepper-sdk
  36. github.com/lycoris0731/go-groovecoaster/groovecoaster
  37. github.com/lytics/go-lytics
  38. github.com/mackerelio/mackerel-agent-plugins/mackerel-plugin-php-fpm
  39. github.com/maddevsio/screen-monitoring/agents/ahrefs_native
  40. github.com/markelog/eclectica/plugins/golang
  41. github.com/markelog/eclectica/plugins/nodejs
  42. github.com/markelog/eclectica/plugins/rust
  43. github.com/markelog/eclectica/request
  44. github.com/neshmi/lg_remote
  45. github.com/onrik/micha
  46. github.com/opencontrol/nvd-cve-resource
  47. github.com/opencontrol/nvd-cve-resource/nvd_cve
  48. github.com/philchia/gopush/pusher
  49. github.com/pipeviz/pipeviz/cmd/pvproxy
  50. github.com/purpleworks/delibird/couriers
  51. github.com/rudes/runestats/statapi
  52. github.com/sebastienblanc/upsgosender
  53. github.com/sebastienfr/go-xee
  54. github.com/seesawlabs/apitest
  55. github.com/seesawlabs/testilla
  56. github.com/segmentio/objects-go
  57. github.com/siadat/clearbit
  58. github.com/simplymeasured/recur
  59. github.com/skip2/rdap
  60. github.com/skip2/rdap/bootstrap
  61. github.com/skygeario/skygear-server/pkg/server/plugin/http
  62. github.com/snowplow/snowplow-golang-tracker/tracker
  63. github.com/softlayer/softlayer-go/session
  64. github.com/srt32/clearbit
  65. github.com/thoughtbot/clearbit
  66. github.com/trustedanalytics/application-broker/cloud
  67. github.com/trustedanalytics/go-cf-lib/api
  68. github.com/urbint/ingest-proto
  69. github.com/vimukthi-git/Aftership-SDK-GoLang/impl
  70. github.com/wpjunior/hyperbigbang/fetch
  71. github.com/yukihir0/hbapi
  72. github.com/zhulik/go_kovalut
  73. gopkg.in/dsmontoya/gitso.v0/bitso
  74. gopkg.in/snowplow/snowplow-golang-tracker.v0/tracker
  75. gopkg.in/snowplow/snowplow-golang-tracker.v1/tracker
  76. gopkg.in/snowplow/snowplow-tracking-cli.v0