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 5 package(s)

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

Imported only in test by 85 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/dkuntz2/sidelines
  28. github.com/dsmontoya/gitso
  29. github.com/dsmontoya/gitso/bitso
  30. github.com/hieven/go-instagram/models
  31. github.com/jcgay/chtignore
  32. github.com/jinarusha/kstock
  33. github.com/jonas747/go-reddit
  34. github.com/kpurdon/go-api-example/internal/repos
  35. github.com/kucuny/delibird
  36. github.com/laibulle/go-xee
  37. github.com/laibulle/gonest
  38. github.com/laibulle/pepper-sdk
  39. github.com/lycoris0731/go-groovecoaster/groovecoaster
  40. github.com/lytics/go-lytics
  41. github.com/mackerelio/mackerel-agent-plugins/mackerel-plugin-php-fpm
  42. github.com/maddevsio/screen-monitoring/agents/ahrefs_native
  43. github.com/markbates/goth/providers/auth0
  44. github.com/markelog/eclectica/plugins/golang
  45. github.com/markelog/eclectica/plugins/nodejs
  46. github.com/markelog/eclectica/plugins/rust
  47. github.com/markelog/eclectica/request
  48. github.com/mmcloughlin/cite
  49. github.com/mmcloughlin/pearl/tordir
  50. github.com/neshmi/lg_remote
  51. github.com/onrik/micha
  52. github.com/opencontrol/nvd-cve-resource
  53. github.com/opencontrol/nvd-cve-resource/nvd_cve
  54. github.com/philchia/gopush/pusher
  55. github.com/pipeviz/pipeviz/cmd/pvproxy
  56. github.com/placer14/moxie/proxyhandler
  57. github.com/placer14/proxyhandler/proxyhandler
  58. github.com/purpleworks/delibird/couriers
  59. github.com/rudes/runestats/statapi
  60. github.com/sebastienblanc/upsgosender
  61. github.com/sebastienfr/go-xee
  62. github.com/seesawlabs/apitest
  63. github.com/seesawlabs/testilla
  64. github.com/segmentio/objects-go
  65. github.com/siadat/clearbit
  66. github.com/simplymeasured/recur
  67. github.com/skip2/rdap
  68. github.com/skip2/rdap/bootstrap
  69. github.com/skygeario/skygear-server/pkg/server/plugin/http
  70. github.com/snowplow/snowplow-golang-tracker/tracker
  71. github.com/softlayer/softlayer-go/session
  72. github.com/srt32/clearbit
  73. github.com/thoughtbot/clearbit
  74. github.com/trustedanalytics/application-broker/cloud
  75. github.com/trustedanalytics/go-cf-lib/api
  76. github.com/urbint/ingest-proto
  77. github.com/vimukthi-git/Aftership-SDK-GoLang/impl
  78. github.com/wpjunior/hyperbigbang/fetch
  79. github.com/yoheimuta/mackerel-agent-plugins/mackerel-plugin-php-fpm
  80. github.com/yukihir0/hbapi
  81. github.com/zhulik/go_kovalut
  82. gopkg.in/dsmontoya/gitso.v0/bitso
  83. gopkg.in/snowplow/snowplow-golang-tracker.v0/tracker
  84. gopkg.in/snowplow/snowplow-golang-tracker.v1/tracker
  85. gopkg.in/snowplow/snowplow-tracking-cli.v0