testmux

package module
v0.0.0-...-327b93f Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

README

NOTE

This repo is no longer being maintained. Users are welcome to fork it, but we make no warranty of its functionality.

testmux

Circle CI GoDoc

testmux provides a testing-only HTTP request multiplexer that can validate that requests are received in a specific order.

Read the full documentation here: http://godoc.org/github.com/CenturyLinkLabs/testmux

Documentation

Overview

Package testmux provides an HTTP request multiplexer that can be used when writing tests where is is necessary to validate that requests are received in a specific order.

Any number of routes can be registered with the testmux.Router. Each route is described by an HTTP method and URL path along with the handler that should be invoked for a matching request.

Every time that a new request is dipatched via the ServeHTTP method, the router will track whether the request matches one of the registered handlers and if it was received in the correct sequence. There are two Assert methods that can be used to validate that all of the registered routes were exercised. AssertVisited will check for any requests received for unregistered routes and any registered routes that were never requested. AssertVisitedInOrder will do the same checks as AssertVisited while also verifying that requests were received in the same order they were originally registered.

func TestRequests(t *testing.T) {
	// Instantiate testmux router and register new routes
	router := &testmux.Router{}
	router.RegisterResp("GET", "/foo", 200, "Hello")
	router.RegisterResp("GET", "/bar", 200, "Bonjour")

	// Set-up test server
	server := httptest.NewServer(router)

	// Make requests
	http.Get(server.URL+"/foo")
	http.Get(server.URL+"/bar")

	// Assert that all registered routes visited in the correct order
	router.AssertVisitedInOrder(t)
}

When registering routes, user's can use either the RegisterResp method which accepts a static response code and body to be returned for a matching request, or the RegisterFunc method which takes a traditional handler function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Router

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

Router registers routes to be matched and dispatches the associated handler. It will track handled requests to ensure that all registered routes are invoked and that they are received in the same order in which they were originally registered.

It implements the http.Hander interface.

func (*Router) AssertVisited

func (r *Router) AssertVisited(t *testing.T) bool

AssertVisited asserts that all of the registered routes were visited. Conditions that will cause the assertion to fail include: requests received for unregistered routes, and routes registered but never requested.

Returns whether the assertion was successful (true) or not (false).

func (*Router) AssertVisitedInOrder

func (r *Router) AssertVisitedInOrder(t *testing.T) bool

AssertVisited asserts that all of the registered routes were visited in the correct order. Conditions that will cause the assertion to fail include: requests received out of order, requests received for unregistered routes, and routes registered but never requested.

Returns whether the assertion was successful (true) or not (false).

func (*Router) RegisterFunc

func (r *Router) RegisterFunc(method, path string, handler func(http.ResponseWriter, *http.Request))

RegisterFunc registers a handler function for the given request method and path.

func (*Router) RegisterResp

func (r *Router) RegisterResp(method, path string, status int, body string)

RegisterResp registers a static status code and body string to be returned for the given request method and path.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP dispatches the handler registered in the matched route and tracks whether the route was requested in the correct order.

Jump to

Keyboard shortcuts

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