diff --git a/client.go b/client.go index cf81998..a4abbc3 100644 --- a/client.go +++ b/client.go @@ -98,7 +98,7 @@ func (client *Client) Handler(h http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { defer func() { if err := recover(); err != nil { - client.Notify(newError(err, 2), Params(r.Form), getCGIData(r), *r.URL) + client.Notify(newError(err, 2), r) panic(err) } }() diff --git a/honeybadger_test.go b/honeybadger_test.go index 2388c1b..cce546e 100644 --- a/honeybadger_test.go +++ b/honeybadger_test.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "net/url" "reflect" "testing" @@ -195,6 +196,49 @@ func TestNotifyWithFingerprint(t *testing.T) { } } +func TestNotifyWithRequest(t *testing.T) { + setup(t) + defer teardown() + + // Make sure nil request doesn't panic + var req *http.Request + Notify("Cobras!", req) + + // Test actual request + req = httptest.NewRequest("GET", "/fail", nil) + req.Header.Set("Accept", "application/test-data") + req.Form = url.Values{"fKey": {"fValue"}} + Notify("Cobras!", req) + Flush() + + if !testRequestCount(t, 2) { + return + } + + payload := requests[1].decodeJSON() + request_payload, _ := payload["request"].(map[string]interface{}) + + if !testNoticePayload(t, payload) { + return + } + + if url, _ := request_payload["url"].(string); url != "/fail" { + t.Errorf("Request URL should be extracted. expected=%v actual=%#v.", "/fail", url) + return + } + + cgi, _ := request_payload["cgi_data"].(map[string]interface{}) + if len(cgi) != 1 || cgi["HTTP_ACCEPT"] != "application/test-data" { + t.Errorf("Request cgi_data should be extracted. expected=%v actual=%#v.", req.Header, cgi) + } + + params, _ := request_payload["params"].(map[string]interface{}) + values, _ := params["fKey"].([]interface{}) + if len(params) != 1 || len(values) != 1 || values[0] != "fValue" { + t.Errorf("Request params should be extracted. expected=%v actual=%#v.", req.Form, params) + } +} + func TestMonitor(t *testing.T) { setup(t) defer teardown() diff --git a/notice.go b/notice.go index b780b58..3135220 100644 --- a/notice.go +++ b/notice.go @@ -2,6 +2,7 @@ package honeybadger import ( "encoding/json" + "net/http" "net/url" "os" "regexp" @@ -174,6 +175,12 @@ func newNotice(config *Configuration, err Error, extra ...interface{}) *Notice { notice.CGIData = t case url.URL: notice.URL = t.String() + case *http.Request: + if t != nil { + notice.Params = Params(t.Form) + notice.CGIData = getCGIData(t) + notice.URL = t.URL.String() + } } }