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


Golang ResponseRecorder.Header方法代码示例

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


在下文中一共展示了ResponseRecorder.Header方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Header

func (m *MockRender) Header() http.Header {

	r := httptest.ResponseRecorder{}
	m.Called()

	return r.Header()
}
开发者ID:masom,项目名称:doorbot,代码行数:7,代码来源:tests.go

示例2: recorderToResponse

func recorderToResponse(recorder *httptest.ResponseRecorder) *http.Response {
	return &http.Response{
		StatusCode: recorder.Code,
		Body:       jsh.CreateReadCloser(recorder.Body.Bytes()),
		Header:     recorder.Header(),
	}
}
开发者ID:samikoskinen,项目名称:go-json-spec-handler,代码行数:7,代码来源:test_util.go

示例3: checkIndex

// checkIndex is like assertIndex but returns an error
func checkIndex(resp *httptest.ResponseRecorder) error {
	header := resp.Header().Get("X-Consul-Index")
	if header == "" || header == "0" {
		return fmt.Errorf("Bad: %v", header)
	}
	return nil
}
开发者ID:hashicorp,项目名称:consul,代码行数:8,代码来源:http_test.go

示例4: testContentType

func testContentType(t *testing.T, r *http.Request, rec *httptest.ResponseRecorder, expected string) {
	ct := rec.Header().Get("content-type")
	if !strings.EqualFold(ct, expected) {
		t.Errorf("Unexpected content-type for %q: %s, expected: %s",
			r.URL.String(), ct, expected)
	}
}
开发者ID:hraban,项目名称:godspeed,代码行数:7,代码来源:godspeed_test.go

示例5: TestHandleFunc

func TestHandleFunc(t *testing.T) {
	wfe := setupWFE(t)
	var mux *http.ServeMux
	var rw *httptest.ResponseRecorder
	var stubCalled bool
	runWrappedHandler := func(req *http.Request, allowed ...string) {
		mux = http.NewServeMux()
		rw = httptest.NewRecorder()
		stubCalled = false
		wfe.HandleFunc(mux, "/test", func(http.ResponseWriter, *http.Request) {
			stubCalled = true
		}, allowed...)
		req.URL = mustParseURL("/test")
		mux.ServeHTTP(rw, req)
	}

	// Plain requests (no CORS)
	type testCase struct {
		allowed       []string
		reqMethod     string
		shouldSucceed bool
	}
	var lastNonce string
	for _, c := range []testCase{
		{[]string{"GET", "POST"}, "GET", true},
		{[]string{"GET", "POST"}, "POST", true},
		{[]string{"GET"}, "", false},
		{[]string{"GET"}, "POST", false},
		{[]string{"GET"}, "OPTIONS", false},     // TODO, #469
		{[]string{"GET"}, "MAKE-COFFEE", false}, // 405, or 418?
	} {
		runWrappedHandler(&http.Request{Method: c.reqMethod}, c.allowed...)
		test.AssertEquals(t, stubCalled, c.shouldSucceed)
		if c.shouldSucceed {
			test.AssertEquals(t, rw.Code, http.StatusOK)
		} else {
			test.AssertEquals(t, rw.Code, http.StatusMethodNotAllowed)
			test.AssertEquals(t, sortHeader(rw.Header().Get("Allow")), strings.Join(c.allowed, ", "))
			test.AssertEquals(t,
				rw.Body.String(),
				`{"type":"urn:acme:error:malformed","detail":"Method not allowed"}`)
		}
		nonce := rw.Header().Get("Replay-Nonce")
		test.AssertNotEquals(t, nonce, lastNonce)
		lastNonce = nonce
	}

	// Disallowed method returns error JSON in body
	runWrappedHandler(&http.Request{Method: "PUT"}, "GET", "POST")
	test.AssertEquals(t, rw.Header().Get("Content-Type"), "application/problem+json")
	test.AssertEquals(t, rw.Body.String(), `{"type":"urn:acme:error:malformed","detail":"Method not allowed"}`)
	test.AssertEquals(t, sortHeader(rw.Header().Get("Allow")), "GET, POST")

	// Disallowed method special case: response to HEAD has got no body
	runWrappedHandler(&http.Request{Method: "HEAD"}, "GET", "POST")
	test.AssertEquals(t, stubCalled, false)
	test.AssertEquals(t, rw.Body.String(), "")
	test.AssertEquals(t, sortHeader(rw.Header().Get("Allow")), "GET, POST")
}
开发者ID:tomclegg,项目名称:boulder,代码行数:59,代码来源:web-front-end_test.go

示例6: ReadHeadersFromResponse

func ReadHeadersFromResponse(writer *httptest.ResponseRecorder) map[string]string {
	headers := map[string]string{}
	for k, v := range writer.Header() {
		log.Println(k, v)
		headers[k] = strings.Join(v, " ")
	}
	return headers
}
开发者ID:dfmonaco,项目名称:yaag,代码行数:8,代码来源:yaagmiddleware.go

示例7: Write

func (s *Stack) Write(response *Response, rec *httptest.ResponseRecorder) {
	for key, values := range rec.Header() {
		for _, value := range values {
			response.header.Add(key, value)
		}
	}
	response.buffer.Write(rec.Body.Bytes())
	response.code = rec.Code
}
开发者ID:dieucao,项目名称:notifications,代码行数:9,代码来源:stack.go

示例8: respFromRecorder

// respFromRecorder builds an http response from a httptest recorder
func respFromRecorder(w *httptest.ResponseRecorder) *http.Response {
	resp := http.Response{}
	resp.StatusCode = w.Code
	resp.Header = w.Header()
	// TODO: fill in the rest of response

	b := w.Body.Bytes()
	resp.Body = ioutil.NopCloser(bytes.NewReader(b))
	return &resp
}
开发者ID:trotha01,项目名称:snowflake,代码行数:11,代码来源:mockflake.go

示例9: assertResponseWithStatusAndMessage

func assertResponseWithStatusAndMessage(t *testing.T, res *httptest.ResponseRecorder, code int, status, message string) {
	var apiMessage responses.APIMessage
	json.NewDecoder(res.Body).Decode(&apiMessage)

	assert.Equal(t, code, res.Code)
	assert.Equal(t, "application/json; charset=UTF-8", res.Header().Get("Content-Type"))

	assert.Equal(t, status, apiMessage.Status)
	assert.Equal(t, message, apiMessage.Message)
}
开发者ID:AntoineAugusti,项目名称:moduluschecking-api,代码行数:10,代码来源:authorized_test.go

示例10: assertHttpMatchRecorder

// Test that the headers and body match.
func assertHttpMatchRecorder(t *testing.T, recorder *httptest.ResponseRecorder,
	expectedStatus int, expectedHeaders http.Header, expectedBody string) {
	assert.Equal(t, expectedStatus, recorder.Code)

	// Verify that headers match. Order shouldn't matter.
	assert.Equal(t, recorder.Header(), expectedHeaders)

	// Convert the body to a string.
	assert.Equal(t, expectedBody, recorder.Body.String())
}
开发者ID:rwl,项目名称:endpoints,代码行数:11,代码来源:utils_test.go

示例11: assertJSONResponse

func assertJSONResponse(t *testing.T, expectedCode int, expectedResponse string, w *httptest.ResponseRecorder) {
	assert.Equal(t, expectedCode, w.Code)
	assert.Equal(t, "application/json", w.Header().Get("content-type"))

	var expected, actual interface{}
	require.NoError(t, json.Unmarshal([]byte(expectedResponse), &expected))
	require.NoError(t, json.Unmarshal(w.Body.Bytes(), &actual))

	assert.Equal(t, expected, actual)
}
开发者ID:tgroshon,项目名称:straitjacket,代码行数:10,代码来源:handlers_test.go

示例12: getIndex

// getIndex parses X-Consul-Index
func getIndex(t *testing.T, resp *httptest.ResponseRecorder) uint64 {
	header := resp.Header().Get("X-Consul-Index")
	if header == "" {
		t.Fatalf("Bad: %v", header)
	}
	val, err := strconv.Atoi(header)
	if err != nil {
		t.Fatalf("Bad: %v", header)
	}
	return uint64(val)
}
开发者ID:hashicorp,项目名称:consul,代码行数:12,代码来源:http_test.go

示例13: copyResponse

// copyResponse copies all relevant info from rec to rw.
func copyResponse(rw http.ResponseWriter, rec *httptest.ResponseRecorder) {

	// copy the headers
	for k, v := range rec.Header() {
		rw.Header()[k] = v
	}
	// copy the code
	rw.WriteHeader(rec.Code)
	// copy the body
	rw.Write(rec.Body.Bytes())
}
开发者ID:fueledbymarvin,项目名称:gocardless,代码行数:12,代码来源:handlers.go

示例14: ReadHeadersFromResponse

func ReadHeadersFromResponse(writer *httptest.ResponseRecorder) (map[string]string, []models.YaagAnnotationHeader) {
	headers := map[string]string{}
	yaagAnnotations := []models.YaagAnnotationHeader{}
	for k, v := range writer.Header() {
		if strings.HasPrefix(k, "Yaag-Annotation") {
			yaagAnnotations = append(yaagAnnotations, annotations.New(k, strings.Join(v, " ")))
		} else {
			headers[k] = strings.Join(v, " ")
		}
	}
	return headers, yaagAnnotations
}
开发者ID:BrianNewsom,项目名称:yaag,代码行数:12,代码来源:yaagmiddleware.go

示例15: Put

func (m mockCacher) Put(key string, r *httptest.ResponseRecorder) *CachedResponse {
	specHeaders := make(map[string]string)
	for k, v := range r.Header() {
		specHeaders[k] = strings.Join(v, ", ")
	}

	m.data[key] = &CachedResponse{
		StatusCode: r.Code,
		Body:       r.Body.Bytes(),
		Headers:    specHeaders,
	}
	return m.data[key]
}
开发者ID:Jinrenjie,项目名称:chameleon,代码行数:13,代码来源:handlers_test.go


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