flute

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package flute provides the HTTP client testing framework. With flute you can define test cases declaratively. You can test request parameters such as the request path, headers, and body. And you can also mock the HTTP server.

flute uses https://github.com/stretchr/testify internally, so it is easy to understand why the test is failed.

Please see the examples https://github.com/suzuki-shunsuke/flute/tree/master/examples .

Example (SimpleMock)
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"

	"github.com/suzuki-shunsuke/flute/flute"
)

func main() {
	http.DefaultClient = &http.Client{
		Transport: &flute.Transport{
			// if *testing.T isn't given, the transport is a just mock and doesn't run the test.
			// T: t,
			Services: []flute.Service{
				{
					Endpoint: "http://example.com",
					Routes: []flute.Route{
						{
							Name: "get a user",
							Matcher: &flute.Matcher{
								Method: "GET",
								Path:   "/users",
								Query: url.Values{
									"id": []string{"10"},
								},
							},
							Response: &flute.Response{
								Base: http.Response{
									StatusCode: 201,
								},
								BodyString: `{"id": 10, "name": "foo", "email": "foo@example.com"}`,
							},
						},
					},
				},
			},
		},
	}
	resp, err := http.Get("http://example.com/users?id=10")
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b))
}
Output:

{"id": 10, "name": "foo", "email": "foo@example.com"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher struct {
	// Match is a custom function to check the request matches with the route.
	Match func(req *http.Request) (bool, error)
	// Path is the request method such as "GET".
	Method string
	// Path is the request path.
	Path string
	// PartOfQuery is the request query parameters.
	PartOfQuery url.Values
	// Query is the request query parameters.
	Query url.Values
	// BodyString is the request body.
	BodyString string
	// BodyJSON is marshaled to JSON and compared to the request body as JSON.
	BodyJSON interface{}
	// BodyJSONString is a JSON string and compared to the request body as JSON.
	BodyJSONString string
	// PartOfHeader is the request header's conditions.
	// If the header value is nil, RoundTrip checks whether the key is included in the request header.
	// Otherwise, RoundTrip also checks whether the value is equal.
	PartOfHeader http.Header
	// Header is the request header's conditions.
	Header http.Header
}

Matcher has conditions the request matches with the route.

type Response

type Response struct {
	// Base is the base response.
	Base http.Response
	// If Response isn't nil, Response is called to return the response and other parameters are ignored.
	Response func(req *http.Request) (*http.Response, error)
	// BodyJSON is marshaled to JSON and used as the response body.
	// BodyJSON and BodyString should only be set to one or the other.
	BodyJSON interface{}
	// BodyString is the response body.
	// BodyJSON and BodyString should only be set to one or the other.
	BodyString string
}

Response has the response parameters.

type Route

type Route struct {
	// Name is embedded the assertion and useful to specify where the test fails.
	Name string
	// SPEC if the matcher is nil, the route matches the request.
	Matcher *Matcher
	// SPEC if the tester is nil, no test is run
	Tester   *Tester
	Response *Response
}

Route is the pair of the macher, tester, and response.

type Service

type Service struct {
	// The format of Endpoint should be "scheme://host", and other parameters
	// such as path and queries shouldn't be set.
	// These parameters should be set at the matcher or tester.
	Endpoint string
	// If the request matches with a route, other routes are ignored.
	Routes []Route
}

Service is a service.

type Tester

type Tester struct {
	Test func(*testing.T, *http.Request, *Service, *Route)
	// Path is the request path.
	Path string
	// Path is the request method such as "GET".
	Method string
	// BodyString is the request body.
	BodyString string
	// BodyJSON is marshaled to JSON and compared to the request body as JSON.
	BodyJSON interface{}
	// BodyJSONString is a JSON string and compared to the request body as JSON.
	BodyJSONString string
	// PartOfHeader is the request header's conditions.
	// If the header value is nil, RoundTrip checks whether the key is included in the request header.
	// Otherwise, RoundTrip also checks whether the value is equal.
	PartOfHeader http.Header
	// Header is the request header's conditions.
	Header http.Header
	// PartOfQuery is the request query parameters.
	// If the query value is nil, RoundTrip checks whether the key is included in the request query.
	// Otherwise, RoundTrip also checks whether the value is equal.
	PartOfQuery url.Values
	// Query is the request query parameters.
	Query url.Values
}

Tester has the request's tests.

type Transport

type Transport struct {
	// Each service's endpoint should be unique.
	Services []Service
	// If *testing.T is nil, the transport is a just mock and doesn't run the test.
	T *testing.T
	// Transport is used when the request doesn't match with any services.
	Transport http.RoundTripper
}

Transport implements http.RoundTripper.

func (*Transport) RoundTrip

func (transport *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper. RoundTrip traverses the matched route and run the test and returns response.

Jump to

Keyboard shortcuts

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