当前位置: 首页>>代码示例>>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;未经允许,请勿转载。