當前位置: 首頁>>代碼示例>>Golang>>正文


Golang httptest.ResponseRecorder類代碼示例

本文整理匯總了Golang中net/http/httptest.ResponseRecorder的典型用法代碼示例。如果您正苦於以下問題:Golang ResponseRecorder類的具體用法?Golang ResponseRecorder怎麽用?Golang ResponseRecorder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了ResponseRecorder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: 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

示例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: Header

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

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

	return r.Header()
}
開發者ID:masom,項目名稱:doorbot,代碼行數:7,代碼來源:tests.go

示例5: 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

示例6: writeRecorder

// Write the contents of a ResponseRecorder to a ResponseWriter
func writeRecorder(w http.ResponseWriter, recorder *httptest.ResponseRecorder) {
	for key, values := range recorder.HeaderMap {
		for _, value := range values {
			w.Header().Set(key, value)
		}
	}
	recorder.Body.WriteTo(w)
	recorder.Flush()
}
開發者ID:jeraldrich,項目名稱:algernon,代碼行數:10,代碼來源:prettyerror.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: 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

示例9: 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

示例10: 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

示例11: 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

示例12: 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

示例13: DoRequest

// DoRequest invokes a request on the given handler with the given
// parameters.
func DoRequest(c *gc.C, p DoRequestParams) *httptest.ResponseRecorder {
	if p.Method == "" {
		p.Method = "GET"
	}
	if p.Do == nil {
		p.Do = http.DefaultClient.Do
	}
	srv := httptest.NewServer(p.Handler)
	defer srv.Close()

	if p.JSONBody != nil {
		data, err := json.Marshal(p.JSONBody)
		c.Assert(err, jc.ErrorIsNil)
		p.Body = bytes.NewReader(data)
	}
	// Note: we avoid NewRequest's odious reader wrapping by using
	// a custom nopCloser function.
	req, err := http.NewRequest(p.Method, srv.URL+p.URL, nopCloser(p.Body))
	c.Assert(err, jc.ErrorIsNil)
	if p.JSONBody != nil {
		req.Header.Set("Content-Type", "application/json")
	}
	for key, val := range p.Header {
		req.Header[key] = val
	}
	if p.ContentLength != 0 {
		req.ContentLength = p.ContentLength
	} else {
		req.ContentLength = bodyContentLength(p.Body)
	}
	if p.Username != "" || p.Password != "" {
		req.SetBasicAuth(p.Username, p.Password)
	}
	for _, cookie := range p.Cookies {
		req.AddCookie(cookie)
	}
	resp, err := p.Do(req)
	if p.ExpectError != "" {
		c.Assert(err, gc.ErrorMatches, p.ExpectError)
		return nil
	}
	c.Assert(err, jc.ErrorIsNil)
	defer resp.Body.Close()

	// TODO(rog) don't return a ResponseRecorder because we're not actually
	// using httptest.NewRecorder ?
	var rec httptest.ResponseRecorder
	rec.HeaderMap = resp.Header
	rec.Code = resp.StatusCode
	rec.Body = new(bytes.Buffer)
	_, err = io.Copy(rec.Body, resp.Body)
	c.Assert(err, jc.ErrorIsNil)
	return &rec
}
開發者ID:claudiu-coblis,項目名稱:testing,代碼行數:56,代碼來源:http.go

示例14: 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

示例15: 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


注:本文中的net/http/httptest.ResponseRecorder類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。