testpilot

package module
v0.0.0-...-3af3f10 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 16 Imported by: 0

README

testpilot

testpilot is a Go package that provides a testing framework for making HTTP requests and validating responses. It is designed to help create test plans and perform assertions on HTTP requests and responses.

Features

  • Create and manage test plans for HTTP testing.
  • Perform HTTP requests with customizable methods, URLs, headers, and body content.
  • Validate HTTP responses with assertions on status codes, response body, and JSON paths.
  • Cache and retrieve responses for later use in tests.

Installation

To install testpilot, run the following command:

go get github.com/trapajim/testpilot

Usage

Creating a Test Plan

Create a new test plan using the NewPlan function:

func TestYourFunction(t *testing.T) {
    p := NewPlan(t, "test")
    p.Request("GET", "https://api.sampleapis.com/futurama/episodes").
    Expect().
    Status(200).
    Body(AssertPath(".0.id", func(val int) error {
        if val != 1 {
            return errors.New("expected 1 got " + strconv.Itoa(val))
        }
        return nil
    }))
    p.Run()
}
Adding a body and headers to the request

You can add a body and headers to the request using the Body and Headers methods:

type Episode struct {
    Id int `json:"id"`
    Title string `json:"title"`
}
func TestYourFunction(t *testing.T) {
    newEpisode := Episode{
        Id: 1,
        Title: "Space Pilot 3000",
    }
    p := NewPlan(t, "test")
    p.Request("POST", "https://api.sampleapis.com/futurama/episodes").
    Body(JSON(newEpisode)).
    Headers(map[string]string{
        "Content-Type": "application/json",
    }).
    Expect().
    Status(201)
    p.Run()
}
Utilizing Placeholder

You can utilize placeholders in the URL of the request. Placeholders are defined using curly braces {} in the URL and should be a json path. The value of the placeholder is extracted from the response of the previous request.

func TestYourFunction(t *testing.T) {
    p := NewPlan(t, "test")
    p.Request("GET", "https://api.sampleapis.com/futurama/episodes").
    Expect().
    Status(200)
    // add a second request that uses the response of the first request as a placeholder
    p.Request("GET", "https://api.sampleapis.com/futurama/episodes/{.0.id}"). // the placeholder is the id of the first episode
    Expect().
    Status(200)
    p.Run()
}
Storing and Retrieving Responses

You can store the response of a request and retrieve it later in the test plan. This is useful when you want to use the response of a request in multiple assertions.

func TestYourFunction(t *testing.T) {
    p := NewPlan(t, "test")
    p.Request("GET", "https://api.sampleapis.com/futurama/episodes").
    Store("episodes"). // store the response of this request
    Expect().
    Status(200).
    
    // add a second request that uses the stored response
    p.Request("GET", "https://api.sampleapis.com/futurama/episodes/{.0.id}"). // the placeholder is the id of the first episode
    Expect().
    Status(200).

    // add a third request, this time using the stored response
    p.Request("GET", "https://api.sampleapis.com/futurama/episodes/{episodes.1.id}"). // starting the placeholder with the name of the stored response 
    Expect().
    Status(200)

	// run the test plan
    p.Run()
}

Responses can also be used in Body method with p.Response() or p.ResponseForKey("name"):

See tests for more comprehensive examples.

Contributing

If you find a bug or want to suggest a new feature for testcraft, please open an issue on GitHub or submit a pull request. We welcome contributions from the community.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[T comparable](expected T) func(val T) error

Equal returns an assertion function that checks if the given value is equal to the expected value

func JSON

func JSON(v any) func() io.Reader

JSON helper function to create a JSON request body panics if marshalling fails

func URLValues

func URLValues(val url.Values) func() io.Reader

URLValues helper function to create a URL encoded request body

Types

type AssertionFunc

type AssertionFunc func(body []byte) error

func AssertEqual

func AssertEqual(response any) AssertionFunc

AssertEqual asserts that the response body is equal to the given value It uses reflect.DeepEqual to compare the response body with the given value

func AssertExists

func AssertExists(path string) AssertionFunc

AssertExists asserts that the value at the given path exists in the response body

func AssertPath

func AssertPath[T comparable](path string, assert func(val T) error) AssertionFunc

AssertPath asserts that the value at the given path in the response body satisfies the given assertion the path is a dot separated string representing the path to the value in the response body e.g. "data.user.0.name" will navigate to the first user in the data array and check if the name field satisfies the given assertion

type Expect

type Expect struct {
	// contains filtered or unexported fields
}

func (*Expect) Body

func (e *Expect) Body(assertionFunc AssertionFunc) *Expect

Body sets the expected response body

func (*Expect) Status

func (e *Expect) Status(code int) *Expect

Status sets the expected response code

type Request

type Request struct {
	// contains filtered or unexported fields
}

func (*Request) Body

func (r *Request) Body(body func() io.Reader) *Request

Body sets the request body

func (*Request) Expect

func (r *Request) Expect() *Expect

Expect sets the expectations for the response

func (*Request) Headers

func (r *Request) Headers(headers map[string]string) *Request

Headers sets the request headers

func (*Request) Store

func (r *Request) Store(key string) *Request

Store stores the response body in the response store

type TestPlan

type TestPlan struct {
	// contains filtered or unexported fields
}

func NewPlan

func NewPlan(t *testing.T, name string) *TestPlan

NewPlan creates a new TestPlan The TestPlan is a collection of requests that will be run in order failing to call Run will result in a test failure

func (*TestPlan) Request

func (p *TestPlan) Request(method, url string) *Request

Request creates a new request in the test plan

func (*TestPlan) Response

func (p *TestPlan) Response() []byte

Response returns the last response body

func (*TestPlan) ResponseForKey

func (p *TestPlan) ResponseForKey(key string) []byte

ResponseForKey returns the response body for a given key

func (*TestPlan) Run

func (p *TestPlan) Run()

Run runs the test plan

Jump to

Keyboard shortcuts

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