Skip to content
This repository has been archived by the owner on Jun 2, 2019. It is now read-only.

Latest commit

 

History

History
55 lines (33 loc) · 1.78 KB

README_zh.md

File metadata and controls

55 lines (33 loc) · 1.78 KB

httptest

What we want ?

不扯淡,简化httptest库。

用法

标准httptest库用法可以参考Testing Your (HTTP) Handlers in Go,但是有目共睹,写一个单元测试特别长,所以我们简化了一下。

//一个永远返回400的测试handler
func badHandler(w http.ResponseWriter, r *http.Request) {
	http.Error(w, "not a regular name or password", http.StatusBadRequest)
}

//测试这个handler是否返回400
New("/bad", badHandler, t).Do().CheckCode(http.StatusBadRequest)

//测试他是不是返回200(当然会测试失败)
New("/ok", badHandler, t).Do().CheckCode(http.StatusOK)

//带着header测试
New("/", badHandler, t).Post().AddParams("name", "value1").AddParams("nam22", "value3").Do()

//带着cookie测试,并且判断结果是否包含字符串。
New("/", cookieHandler, t).Get().AddCookies(cookie).Do().CheckBodyContains("testcookievalue")

//获取 *http.ResponseRecorder, 然后自己测试
rr = New("/dump", headerHandler, t).Post().AddParams("name", "value1").Do().GetResponseRecorder()

//获取 cookie, 以供后续测试
rr = New("/dump", headerHandler, t).Post().AddParams("name", "value1").Do().GetCookie()

//给请求加参数,不写默认是GET请求
New("/ok", badHandler, t).AddParams("a", "aa").AddParams("b", "bb").Do().CheckCode(http.StatusOK)

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

//自己定制 http.Request:
New("/bad", badHandler, t).SetRequest(req).Do().CheckCode(http.StatusBadRequest)

//And more in test file and source code.

必须有 .Do(),才能进行请求,不然不会请求。

后续

[] 另外,还有计划对获取到的请求进行json解析……