httptest

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2017 License: Apache-2.0 Imports: 5 Imported by: 0

README

httptest

中文文档

GoDoc Build Status Go Report Card

What we want ?

In development we found the advantages of unit tests, and golang it self provides good test tools.

However, some test tools are too board, we have to add a layer for using it.

As well as the gorequest does to http.Request, we add a layer on httptest to make http unit test easy.

How to use it?

As the Testing Your (HTTP) Handlers in Go says, write a http unit for a http handler is a boring work.

We just simplify it into some several lines:

//badHandler is a handler always returns 400
func badHandler(w http.ResponseWriter, r *http.Request) {
	http.Error(w, "not a regular name or password", http.StatusBadRequest)
}

//We want to test if it returns 400, we could write:
New("/bad", badHandler, t).Do().CheckCode(http.StatusBadRequest)

//So the 200 is as follows:(this is always fail)
New("/ok", badHandler, t).Do().CheckCode(http.StatusOK)

//Add header to request, and just do a request, using POST:
New("/", badHandler, t).Post().AddParams("name", "value1").AddParams("nam22", "value3").Do()

//Add cookie to request, and test if the response body contains a certain string:
New("/", cookieHandler, t).Get().AddCookies(cookie).Do().CheckBodyContains("testcookievalue")

//Just get the *http.ResponseRecorder, do every thing your self.
rr = New("/dump", headerHandler, t).Post().AddParams("name", "value1").Do().GetResponseRecorder()

//Get the cookie, for further testing
cookie = New("/dump", headerHandler, t).Post().AddParams("name", "value1").Do().GetCookie()

//We forget to add parameter to request
New("/ok", badHandler, t).AddParams("a", "aa").AddParams("b", "bb").Do().CheckCode(http.StatusOK)

//Use http basic auth:
New("/bad", badHandler, t).SetBasicAuth(username, password).Do().CheckCode(http.StatusBadRequest)

//Use your self-defined http.Request:
New("/bad", badHandler, t).SetRequest(req).Do().CheckCode(http.StatusBadRequest)

//And more in test file and source code.

Don't forget .Do().

What we did and roadmap?
  • Add common layer for request.
  • Add common header, parameter for request.
  • Do wrapper on httptest.ResponseRecorder
  • Do better wrapper on body test, such as json.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type T

type T struct {
	// contains filtered or unexported fields
}

T is a struct used of writing tests, contains request information.

func New

func New(path string, f func(http.ResponseWriter, *http.Request), t *testing.T) *T

New is a function create a new http request, with the url and function being tested .

func (*T) AddCookies

func (t *T) AddCookies(c ...*http.Cookie) *T

AddCookies adds cookie(s) to the request.

func (*T) AddParams

func (t *T) AddParams(k, v string) *T

AddParams add parameters to request. In get request, the parameters are encoded in url.Values(url) In other requests, the parameters are encoded in request body.

func (*T) CheckBodyContains

func (t *T) CheckBodyContains(want string) *T

CheckBodyContains checks whether the body contains a certain string.

func (*T) CheckCode

func (t *T) CheckCode(code int) *T

CheckCode checks whether the response code equals the expected.

func (*T) CheckHeader

func (t *T) CheckHeader(name, want string) *T

CheckHeader checks whether the header value equals the expected.

func (*T) Delete

func (t *T) Delete() *T

Delete means the method is post.

func (*T) Do

func (t *T) Do() *T

Do just make a request. CheckCode, CheckHeader, BodyContains and Body rely on Do. Just do it.

func (*T) Get

func (t *T) Get() *T

Get means the method is post.

func (*T) GetBody

func (t *T) GetBody() string

GetBody returns the pure response body.

func (*T) GetCookies

func (t *T) GetCookies() []*http.Cookie

GetCookies returns the cookies that the test handler returns.

func (*T) GetResponseRecorder

func (t *T) GetResponseRecorder() *httptest.ResponseRecorder

GetResponseRecorder returns the origin *httptest.ResponseRecorder

func (*T) Head

func (t *T) Head() *T

Head means the method is post.

func (*T) Patch

func (t *T) Patch() *T

Patch means the method is post.

func (*T) Post

func (t *T) Post() *T

Post means the method is post. Default ContentType of POST is application/x-www-form-urlencoded.

func (*T) Put

func (t *T) Put() *T

Put means the method is post.

func (*T) SetBasicAuth

func (t *T) SetBasicAuth(username, password string) *T

SetBasicAuth set the request to use basic http auth.

func (*T) SetContentType

func (t *T) SetContentType(contentType string) *T

SetContentType is used less except doing post. Default content-type of a post request is application/x-www-form-urlencoded.

func (*T) SetContentTypeFormUrlencoded

func (t *T) SetContentTypeFormUrlencoded() *T

SetContentTypeFormUrlencoded only works for post.

func (*T) SetContentTypeMultipart

func (t *T) SetContentTypeMultipart() *T

SetContentTypeMultipart only works for post.

func (*T) SetRequest

func (t *T) SetRequest(r *http.Request) *T

SetRequest is to set your custom *http.Request to the test. Deafult method is GET, Default ContentType of POST is application/x-www-form-urlencoded.

Jump to

Keyboard shortcuts

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