本文整理匯總了Golang中net/http/httptest.ResponseRecorder.Code方法的典型用法代碼示例。如果您正苦於以下問題:Golang ResponseRecorder.Code方法的具體用法?Golang ResponseRecorder.Code怎麽用?Golang ResponseRecorder.Code使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net/http/httptest.ResponseRecorder
的用法示例。
在下文中一共展示了ResponseRecorder.Code方法的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
}