Skip to content
This is a wrapper around net/http/httptest to make unit test easy.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml
LICENSE
Makefile
README.md
README_zh.md
httptest.go
httptest_test.go

README.md

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.
You can’t perform that action at this time.