Skip to content

pcl/httptest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP Test Utilities

This module contains a number of Go HTTP test utilities.

  1. In-process gorilla/mux test harness
  2. Unit testing utilities (assertions etc.)

In-process gorilla/mux test harness

The Gorilla mux package is a powerful framework for building RESTful HTTP services. However, it's not as testable as it could be. In particular, tests written for it must either be written directly against the handler methods themselves, or an HTTP server must be externally brought online and configured for the test environment. The muxrunner package provides some functions to write endpoint-oriented tests that can run in a variety of different contexts, including in-process testing during development and remote testing for pre-deploy or ongoing system verification.

(Currently, only the in-process variant is implemented. Next step: implement the remote testing function, and environment variables to detect the appropriate current context.)

Usage

First, set up your handlers as you would normally:

func init() {
    r := mux.NewRouter()
    r.HandleFunc("/", HomeHandler)
    http.Handle("/", r)
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "HomeHandler")
}

Next, write a test that creates an in-process Client and executes a request against it:

func TestHomeHandler() {
    runner := muxrunner.InProcessClient()
    req, _ := http.NewRequest("GET", "/path/to/handler", nil)
    response, _ := runner.Do(req)
    testutil.AssertResponseStatus(t, 200, response)
    testutil.AssertResponseBody(t, "HomeHandler", response)
}

HTTP testing utilities

This package provides some useful functions for writing HTTP-oriented tests, including response body and header assertions. See the usage for muxrunner above for an example.

Development

To build: GOROOT=/usr/local/share/go-app-engine-64/goroot GOPATH=/Users/pcl/.go:/Users/pcl/src/go /usr/local/share/go-app-engine-64/goroot/bin/goapp build github.com/pcl/httptest/aeutil github.com/pcl/httptest/aetestutil github.com/pcl/httptest/muxrunner github.com/pcl/httptest/test github.com/pcl/httptest/testutil

To test: GOPATH=/Users/pcl/.go:/Users/pcl/src/go go test github.com/pcl/httptest/test

About

HTTP test utilities for Go programming

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages