当前位置: 首页>>代码示例>>Golang>>正文


Golang ResponseRecorder.WriteHeader方法代码示例

本文整理汇总了Golang中net/http/httptest.ResponseRecorder.WriteHeader方法的典型用法代码示例。如果您正苦于以下问题:Golang ResponseRecorder.WriteHeader方法的具体用法?Golang ResponseRecorder.WriteHeader怎么用?Golang ResponseRecorder.WriteHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net/http/httptest.ResponseRecorder的用法示例。


在下文中一共展示了ResponseRecorder.WriteHeader方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
	}
}
开发者ID:SpectoLabs,项目名称:hoverfly,代码行数:48,代码来源:bear-tests_test.go

示例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)
	}
}
开发者ID:SpectoLabs,项目名称:hoverfly,代码行数:42,代码来源:bear-tests_test.go

示例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)
	}
}
开发者ID:SpectoLabs,项目名称:hoverfly,代码行数:22,代码来源:bear-tests_test.go

示例4: middleware

func (suite *TestSuite) middleware(w *httptest.ResponseRecorder, r *http.Request) {
	suite.authenticator.JWTHandlerFunc(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	})).ServeHTTP(w, r)

}
开发者ID:clawio,项目名称:clawiod,代码行数:6,代码来源:lib_test.go


注:本文中的net/http/httptest.ResponseRecorder.WriteHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。