argot

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

README

argot

A lightweight simple testing framework in Go. Comes with reasonable support for testing HTTP servers.

GoDoc available here: http://godoc.org/github.com/msackman/argot

Blog post available here: https://tech.labs.oliverwyman.com/blog/2017/04/21/argot-a-lightweight-composable-test-framework-for-go/

Build

go get github.com/msackman/argot

Example

Just to show off one example again:

package main

import (
    "errors"
    "github.com/msackman/argot"
    "net/http"
    "testing"
)

func AlwaysFails() argot.Step {
    return argot.NewNamedStep(
        "AlwaysSucceeds",
        func() error {
            return errors.New("nope")
        },
    )
}

func Testify(t *testing.T) {
    req := argot.NewHttpCall(nil)
    defer req.Reset()
    argot.Steps([]argot.Step{
        req.NewRequest("POST", server+"/api/v1/foo/bar", nil),
        req.RequestHeader("Content-Type", "application/json"),
        req.ResponseStatusEquals(http.StatusOK),
        req.ResponseHeaderEquals("Content-Type", "application/json"),
        req.ResponseHeaderNotExists("Magical-Response"),
        req.ResponseHeaderNotExists("No-Unicorns"),
        req.ResponseBodyEquals("Attack ships on fire off the shoulder of Orion...\n"),
        AlwaysFails(req),
    }).Test(t)
}

As a result running go test will output a helpful log in case any of the steps failed:

% go test
--- FAIL: Testify (0.24s)
        test.go:20: Achieved Steps:
               [NewRequest(POST: https://...)
                RequestHeader(Content-Type: application/json)
                ResponseStatusEquals(200)
                ResponseHeaderEquals(Content-Type: application/json)
                ResponseHeaderNotExists(Magical-Response)
                ResponseHeaderNotExists(No-Unicorns)]
               Error: nope
FAIL

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyError

func AnyError(errs ...error) error

AnyError is a utility function that returns the first non-nil error in the slice, or nil if either the slice or all elements of the slice are nil.

Types

type HttpCall

type HttpCall struct {
	// The client used to perform the request.
	Client *http.Client
	// The request to be made.
	Request *http.Request
	// The response.
	Response *http.Response
	// The body which once received can be repeatedly reused.
	ResponseBody []byte
}

HttpCall captures all the state relating to a single HTTP call. It may be used multiple times. An HttpCall can only be used by a single go-routine at a time.

func NewHttpCall

func NewHttpCall(client *http.Client) *HttpCall

NewHttpCall creates a new HttpCall. If client is nil, a new http.Client is used.

func (*HttpCall) AssertNoRequest

func (hc *HttpCall) AssertNoRequest() error

AssertNoRequest returns nil iff hc.Request is nil.

func (*HttpCall) AssertNoResponse

func (hc *HttpCall) AssertNoResponse() error

AssertNoResponse returns nil iff hc.Response is nil.

func (*HttpCall) AssertRequest

func (hc *HttpCall) AssertRequest() error

AssertRequest returns nil iff hc.Request is non-nil.

func (*HttpCall) Call

func (hc *HttpCall) Call() Step

Call is a Step that when executed performs the HTTP Request Call. This is not normally necessary: all steps that require a Response will perform the HTTP Request when necessary. However, in some tests, you may not care about inspecting the HTTP Response but nevertheless wish the HTTP Request to be made.

func (*HttpCall) EnsureResponse

func (hc *HttpCall) EnsureResponse() error

EnsureResponse is idempotent. If there is already a response then it will return nil. Otherwise if there is no Request then it will return non-nil. Otherwise it will use hc.Client.Do to perform the request, set hc.Response, and return any error that occurs.

Always use this in any step where you want to inspect the hc.Response.

func (*HttpCall) NewRequest

func (hc *HttpCall) NewRequest(method, urlStr string, body io.Reader) Step

NewRequest is a Step that when executed will create a new request using the given parameters. The step will automatically call hc.Reset to tidy up any previous use of hc, and thus prepare hc for the new request.

func (*HttpCall) ReceiveBody

func (hc *HttpCall) ReceiveBody() error

ReceiveBody is idempotent. It will ensure there is a response using hc.EnsureResponse. If there is already a non-nil hc.ResponseBody then it will return nil. Otherwise it will receive the Response.Body, store it in hc.ResponseBody, and return any error that occurs.

Always use this in any step where you want to inspect the hc.ResponseBody.

func (*HttpCall) RequestHeader

func (hc *HttpCall) RequestHeader(key, value string) Step

RequestHeader is a Step that when executed will set the given key and value as a header on the HTTP Request. This can only be done after hc.Request has been created (with NewRequest), and before hc.Response has been created.

func (*HttpCall) Reset

func (hc *HttpCall) Reset() error

Reset is idempotent. You should ensure this is called at the end of life for each HttpCall. It drains Response bodies if necessary, and cleans up resources.

func (*HttpCall) ResponseBodyContains

func (hc *HttpCall) ResponseBodyContains(value string) Step

ResponseBodyContains is a Step that when executed ensures there is a non-nil hc.ResponseBody and errors unless the hc.ResponseBody contains the value parameter using strings.Contains.

func (*HttpCall) ResponseBodyEquals

func (hc *HttpCall) ResponseBodyEquals(value string) Step

ResponseBodyEquals is a Step that when executed ensures there is a non-nil hc.ResponseBody and errors unless the hc.ResponseBody equals the value parameter. Note this is an exact match.

func (*HttpCall) ResponseBodyJSONMatchesStruct

func (hc *HttpCall) ResponseBodyJSONMatchesStruct(expected interface{}) Step

ResponseBodyJSONMatchesStruct is a Step that when executed ensures there is a non-nil hc.ResponseBody, parses it as JSON (via encoding/json) based on the type of the expected structure and errors unless it is equal to the expected value, as validated by the pretty package. The error will contain a structured diff output with a plus/"+" marking the values that were expected and a minus/"-" marking the values that were actually present.

func (*HttpCall) ResponseBodyJSONSchema

func (hc *HttpCall) ResponseBodyJSONSchema(schema string) Step

ResponseBodyJSONSchema is a Step that when executed ensures there is a non-nil hc.ResponseBody and errors unless the hc.ResponseBody can be validated against the schema parameter using gojsonschema.

func (*HttpCall) ResponseBodyMatches

func (hc *HttpCall) ResponseBodyMatches(pattern *regexp.Regexp) Step

ResponseBodyMatches is a Step that when executed ensures there is a non-nil hc.ResponseBody and errors unless the hc.ResponseBody matches the regular expression parameter.

func (*HttpCall) ResponseHeaderContains

func (hc *HttpCall) ResponseHeaderContains(key, value string) Step

ResponseHeaderContains is a Step that when executed ensures there is a non-nil hc.Response and errors unless the hc.Response.Header.Get(key) contains the value parameter using strings.Contains.

func (*HttpCall) ResponseHeaderEquals

func (hc *HttpCall) ResponseHeaderEquals(key, value string) Step

ResponseHeaderEquals is a Step that when executed ensures there is a non-nil hc.Response and errors unless the hc.Response.Header.Get(key) equals the value parameter. Note this is an exact match.

func (*HttpCall) ResponseHeaderExists

func (hc *HttpCall) ResponseHeaderExists(key string) Step

ResponseHeaderExists is a Step that when executed ensures there is a non-nil hc.Response and errors unless hc.Response.Header[key] exists. It says nothing about the value of the header.

func (*HttpCall) ResponseHeaderNotExists

func (hc *HttpCall) ResponseHeaderNotExists(key string) Step

ResponseHeaderNotExists is a Step that when executed ensures there is a non-nil hc.Response and errors unless hc.Response.Header[key] does not exist.

func (*HttpCall) ResponseStatusEquals

func (hc *HttpCall) ResponseStatusEquals(status int) Step

ResponseStatusEquals is a Step that when executed ensures there is a non-nil hc.Response and errors unless the hc.Response.StatusCode equals the status parameter.

type NamedStep

type NamedStep struct {
	StepFunc
	// contains filtered or unexported fields
}

NamedStep extends StepFunc by adding a name, which is mainly of use when formatting a Step.

func NewNamedStep

func NewNamedStep(name string, step StepFunc) *NamedStep

NewNamedStep creates a NamedStep with the given name and Step function.

func (NamedStep) String

func (ns NamedStep) String() string

type Step

type Step interface {
	Go() error
}

Step represents a step in a test.

type StepFunc

type StepFunc func() error

StepFunc is the basic type of a Step: a function that takes no arguments and returns an error.

func (StepFunc) Go

func (sf StepFunc) Go() error

type Steps

type Steps []Step

If we can have a single Step, then we can have a slice of Steps representing the order of Steps in a larger unit.

func (Steps) Go

func (ss Steps) Go() error

func (Steps) Test

func (ss Steps) Test(t *testing.T) (results Steps, err error)

Test runs the steps in order and returns either all the steps, or all the steps that did not error, plus the step that errored. Thus the results are always a prefix of the Steps. t can be nil. If t is not nil and an error occurs, then t.Fatal will be called. If an error occurs, it will be returned.

Jump to

Keyboard shortcuts

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