Skip to content

Commit

Permalink
Added support for *http.Request notice extra
Browse files Browse the repository at this point in the history
  • Loading branch information
rykov committed Sep 14, 2024
1 parent 401a3e6 commit 5046808
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
Expand Down
44 changes: 44 additions & 0 deletions honeybadger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"

Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package honeybadger

import (
"encoding/json"
"net/http"
"net/url"
"os"
"regexp"
Expand Down Expand Up @@ -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()
}
}
}

Expand Down

0 comments on commit 5046808

Please sign in to comment.