v1.0.2 Latest Latest

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

Go to latest
Published: Oct 1, 2018 License: MIT Imports: 9 Imported by: 0



Package fclient provides a testing client for http handlers.

To use it, you should write a testable app. You need to pass an object which implements http.Handler (has a ServeHTTP method) to the client

package app_test

import (


type MyApp struct{}

func (app MyApp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte(`{"foo": "bar"}`))

func Test_hello(t *testing.T) {
  app := MyApp{}
  cl := fclient.New(t, app)


  // using response directly
  res := cl.Get("/hello")

You can also transform a simple function into http.Handler interface using http.HandlerFunc adapter

fn := func(w http.ResponseWriter, r *http.Request) {}
cl := fclient.New(t, http.HandlerFunc(fn))
// ...



This section is empty.


This section is empty.


This section is empty.


type Client

type Client struct {

	// handler is an instance of http.Handler
	Handler http.Handler

	// DefaultHeaders is a map for default headers
	DefaultHeaders map[string]string

	// Jar holds cookies. Can be set to nil to turn cookies off
	Jar http.CookieJar
	// contains filtered or unexported fields

Client is a struct which holds current test, LastResponse and LastRequest instances

func New

func New(t test, handler http.Handler) *Client

New builds a new http testing client

func (*Client) Do

func (cl *Client) Do(req *http.Request) *Response

Do invokes the handler, passing a given argument as a request, and stores cookies from the response in Jar. A ".Domain" field of every cookie will be cleared

func (*Client) Get

func (cl *Client) Get(path string) *Response

Get makes a GET Request with nil body

func (*Client) NewRequest

func (cl *Client) NewRequest(method, path string, body io.Reader) *http.Request

NewRequest creates a new request (r) by httptest.NewRequest, then fills r.Headers from cl.DefaultHeaders, then fills cookies from cl.Jar and returns r

func (*Client) Post

func (cl *Client) Post(path string, body interface{}) *Response

Post makes a POST Request

type Response

type Response struct {
	// contains filtered or unexported fields

A Response represents the response from an HTTP request. It also inherits all methods from *httptest.ResponseRecorder

func NewResponse

func NewResponse(t test) *Response

NewResponse returns a Response object with default ResponseRecorder

func (*Response) BodyContains

func (resp *Response) BodyContains(substr string) *Response

BodyContains checks if a response body contains a given string

func (*Response) BodyEq

func (resp *Response) BodyEq(expected interface{}) *Response

BodyEq checks if a response body is equal to the given argument. Accepts []byte or string

func (*Response) CodeEq

func (resp *Response) CodeEq(expected int) *Response

CodeEq checks if a response code is equal to

func (*Response) HeaderEq

func (resp *Response) HeaderEq(key, value string) *Response

HeaderEq checks if the first http header with given name is equal to the given value

func (*Response) JSONEq

func (resp *Response) JSONEq(expected interface{}) *Response

JSONEq checks if given argument is equal to response JSON. Argument can be a string, in which case it will be used as a raw JSON, or other kind, in which case it will be transformed to JSON

Jump to

Keyboard shortcuts

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