flute: github.com/suzuki-shunsuke/flute/flute Index | Examples | Files

package flute

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

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 .

Code:

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"}`,
                        },
                    },
                },
            },
        },
    },
}
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com/users?id=10", nil)
if err != nil {
    log.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
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

Package Files

doc.go matcher.go response.go struct.go tester.go transport.go

type Matcher Uses

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 Uses

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 Uses

type Route struct {
    // Name is embedded the assertion and useful to specify where the test fails.
    Name     string
    Matcher  Matcher
    Tester   Tester
    Response Response
}

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

type Service Uses

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 Uses

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 Uses

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 Uses

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.

Package flute imports 13 packages (graph). Updated 2020-09-28. Refresh now. Tools for package owners.