本文整理汇总了Golang中net/http/httptest.ResponseRecorder.HeaderMap方法的典型用法代码示例。如果您正苦于以下问题:Golang ResponseRecorder.HeaderMap方法的具体用法?Golang ResponseRecorder.HeaderMap怎么用?Golang ResponseRecorder.HeaderMap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http/httptest.ResponseRecorder
的用法示例。
在下文中一共展示了ResponseRecorder.HeaderMap方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: DoRequest
// DoRequest is the same as Do except that it returns
// an httptest.ResponseRecorder instead of an http.Response.
// This function exists for backward compatibility reasons.
func DoRequest(c *gc.C, p DoRequestParams) *httptest.ResponseRecorder {
resp := Do(c, p)
if p.ExpectError != "" {
return nil
}
defer resp.Body.Close()
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
}