本文整理汇总了Golang中net/http/httptest.ResponseRecorder.Write方法的典型用法代码示例。如果您正苦于以下问题:Golang ResponseRecorder.Write方法的具体用法?Golang ResponseRecorder.Write怎么用?Golang ResponseRecorder.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http/httptest.ResponseRecorder
的用法示例。
在下文中一共展示了ResponseRecorder.Write方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestWildcardCompeting
func TestWildcardCompeting(t *testing.T) {
var (
method string = "GET"
mux *Mux = New()
patternOne string = "/*"
pathOne string = "/bar/baz"
wantOne string = "bar/baz"
patternTwo string = "/foo/*"
pathTwo string = "/foo/baz"
wantTwo string = "baz"
patternThree string = "/foo/bar/*"
pathThree string = "/foo/bar/bar/baz"
wantThree string = "bar/baz"
req *http.Request
res *httptest.ResponseRecorder
)
handler := func(res http.ResponseWriter, _ *http.Request, ctx *Context) {
res.WriteHeader(http.StatusOK)
res.Write([]byte(ctx.Params["*"]))
}
mux.On(method, patternOne, handler)
mux.On(method, patternTwo, handler)
mux.On(method, patternThree, handler)
req, _ = http.NewRequest(method, pathOne, nil)
res = httptest.NewRecorder()
mux.ServeHTTP(res, req)
if body := res.Body.String(); body != wantOne {
t.Errorf(
"%s %s (%s) got %s want %s",
method, pathOne, patternOne, body, wantOne)
}
req, _ = http.NewRequest(method, pathTwo, nil)
res = httptest.NewRecorder()
mux.ServeHTTP(res, req)
if body := res.Body.String(); body != wantTwo {
t.Errorf(
"%s %s (%s) got %s want %s",
method, pathTwo, patternTwo, body, wantTwo)
}
req, _ = http.NewRequest(method, pathThree, nil)
res = httptest.NewRecorder()
mux.ServeHTTP(res, req)
if body := res.Body.String(); body != wantThree {
t.Errorf(
"%s %s (%s) got %s want %s",
method, pathThree, patternThree, body, wantThree)
}
}
示例2: TestNotFoundCustom
func TestNotFoundCustom(t *testing.T) {
var (
method string = "GET"
mux *Mux = New()
pathFound string = "/foo/bar"
pathLost string = "/foo/bar/baz"
patternFound string = "/foo/bar"
patternLost string = "/*"
req *http.Request
res *httptest.ResponseRecorder
wantFound int = http.StatusOK
wantLost int = http.StatusTeapot
)
handlerFound := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
res.WriteHeader(http.StatusOK)
res.Write([]byte("found!"))
})
handlerLost := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
res.WriteHeader(http.StatusTeapot)
res.Write([]byte("not found!"))
})
// Test found to make sure wildcard doesn't overtake everything.
req, _ = http.NewRequest(method, pathFound, nil)
res = httptest.NewRecorder()
mux.On(method, patternFound, handlerFound)
mux.ServeHTTP(res, req)
if res.Code != wantFound {
t.Errorf(
"%s %s (%s) got %d want %d",
method, pathFound, patternFound, res.Code, wantFound)
}
// Test lost to make sure wildcard can gets non-pattern-matching paths.
req, _ = http.NewRequest(method, pathLost, nil)
res = httptest.NewRecorder()
mux.On(method, patternLost, handlerLost)
mux.ServeHTTP(res, req)
if res.Code != wantLost {
t.Errorf(
"%s %s (%s) got %d want %d",
method, pathLost, patternLost, res.Code, wantLost)
}
}
示例3: TestWildcardParams
func TestWildcardParams(t *testing.T) {
var (
method string = "GET"
mux *Mux = New()
pattern string = "/foo/{bar}/*"
path string = "/foo/ABC/baz"
want string = "ABC"
req *http.Request
res *httptest.ResponseRecorder
)
handler := func(res http.ResponseWriter, _ *http.Request, ctx *Context) {
res.WriteHeader(http.StatusOK)
res.Write([]byte(ctx.Params["bar"]))
}
mux.On(method, pattern, handler)
req, _ = http.NewRequest(method, path, nil)
res = httptest.NewRecorder()
mux.ServeHTTP(res, req)
if body := res.Body.String(); body != want {
t.Errorf("%s %s (%s) got %s want %s", method, path, pattern, body, want)
}
}