test

package
v0.0.0-...-bcc9f66 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2015 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Utility functions to help writing tests for a Go-Json-Rest app

Go comes with net/http/httptest to help writing test for an http server. When this http server implements a JSON REST API, some basic checks end up to be always the same. This test package tries to save some typing by providing helpers for this particular use case.

package main

import (
	"github.com/ant0ine/go-json-rest/rest/test"
	"testing"
)

func TestSimpleRequest(t *testing.T) {
	handler := ResourceHandler{}
	handler.SetRoutes(
		&Route{"GET", "/r",
			func(w ResponseWriter, r *Request) {
				w.WriteJson(map[string]string{"Id": "123"})
			},
		},
	)
	recorded := test.RunRequest(t, &handler,
		test.MakeSimpleRequest("GET", "http://1.2.3.4/r", nil))
	recorded.CodeIs(200)
	recorded.ContentTypeIsJson()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BodyIs

func BodyIs(t *testing.T, r *httptest.ResponseRecorder, expectedBody string)

func CodeIs

func CodeIs(t *testing.T, r *httptest.ResponseRecorder, expectedCode int)

CodeIs compares the rescorded status code

func ContentEncodingIsGzip

func ContentEncodingIsGzip(t *testing.T, r *httptest.ResponseRecorder)

func ContentTypeIsJson

func ContentTypeIsJson(t *testing.T, r *httptest.ResponseRecorder)

func DecodeJsonPayload

func DecodeJsonPayload(r *httptest.ResponseRecorder, v interface{}) error

func HeaderIs

func HeaderIs(t *testing.T, r *httptest.ResponseRecorder, headerKey, expectedValue string)

HeaderIs tests the first value for the given headerKey

func MakeSimpleRequest

func MakeSimpleRequest(method string, urlStr string, payload interface{}) *http.Request

MakeSimpleRequest returns a http.Request. The returned request object can be further prepared by adding headers and query string parmaters, for instance.

Types

type Recorded

type Recorded struct {
	T        *testing.T
	Recorder *httptest.ResponseRecorder
}

func RunRequest

func RunRequest(t *testing.T, handler http.Handler, request *http.Request) *Recorded

RunRequest runs a HTTP request through the given handler

func (*Recorded) BodyIs

func (rd *Recorded) BodyIs(expectedBody string)

func (*Recorded) CodeIs

func (rd *Recorded) CodeIs(expectedCode int)

func (*Recorded) ContentEncodingIsGzip

func (rd *Recorded) ContentEncodingIsGzip()

func (*Recorded) ContentTypeIsJson

func (rd *Recorded) ContentTypeIsJson()

func (*Recorded) DecodeJsonPayload

func (rd *Recorded) DecodeJsonPayload(v interface{}) error

func (*Recorded) HeaderIs

func (rd *Recorded) HeaderIs(headerKey, expectedValue string)

Jump to

Keyboard shortcuts

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