httpex

package
v1.4.0-201911112215 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2019 License: MIT Imports: 18 Imported by: 1

Documentation

Overview

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

Usage examples

See example directory:

Communication mode

There are two common ways to test API with http:

  • start HTTP server and instruct http to use HTTP client for communication
  • don't start server and instruct http 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 HTTP transport from net/http (you should start server)
  2. http.Binder - invoke given http.Handler directly
  3. http.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 implementation of RequestFactory (creates http.request), or Client (gets http.request and returns http.response).

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

Value equality

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

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

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(baseURL string) *expect

New returns a new expect object.

baseURL specifies URL to prepended to all request. My be empty. If non-empty, trailing slash is allowed but not required and is appended automatically.

New is a shorthand for WithConfig. It uses:

  • CompactPrinter as Printer, with testing.TB as Logger
  • AssertReporter as Reporter
  • defaultRequestFactory as RequestFactory

Client is set to a default client with a non-nil Jar:

&http.Client{
    Jar: http.NewJar(),
}

Example:

func TestSomething(t *testing.T) {
    e := http.New(t, "http://example.com/")

    e.GET("/path").
        expect().
        Status(http.StatusOK)
}

func WithConfig

func WithConfig(config Config) *expect

WithConfig returns a new expect object with given config.

If RequestFactory is nil, it's set to a defaultRequestFactory instance.

If Client is nil, it's set to a default client with a non-nil Jar:

&http.Client{
    Jar: http.NewJar(),
}

Example:

func TestWithConfig(t *testing.T) {
    e := http.WithConfig(http.Config{
        BaseURL:  "http://example.com/",
        Client:   &http.Client{
            Transport: http.NewBinder(myHandler()),
            Jar:       http.NewJar(),
        },
    })

    e.GET("/path").
        expect().
        Status(http.StatusOK)
}

Types

type Client

type Client interface {
	// Do sends request and returns response.
	Do(*http.Request) (*http.Response, error)
}

Client is used to send http.request and receive http.response. http.Client implements this interface.

Binder and FastBinder may be used to obtain this interface implementation.

Example:

httpBinderClient := &http.Client{
  Transport: http.NewBinder(HTTPHandler),
}
fastBinderClient := &http.Client{
  Transport: http.NewFastBinder(FastHTTPHandler),
}

type Config

type Config struct {
	// BaseURL is a URL to prepended to all request. My be empty. If
	// non-empty, trailing slash is allowed but not required and is
	// appended automatically.
	BaseURL string

	// RequestFactory is used to pass in a custom *http.request generation func.
	// May be nil.
	//
	// You can use defaultRequestFactory, or provide custom implementation.
	// Useful for Google App Engine testing for example.
	RequestFactory RequestFactory

	// Client is used to send http.request and receive http.response.
	// Should not be nil.
	//
	// You can use http.DefaultClient or http.Client, or provide
	// custom implementation.
	Client Client
}

Config contains various settings.

type RequestFactory

type RequestFactory interface {
	NewRequest(method, urlStr string, body io.Reader) (*http.Request, error)
}

RequestFactory is used to create all http.request objects.

func DefaultRequestFactory

func DefaultRequestFactory() RequestFactory

DefaultRequestFactory return a defaultRequestFactory Instance.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL