本文整理汇总了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()
}
示例2: recorderToResponse
func recorderToResponse(recorder *httptest.ResponseRecorder) *http.Response {
return &http.Response{
StatusCode: recorder.Code,
Body: jsh.CreateReadCloser(recorder.Body.Bytes()),
Header: recorder.Header(),
}
}
示例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
}
示例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)
}
}
示例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")
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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())
}
示例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)
}
示例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)
}
示例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())
}
示例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
}
示例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]
}