v0.1.0-rc4 Latest Latest

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

Go to latest
Published: Feb 16, 2015 License: Apache-2.0, Apache-2.0 Imports: 8 Imported by: 0



Package httptest provides utilities for HTTP testing.




View Source
const DefaultRemoteAddr = ""

DefaultRemoteAddr is the default remote address to return in RemoteAddr if an explicit DefaultRemoteAddr isn't set on ResponseRecorder.


This section is empty.


This section is empty.


type ResponseRecorder

type ResponseRecorder struct {
	Code      int           // the HTTP response code from WriteHeader
	HeaderMap http.Header   // the HTTP response headers
	Body      *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to
	Flushed   bool
	// contains filtered or unexported fields

ResponseRecorder is an implementation of http.ResponseWriter that records its mutations for later inspection in tests.

package main

import (

func main() {
	handler := func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "something failed", http.StatusInternalServerError)

	req, err := http.NewRequest("GET", "", nil)
	if err != nil {

	w := httptest.NewRecorder()
	handler(w, req)

	fmt.Printf("%d - %s", w.Code, w.Body.String())

500 - something failed

func NewRecorder

func NewRecorder() *ResponseRecorder

NewRecorder returns an initialized ResponseRecorder.

func (*ResponseRecorder) Flush

func (rw *ResponseRecorder) Flush()

Flush sets rw.Flushed to true.

func (*ResponseRecorder) Header

func (rw *ResponseRecorder) Header() http.Header

Header returns the response headers.

func (*ResponseRecorder) Write

func (rw *ResponseRecorder) Write(buf []byte) (int, error)

Write always succeeds and writes to rw.Body, if not nil.

func (*ResponseRecorder) WriteHeader

func (rw *ResponseRecorder) WriteHeader(code int)

WriteHeader sets rw.Code.

type Server

type Server struct {
	URL      string // base URL of form http://ipaddr:port with no trailing slash
	Listener net.Listener

	// TLS is the optional TLS configuration, populated with a new config
	// after TLS is started. If set on an unstarted server before StartTLS
	// is called, existing fields are copied into the new config.
	TLS *tls.Config

	// Config may be changed after calling NewUnstartedServer and
	// before Start or StartTLS.
	Config *http.Server
	// contains filtered or unexported fields

A Server is an HTTP server listening on a system-chosen port on the local loopback interface, for use in end-to-end HTTP tests.

package main

import (

func main() {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, client")
	defer ts.Close()

	res, err := http.Get(ts.URL)
	if err != nil {
	greeting, err := ioutil.ReadAll(res.Body)
	if err != nil {

	fmt.Printf("%s", greeting)

Hello, client

func NewServer

func NewServer(handler http.Handler) *Server

NewServer starts and returns a new Server. The caller should call Close when finished, to shut it down.

func NewTLSServer

func NewTLSServer(handler http.Handler) *Server

NewTLSServer starts and returns a new Server using TLS. The caller should call Close when finished, to shut it down.

func NewUnstartedServer

func NewUnstartedServer(handler http.Handler) *Server

NewUnstartedServer returns a new Server but doesn't start it.

After changing its configuration, the caller should call Start or StartTLS.

The caller should call Close when finished, to shut it down.

func (*Server) Close

func (s *Server) Close()

Close shuts down the server and blocks until all outstanding requests on this server have completed.

func (*Server) CloseClientConnections

func (s *Server) CloseClientConnections()

CloseClientConnections closes any currently open HTTP connections to the test Server.

func (*Server) Start

func (s *Server) Start()

Start starts a server from NewUnstartedServer.

func (*Server) StartTLS

func (s *Server) StartTLS()

StartTLS starts TLS on a server from NewUnstartedServer.

Jump to

Keyboard shortcuts

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