/usr/share/gocode/src/github.com/bugsnag/bugsnag-go/bugsnag_test.go is in golang-github-bugsnag-bugsnag-go-dev 1.0.5+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | package bugsnag
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/bitly/go-simplejson"
)
func TestConfigure(t *testing.T) {
Configure(Configuration{
APIKey: testAPIKey,
})
if Config.APIKey != testAPIKey {
t.Errorf("Setting APIKey didn't work")
}
if New().Config.APIKey != testAPIKey {
t.Errorf("Setting APIKey didn't work for new notifiers")
}
}
var postedJSON = make(chan []byte, 10)
var testOnce sync.Once
var testEndpoint string
var testAPIKey = "166f5ad3590596f9aa8d601ea89af845"
func startTestServer() {
testOnce.Do(func() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
postedJSON <- body
})
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
testEndpoint = "http://" + l.Addr().String() + "/"
go http.Serve(l, mux)
})
}
type _recurse struct {
*_recurse
}
func TestNotify(t *testing.T) {
startTestServer()
recurse := _recurse{}
recurse._recurse = &recurse
OnBeforeNotify(func(event *Event, config *Configuration) error {
if event.Context == "testing" {
event.GroupingHash = "lol"
}
return nil
})
Notify(fmt.Errorf("hello world"),
Configuration{
APIKey: testAPIKey,
Endpoint: testEndpoint,
ReleaseStage: "test",
AppVersion: "1.2.3",
Hostname: "web1",
ProjectPackages: []string{"github.com/bugsnag/bugsnag-go"},
},
User{Id: "123", Name: "Conrad", Email: "me@cirw.in"},
Context{"testing"},
MetaData{"test": {
"password": "sneaky",
"value": "able",
"broken": complex(1, 2),
"recurse": recurse,
}},
)
json, err := simplejson.NewJson(<-postedJSON)
if err != nil {
t.Fatal(err)
}
if json.Get("apiKey").MustString() != testAPIKey {
t.Errorf("Wrong api key in payload")
}
if json.GetPath("notifier", "name").MustString() != "Bugsnag Go" {
t.Errorf("Wrong notifier name in payload")
}
event := json.Get("events").GetIndex(0)
for k, value := range map[string]string{
"payloadVersion": "2",
"severity": "warning",
"context": "testing",
"groupingHash": "lol",
"app.releaseStage": "test",
"app.version": "1.2.3",
"device.hostname": "web1",
"user.id": "123",
"user.name": "Conrad",
"user.email": "me@cirw.in",
"metaData.test.password": "[REDACTED]",
"metaData.test.value": "able",
"metaData.test.broken": "[complex128]",
"metaData.test.recurse._recurse": "[RECURSION]",
} {
key := strings.Split(k, ".")
if event.GetPath(key...).MustString() != value {
t.Errorf("Wrong %v: %v != %v", key, event.GetPath(key...).MustString(), value)
}
}
exception := event.Get("exceptions").GetIndex(0)
if exception.Get("message").MustString() != "hello world" {
t.Errorf("Wrong message in payload")
}
if exception.Get("errorClass").MustString() != "*errors.errorString" {
t.Errorf("Wrong errorClass in payload: %v", exception.Get("errorClass").MustString())
}
frame0 := exception.Get("stacktrace").GetIndex(0)
if frame0.Get("file").MustString() != "bugsnag_test.go" ||
frame0.Get("method").MustString() != "TestNotify" ||
frame0.Get("inProject").MustBool() != true ||
frame0.Get("lineNumber").MustInt() == 0 {
t.Errorf("Wrong frame0")
}
frame1 := exception.Get("stacktrace").GetIndex(1)
if frame1.Get("file").MustString() != "testing/testing.go" ||
frame1.Get("method").MustString() != "tRunner" ||
frame1.Get("inProject").MustBool() != false ||
frame1.Get("lineNumber").MustInt() == 0 {
t.Errorf("Wrong frame1")
}
}
func crashyHandler(w http.ResponseWriter, r *http.Request) {
c := make(chan int)
close(c)
c <- 1
}
func runCrashyServer(rawData ...interface{}) (net.Listener, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
}
mux := http.NewServeMux()
mux.HandleFunc("/", crashyHandler)
srv := http.Server{
Addr: l.Addr().String(),
Handler: Handler(mux, rawData...),
ErrorLog: log.New(ioutil.Discard, log.Prefix(), 0),
}
go srv.Serve(l)
return l, err
}
func TestHandler(t *testing.T) {
startTestServer()
l, err := runCrashyServer(Configuration{
APIKey: testAPIKey,
Endpoint: testEndpoint,
ProjectPackages: []string{"github.com/bugsnag/bugsnag-go"},
Logger: log.New(ioutil.Discard, log.Prefix(), log.Flags()),
}, SeverityInfo)
if err != nil {
t.Fatal(err)
}
http.Get("http://" + l.Addr().String() + "/ok?foo=bar")
l.Close()
json, err := simplejson.NewJson(<-postedJSON)
if err != nil {
t.Fatal(err)
}
if json.Get("apiKey").MustString() != testAPIKey {
t.Errorf("Wrong api key in payload")
}
if json.GetPath("notifier", "name").MustString() != "Bugsnag Go" {
t.Errorf("Wrong notifier name in payload")
}
event := json.Get("events").GetIndex(0)
for k, value := range map[string]string{
"payloadVersion": "2",
"severity": "info",
"user.id": "127.0.0.1",
"metaData.Request.Url": "http://" + l.Addr().String() + "/ok?foo=bar",
"metaData.Request.Method": "GET",
} {
key := strings.Split(k, ".")
if event.GetPath(key...).MustString() != value {
t.Errorf("Wrong %v: %v != %v", key, event.GetPath(key...).MustString(), value)
}
}
if event.GetPath("metaData", "Request", "Params", "foo").GetIndex(0).MustString() != "bar" {
t.Errorf("missing GET params in request metadata")
}
if event.GetPath("metaData", "Headers", "Accept-Encoding").GetIndex(0).MustString() != "gzip" {
t.Errorf("missing GET params in request metadata: %v", event.GetPath("metaData", "Headers"))
}
exception := event.Get("exceptions").GetIndex(0)
if !strings.Contains(exception.Get("message").MustString(), "send on closed channel") {
t.Errorf("Wrong message in payload: %v '%v'", exception.Get("message").MustString(), "runtime error: send on closed channel")
}
errorClass := exception.Get("errorClass").MustString()
if errorClass != "runtime.errorCString" && errorClass != "*errors.errorString" {
t.Errorf("Wrong errorClass in payload: %v, '%v'", exception.Get("errorClass").MustString(), "runtime.errorCString")
}
frame0 := exception.Get("stacktrace").GetIndex(0)
file0 := frame0.Get("file").MustString()
if !strings.HasPrefix(file0, "runtime/panic") ||
frame0.Get("inProject").MustBool() != false {
t.Errorf("Wrong frame0: %v", frame0)
}
frame3 := exception.Get("stacktrace").GetIndex(3)
if frame3.Get("file").MustString() != "bugsnag_test.go" ||
frame3.Get("method").MustString() != "crashyHandler" ||
frame3.Get("inProject").MustBool() != true ||
frame3.Get("lineNumber").MustInt() == 0 {
t.Errorf("Wrong frame3: %v", frame3)
}
}
func TestAutoNotify(t *testing.T) {
var panicked interface{}
func() {
defer func() {
panicked = recover()
}()
defer AutoNotify(Configuration{Endpoint: testEndpoint, APIKey: testAPIKey})
panic("eggs")
}()
if panicked.(string) != "eggs" {
t.Errorf("didn't re-panic")
}
json, err := simplejson.NewJson(<-postedJSON)
if err != nil {
t.Fatal(err)
}
event := json.Get("events").GetIndex(0)
if event.Get("severity").MustString() != "error" {
t.Errorf("severity should be error")
}
exception := event.Get("exceptions").GetIndex(0)
if exception.Get("message").MustString() != "eggs" {
t.Errorf("caught wrong panic")
}
}
func TestRecover(t *testing.T) {
var panicked interface{}
func() {
defer func() {
panicked = recover()
}()
defer Recover(Configuration{Endpoint: testEndpoint, APIKey: testAPIKey})
panic("ham")
}()
if panicked != nil {
t.Errorf("re-panick'd")
}
json, err := simplejson.NewJson(<-postedJSON)
if err != nil {
t.Fatal(err)
}
event := json.Get("events").GetIndex(0)
if event.Get("severity").MustString() != "warning" {
t.Errorf("severity should be warning")
}
exception := event.Get("exceptions").GetIndex(0)
if exception.Get("message").MustString() != "ham" {
t.Errorf("caught wrong panic")
}
}
func handleGet(w http.ResponseWriter, r *http.Request) {
}
var createAccount = handleGet
type _job struct {
Name string
Process func()
}
func ExampleAutoNotify() interface{} {
return func(w http.ResponseWriter, request *http.Request) {
defer AutoNotify(request, Context{"createAccount"})
createAccount(w, request)
}
}
func ExampleRecover(job _job) {
go func() {
defer Recover(Context{job.Name}, SeverityWarning)
job.Process()
}()
}
func ExampleConfigure() {
Configure(Configuration{
APIKey: "YOUR_API_KEY_HERE",
ReleaseStage: "production",
// See Configuration{} for other fields
})
}
func ExampleHandler() {
// Set up your http handlers as usual
http.HandleFunc("/", handleGet)
// use bugsnag.Handler(nil) to wrap the default http handlers
// so that Bugsnag is automatically notified about panics.
http.ListenAndServe(":1234", Handler(nil))
}
func ExampleHandler_customServer() {
// If you're using a custom server, set the handlers explicitly.
http.HandleFunc("/", handleGet)
srv := http.Server{
Addr: ":1234",
ReadTimeout: 10 * time.Second,
// use bugsnag.Handler(nil) to wrap the default http handlers
// so that Bugsnag is automatically notified about panics.
Handler: Handler(nil),
}
srv.ListenAndServe()
}
func ExampleHandler_customHandlers() {
// If you're using custom handlers, wrap the handlers explicitly.
handler := http.NewServeMux()
http.HandleFunc("/", handleGet)
// use bugsnag.Handler(handler) to wrap the handlers so that Bugsnag is
// automatically notified about panics
http.ListenAndServe(":1234", Handler(handler))
}
func ExampleNotify() {
_, err := net.Listen("tcp", ":80")
if err != nil {
Notify(err)
}
}
func ExampleNotify_details(userID string) {
_, err := net.Listen("tcp", ":80")
if err != nil {
Notify(err,
// show as low-severity
SeverityInfo,
// set the context
Context{"createlistener"},
// pass the user id in to count users affected.
User{Id: userID},
// custom meta-data tab
MetaData{
"Listen": {
"Protocol": "tcp",
"Port": "80",
},
},
)
}
}
type Job struct {
Retry bool
UserId string
UserEmail string
Name string
Params map[string]string
}
func ExampleOnBeforeNotify() {
OnBeforeNotify(func(event *Event, config *Configuration) error {
// Search all the RawData for any *Job pointers that we're passed in
// to bugsnag.Notify() and friends.
for _, datum := range event.RawData {
if job, ok := datum.(*Job); ok {
// don't notify bugsnag about errors in retries
if job.Retry {
return fmt.Errorf("bugsnag middleware: not notifying about job retry")
}
// add the job as a tab on Bugsnag.com
event.MetaData.AddStruct("Job", job)
// set the user correctly
event.User = &User{Id: job.UserId, Email: job.UserEmail}
}
}
// continue notifying as normal
return nil
})
}
|