top

Package httpexpect helps with end-to-end HTTP and REST API testing.

Usage examples

See example directory:

- https://godoc.org/github.com/gavv/httpexpect/example
- https://github.com/gavv/httpexpect/tree/master/example

Communication mode

There are two common ways to test API with httpexpect:

- start HTTP server and instruct httpexpect to use HTTP client for communication
- don't start server and instruct httpexpect to invoke http handler directly

The second approach works only if the server is a Go module and its handler can be imported in tests.

Concrete behaviour is determined by Client implementation passed to Config struct. If you're using http.Client, set its Transport field (http.RoundTriper) to one of the following:

1. default (nil) - use regular HTTP transport from net/http (you should start server)
2. httpexpect.Binder - invoke given http.Handler directly
4. httpexpect.FastBinder - invoke given fasthttp.RequestHandler directly

Note that http handler can be usually obtained from http framework you're using. E.g., echo framework provides either http.Handler or fasthttp.RequestHandler.

You can also provide your own Client or http.RoundTriper implementation and do whatever you want to convert http.Request to http.Response.

If you're starting server from tests, it's very handy to use net/http/httptest for that.

Value equality

Whenever values are checked for equality in httpexpect, they are converted to "canonical form":

- type aliases are removed
- numeric types are converted to float64
- non-nil interfaces pointing to nil slices and maps are replaced with nil interfaces
- structs are converted to map[string]interface{}

This is equivalent to subsequently json.Marshal() and json.Unmarshal() the value and currently is implemented so.

Failure handling

When some check fails, failure is reported. If non-fatal failures are used (see Reporter interface), execution is continued and instance that was checked is marked as failed.

If specific instance is marked as failed, all subsequent checks are ignored for this instance and for any child instances retrieved after failure.

Example:

array := NewArray(NewAssertReporter(t), []interface{}{"foo", 123})

e0 := array.Element(0)  // success
e1 := array.Element(1)  // success

s0 := e0.String()       // success
s1 := e1.String()       // failure; e1 and s1 are marked as failed, e0 and s0 are not

s0.Equal("foo")         // success
s1.Equal("bar")         // this check is ignored because s1 is marked as failed

Imported by 25 package(s)

  1. github.com/DispatchMe/go-apimocker
  2. github.com/GeekyPanda/httpcache/httptest
  3. github.com/Jimmy99/iris
  4. github.com/LyricTian/iris
  5. github.com/bear256/iris
  6. github.com/cention-mujibur-rahman/iris/test
  7. github.com/gavv/iris-tests
  8. github.com/importcjj/iris
  9. github.com/jpgriffo/iris
  10. github.com/kataras/gapi
  11. github.com/kataras/iris
  12. github.com/kataras/iris/httptest
  13. github.com/kataras/q
  14. github.com/kekek/iris
  15. github.com/kjfcpua/iris
  16. github.com/pavelz/iris
  17. github.com/philchia/iris
  18. github.com/qiyi/iris
  19. github.com/topfreegames/arkadiko/api
  20. github.com/wolfgangmeyers/iris
  21. gopkg.in/geekypanda/httpcache.v0/httptest
  22. gopkg.in/iris.v4/httptest
  23. gopkg.in/kataras/iris.v3
  24. gopkg.in/kataras/iris.v4/httptest
  25. gopkg.in/kataras/iris.v5/httptest

Imported only in test by 25 package(s)

  1. github.com/GeekyPanda/httpcache
  2. github.com/GeekyPanda/mono
  3. github.com/LyricTian/oauth2/server
  4. github.com/azber/iris/tests
  5. github.com/fasthttp-contrib/sessions
  6. github.com/gavv/httpexpect/example
  7. github.com/geekypanda/httpcache
  8. github.com/geekypanda/mono
  9. github.com/go-gas/gas
  10. github.com/go-gas/sessions/tests
  11. github.com/go-oauth2/oauth2/server
  12. github.com/jedy/iris/tests
  13. github.com/jpillora/cookieauth
  14. github.com/kataras/go-sessions
  15. github.com/packrat386/iris/tests
  16. github.com/qyuhen/httpexpect/example
  17. gopkg.in/gas.v0
  18. gopkg.in/gavv/httpexpect.v1/example
  19. gopkg.in/geekypanda/httpcache.v0
  20. gopkg.in/go-oauth2/oauth2.v3/server
  21. gopkg.in/iris.v4
  22. gopkg.in/kataras/go-sessions.v0
  23. gopkg.in/kataras/iris.v0/tests
  24. gopkg.in/kataras/iris.v4
  25. gopkg.in/oauth2.v3/server

Imports 14 package(s)

  1. github.com/google/go-querystring/query
  2. github.com/yalp/jsonpath
  3. github.com/ajg/form
  4. github.com/fatih/structs
  5. golang.org/x/net/publicsuffix
  6. github.com/gavv/gojsondiff
  7. github.com/gavv/monotime
  8. github.com/stretchr/testify/assert
  9. github.com/valyala/fasthttp
  10. github.com/imkira/go-interpol
  11. github.com/stretchr/testify/require
  12. github.com/xeipuuv/gojsonschema
  13. github.com/moul/http2curl
  14. github.com/gavv/gojsondiff/formatter

Test imports 1 package(s)

  1. github.com/valyala/fasthttp/fasthttpadaptor