blackbox

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2018 License: MIT Imports: 11 Imported by: 0

README

GoDoc Badge

Baby Blackbox

Blackbox testing for Go JSON APIs. Currently, it supports the Go standard library’s Multiplexer (http.ServeMux) and the Goji multiplexer (goji.Mux). If there's another multiplexer you'd like it to support let us know.

Example


package main_test

import (
    "testing"
    "github.com/magicnumbers/baby-blackbox"
    app "."
)

type user struct {
    ID int      `json:"id,omitempty"`
    Name string `json:"name"`
}

type apiError {
    Code int       `json:"code"`
    Message string `json:"message"`
}

func TestMain(m *testing.M) {
    os.Exit(m.Run())
}

func TestStuff(t *testing.T) {

    // Create a blackbox testing thing from your application's multiplexer
    api := blackbox.New(t, app.GetMux())

    // The payload we'll send in the next request
    u := user{Name: "Frankie"}

    // Create a user expecting to get an ID back in a JSON object. We assert
    // that we want a 201 Created status code.
    api.Request("POST", "/user", u).
        Created().
        JSON(&u)

    // Make sure we got an ID in the response
    if u.ID == 0 {
        t.Error("expected to receive an ID, but we did not")
    }

    var apiErr apiError

    // Check another route, expecting a 401 Unauthorized error
    api.Request("GET", "/cats", nil).
        Status(http.StatusUnauthorized).
        JSON(&apiErr)

    if err.Code != 21 {
        t.Errorf("thought we were gonna get error code 21, instead got %d", err.Code)
    }
}

For an example in the context of a more complete application, see the example directory.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APITest

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

APITest is a helper for running a series of API tests. Initialize it once with New (or NewWithGoji) then call `Request` to issue API requests

func New

func New(t *testing.T, mux *http.ServeMux) APITest

New instantiates a new API test object with a standard http.ServeMux

func NewWithGoji

func NewWithGoji(t *testing.T, mux *goji.Mux) APITest

NewWithGoji instantiates a new API test object with a goji.Mux

func (*APITest) Request

func (a *APITest) Request(method string, path string, body interface{}) Response

Request makes a call to a REST API. It returns a Response struct which contains information about the response to the request, as well as methods for analyzing and working with the request data. See Request.

type Response

type Response struct {
	StatusCode int
	StatusText string
	Body       []byte
	// contains filtered or unexported fields
}

Response contains helper members and methods for working with API tests

func (Response) Cool

func (a Response) Cool() Response

Cool checks weather HTTP status code is in the 200s-300s range (i.e. not an error)

func (Response) Created

func (a Response) Created() Response

Created is a shortcut for checking for an HTTP 201 response

func (Response) InternalServerError

func (a Response) InternalServerError() Response

InternalServerError is a shortcut for checking for an HTTP 500 response

func (Response) JSON

func (a Response) JSON(i interface{}) Response

JSON decodes the request body into the given interface

func (Response) NoContent

func (a Response) NoContent() Response

NoContent is a shortcut for checking for an HTTP 204 response

func (Response) NotFound

func (a Response) NotFound() Response

NotFound is a shortcut for checking for an HTTP 404 response

func (Response) OK

func (a Response) OK() Response

OK is a shortcut for checking for an HTTP 200 response

func (Response) Status

func (a Response) Status(code int) Response

Status checks that the HTTP status code matches an expected one

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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