top

Package ghttp supports testing HTTP clients by providing a test server (simply a thin wrapper around httptest's server) that supports registering multiple handlers. Incoming requests are not routed between the different handlers - rather it is merely the order of the handlers that matters. The first request is handled by the first registered handler, the second request by the second handler, etc.

The intent here is to have each handler *verify* that the incoming request is valid. To accomplish, ghttp also provides a collection of bite-size handlers that each perform one aspect of request verification. These can be composed together and registered with a ghttp server. The result is an expressive language for describing the requests generated by the client under test.

Here's a simple example, note that the server handler is only defined in one BeforeEach and then modified, as required, by the nested BeforeEaches. A more comprehensive example is available at https://onsi.github.io/gomega/#_testing_http_clients

var _ = Describe("A Sprockets Client", func() {
	var server *ghttp.Server
	var client *SprocketClient
	BeforeEach(func() {
		server = ghttp.NewServer()
		client = NewSprocketClient(server.URL(), "skywalker", "tk427")
	})

	AfterEach(func() {
		server.Close()
	})

	Describe("fetching sprockets", func() {
		var statusCode int
		var sprockets []Sprocket
		BeforeEach(func() {
			statusCode = http.StatusOK
			sprockets = []Sprocket{}
			server.AppendHandlers(ghttp.CombineHandlers(
				ghttp.VerifyRequest("GET", "/sprockets"),
				ghttp.VerifyBasicAuth("skywalker", "tk427"),
				ghttp.RespondWithJSONEncodedPtr(&statusCode, &sprockets),
			))
		})

		Context("when requesting all sprockets", func() {
			Context("when the response is succesful", func() {
				BeforeEach(func() {
					sprockets = []Sprocket{
						NewSprocket("Alfalfa"),
						NewSprocket("Banana"),
					}
				})

				It("should return the returned sprockets", func() {
					Ω(client.Sprockets()).Should(Equal(sprockets))
				})
			})

			Context("when the response is missing", func() {
				BeforeEach(func() {
					statusCode = http.StatusNotFound
				})

				It("should return an empty list of sprockets", func() {
					Ω(client.Sprockets()).Should(BeEmpty())
				})
			})

			Context("when the response fails to authenticate", func() {
				BeforeEach(func() {
					statusCode = http.StatusUnauthorized
				})

				It("should return an AuthenticationError error", func() {
					sprockets, err := client.Sprockets()
					Ω(sprockets).Should(BeEmpty())
					Ω(err).Should(MatchError(AuthenticationError))
				})
			})

			Context("when the response is a server failure", func() {
				BeforeEach(func() {
					statusCode = http.StatusInternalServerError
				})

				It("should return an InternalError error", func() {
					sprockets, err := client.Sprockets()
					Ω(sprockets).Should(BeEmpty())
					Ω(err).Should(MatchError(InternalError))
				})
			})
		})

		Context("when requesting some sprockets", func() {
			BeforeEach(func() {
				sprockets = []Sprocket{
					NewSprocket("Alfalfa"),
					NewSprocket("Banana"),
				}

				server.WrapHandler(0, ghttp.VerifyRequest("GET", "/sprockets", "filter=FOOD"))
			})

			It("should make the request with a filter", func() {
				Ω(client.Sprockets("food")).Should(Equal(sprockets))
			})
		})
	})
})

Imports 2 package(s)

  1. github.com/onsi/gomega/types
  2. github.com/onsi/gomega

Test imports 1 package(s)

  1. github.com/onsi/ginkgo