package module
Version: v0.0.0-...-8e4c9d1 Latest Latest

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

Go to latest
Published: Sep 3, 2014 License: MIT Imports: 6 Imported by: 0




go get bitbucket.org/coinpass/testflight
import "bitbucket.org/coinpass/testflight"


testflight makes it simple to test your http servers in Go. Suppose you're using pat to create a simple http handler, like so:

func Handler() http.Handler {
	m := pat.New()

	m.Get("/hello/:name", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		io.WriteString(w, "hello, "+req.URL.Query().Get(":name"))

	m.Post("/post/form", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		name := req.Form.Get("name")
		io.WriteString(w, name+" created")

	return m

Let's use testflight to test our handler. Keep in mind that testflight is doing full-stack http tests. We're also using assert for test assertions.

func TestGet(t *testing.T) {
	testflight.WithServer(Handler(), func(r *testflight.Requester) {
		response := r.Get("/hello/drew")

		assert.Equal(t, 200, response.StatusCode)
		assert.Equal(t, "hello, drew", response.Body)

func TestPostWithForm(t *testing.T) {
	testflight.WithServer(Handler(), func(r *testflight.Requester) {
		response := r.Post("/post/form", testflight.FORM_ENCODED, "name=Drew")

		assert.Equal(t, 201, response.StatusCode)
		assert.Equal(t, "Drew created", response.Body)

The testflight.Requester class has the following methods: Get, Post, Put, Delete and Do. Do accepts an *http.Request for times when you need more explicit control of our request. See testflight_test.go for more usage information.

Testing Websockets

Testflight also allows you to perform full-stack testing of websockets. You'll want to import both the testflight and testflight/ws packages.

import (

Now, let's make a handler with a websocket route.

func Handler() http.Handler {
	mux := http.NewServeMux()

	mux.Handle("/websocket", websocket.Handler(func(ws *websocket.Conn) {
		var name string
		websocket.Message.Receive(ws, &name)
		websocket.Message.Send(ws, "Hello, "+name)

	return mux

Finally, let's write the test.

func TestWebSocket(t *testing.T) {
    testflight.WithServer(Handler(), func(r *testflight.Requester) {
        connection := ws.Connect(r, "/websocket")

        message, _ := connection.ReceiveMessage()
        assert.Equal(t, "Hello, Drew", message)


First, run the tests.

mkdir testflight
cd testflight

export GOPATH=`pwd`

go get bitbucket.org/coinpass/testflight
go get code.google.com/p/go.net/websocket
go get github.com/kr/pretty
go get github.com/bmizerany/assert
go get github.com/bmizerany/pat

go test bitbucket.org/coinpass/testflight

Now write new tests, fix them and send me a pull request!




View Source
const (
	JSON         = "application/json"
	FORM_ENCODED = "application/x-www-form-urlencoded"


View Source
var RedirectionError = errors.New("Redirection Error")


func WithServer

func WithServer(handler http.Handler, context func(*Requester))


type Requester

type Requester struct {
	// contains filtered or unexported fields

func (*Requester) Delete

func (requester *Requester) Delete(route, contentType string, params map[string]string) *Response

func (*Requester) Do

func (requester *Requester) Do(request *http.Request) *Response

func (*Requester) Get

func (requester *Requester) Get(route string) *Response

func (*Requester) GetWithAuthorization

func (requester *Requester) GetWithAuthorization(route, authorization string) *Response

func (*Requester) GetWithCookie

func (requester *Requester) GetWithCookie(route, cookie string) *Response

func (*Requester) Options

func (requester *Requester) Options(route string) *Response

func (*Requester) OptionsWithAccessControRequestHeaders

func (requester *Requester) OptionsWithAccessControRequestHeaders(route, acrh string) *Response

func (*Requester) Post

func (requester *Requester) Post(route, contentType string, params map[string]string) *Response

func (*Requester) PostWithAuthorization

func (requester *Requester) PostWithAuthorization(route, authorization, contentType string, params map[string]string) *Response

func (*Requester) Put

func (requester *Requester) Put(route, contentType string, params map[string]string) *Response

func (*Requester) Url

func (requester *Requester) Url(route string) string

type Response

type Response struct {
	Body        string
	RawResponse *http.Response
	StatusCode  int


Path Synopsis

Jump to

Keyboard shortcuts

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