本文整理汇总了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)
}
}
示例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: Header
func (m *MockRender) Header() http.Header {
r := httptest.ResponseRecorder{}
m.Called()
return r.Header()
}
示例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
}
示例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()
}
示例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: 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())
}
示例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)
}
示例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
}
示例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)
}
示例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())
}
示例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
}
示例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)
}
示例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
}