+
Skip to content

pombredanne/httpexpect

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

httpexpect GoDoc Gitter Wercker Coveralls

Go module that helps to write nice tests for your HTTP API.

Features

Essential:

  • Incrementally build HTTP requests.
  • Inspect HTTP responses.
  • Inspect JSON payload, recursively (supported types: object, array, string, number, boolean, null).

Misc.:

  • Can communicate with server via HTTP client or invoke HTTP handler directly.
  • Configurable (accepts custom implementations of failure reporter, HTTP client, and logger).

Integrations:

  • By default, uses testify to report failures (can be configured to use assert or require package).
  • May use httputil to dump requests and responses, or more compact logger.
  • Produces nice diff on failure, using gojsondiff.
  • Provides integration with fasthttp client and HTTP handler via fasthttpexpect module.

Status

First stable release (v1) is planned soon. Prior to this, API may be changing slightly.

Documentation

Documentation is available on GoDoc. It contains an overview and reference.

Installation

$ go get github.com/gavv/httpexpect

Examples

See example directory for various usage examples.

  • fruits_test.go

    Using httpexpect with default and custom config. Communicating with server via HTTP client or invoking http.Handler directly.

  • echo_test.go

    Using httpexpect with two http handlers created using echo framework: http.Handler and fasthttp.RequestHandler.

  • iris_test.go

    Using httpexpect with fasthttp.RequestHandler created using iris framework.

Quick start

Here is a complete example of end-to-end test for FruitServer.

import (
	"github.com/gavv/httpexpect"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestFruits(t *testing.T) {
	server := httptest.NewServer(FruitServer())
	defer server.Close()

	e := httpexpect.New(t, server.URL)

	e.GET("/fruits").
		Expect().
		Status(http.StatusOK).JSON().Array().Empty()

	orange := map[string]interface{}{
		"weight": 100,
	}

	e.PUT("/fruits/orange").WithJSON(orange).
		Expect().
		Status(http.StatusNoContent).NoContent()

	apple := map[string]interface{}{
		"colors": []interface{}{"green", "red"},
		"weight": 200,
	}

	e.PUT("/fruits/apple").WithJSON(apple).
		Expect().
		Status(http.StatusNoContent).NoContent()

	e.GET("/fruits").
		Expect().
		Status(http.StatusOK).JSON().Array().ContainsOnly("orange", "apple")

	e.GET("/fruits/orange").
		Expect().
		Status(http.StatusOK).JSON().Object().Equal(orange).NotEqual(apple)

	e.GET("/fruits/orange").
		Expect().
		Status(http.StatusOK).
		JSON().Object().ContainsKey("weight").ValueEqual("weight", 100)

	obj := e.GET("/fruits/apple").
		Expect().
		Status(http.StatusOK).JSON().Object()

	obj.Keys().ContainsOnly("colors", "weight")

	obj.Value("colors").Array().Elements("green", "red")
	obj.Value("colors").Array().Element(0).String().Equal("green")
	obj.Value("colors").Array().Element(1).String().Equal("red")

	obj.Value("weight").Number().Equal(200)

	e.GET("/fruits/melon").
		Expect().
		Status(http.StatusNotFound)
}

Similar modules

Contributing

Feel free to report bugs, suggest improvements, and send pull requests! Don't forget to add documentation and tests for new features and run all tests before submitting pull requests:

$ go test -bench . github.com/gavv/httpexpect/...

License

MIT

About

Go module that helps to write nice tests for your HTTP API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载