示例#1
0
func TestHttpErrorHandler(t *testing.T) {
	delegate := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		panic(ghttp.NewHttpError(errors.New("i/o timeout"), http.StatusRequestTimeout))
	})
	h := Decorate(nil, nil, delegate)

	ts := httptest.NewServer(h)
	defer ts.Close()

	req, err := http.NewRequest("POST", ts.URL, bytes.NewReader([]byte("blah")))
	panicIf(err)
	res, err := http.DefaultClient.Do(req)
	panicIf(err)

	if res.StatusCode != http.StatusRequestTimeout {
		t.Errorf("Expected %d, got %d", http.StatusRequestTimeout, res.StatusCode)
	}
}
示例#2
0
// BodyData returns the full request body
func (r *Request) BodyData() []byte {
	var err error
	if !r.bodyRead {
		if r.Body != nil {
			r.bodyData, err = ioutil.ReadAll(r.Body)
			if err != nil {
				// catch i/o timeout errors
				neterr, isNetError := err.(net.Error)
				if isNetError && neterr.Timeout() {
					panic(ghttp.NewHttpError(err, http.StatusRequestTimeout))
				} else {
					panic(err)
				}
			}
		}
		r.bodyRead = true
	}

	return r.bodyData
}