Skip to content

Commit

Permalink
add test case for issue #1875
Browse files Browse the repository at this point in the history
  • Loading branch information
djskinner committed Nov 28, 2022
1 parent 8d607ca commit 81a9fa0
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 2 deletions.
17 changes: 15 additions & 2 deletions test/node/features/express.feature
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Scenario: a handled error passed to req.bugsnag.notify()
And the event "request.clientIp" is not null

Scenario: adding body to request metadata
When I POST the data "data=in_request_body" to the URL "http://express/bodytest"
When I POST the data "data=in_request_body_1" to the URL "http://express/bodytest"
And I wait to receive an error
Then the error is valid for the error reporting API version "4" for the "Bugsnag Node" notifier
And the event "unhandled" is true
Expand All @@ -116,5 +116,18 @@ Scenario: adding body to request metadata
And the exception "message" equals "request body"
And the exception "type" equals "nodejs"
And the "file" of stack frame 0 equals "scenarios/app.js"
And the event "request.body.data" equals "in_request_body"
And the event "request.body.data" equals "in_request_body_1"
And the event "request.httpMethod" equals "POST"
And I discard the oldest error

Then I POST the data "data=in_request_body_2" to the URL "http://express/bodytest"
And I wait to receive an error
Then the error is valid for the error reporting API version "4" for the "Bugsnag Node" notifier
And the event "unhandled" is true
And the event "severity" equals "error"
And the exception "errorClass" equals "Error"
And the exception "message" equals "request body"
And the exception "type" equals "nodejs"
And the "file" of stack frame 0 equals "scenarios/app.js"
And the event "request.body.data" equals "in_request_body_2"
And the event "request.httpMethod" equals "POST"
34 changes: 34 additions & 0 deletions test/node/features/express_auto_sessions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Feature: @bugsnag/plugin-express

Background:
Given I store the api key in the environment variable "BUGSNAG_API_KEY"
And I store the notify endpoint in the environment variable "BUGSNAG_NOTIFY_ENDPOINT"
And I store the sessions endpoint in the environment variable "BUGSNAG_SESSIONS_ENDPOINT"
And I start the service "express"
And I wait for the host "express" to open port "80"

Scenario: adding body to request metadata
When I POST the data "data=in_request_body_1" to the URL "http://express/bodytest"
And I wait to receive an error
Then the error is valid for the error reporting API version "4" for the "Bugsnag Node" notifier
And the event "unhandled" is true
And the event "severity" equals "error"
And the exception "errorClass" equals "Error"
And the exception "message" equals "request body"
And the exception "type" equals "nodejs"
And the "file" of stack frame 0 equals "scenarios/app.js"
And the event "request.body.data" equals "in_request_body_1"
And the event "request.httpMethod" equals "POST"
And I discard the oldest error

Then I POST the data "data=in_request_body_2" to the URL "http://express/bodytest"
And I wait to receive an error
Then the error is valid for the error reporting API version "4" for the "Bugsnag Node" notifier
And the event "unhandled" is true
And the event "severity" equals "error"
And the exception "errorClass" equals "Error"
And the exception "message" equals "request body"
And the exception "type" equals "nodejs"
And the "file" of stack frame 0 equals "scenarios/app.js"
And the event "request.body.data" equals "in_request_body_2"
And the event "request.httpMethod" equals "POST"
16 changes: 16 additions & 0 deletions test/node/features/fixtures/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ services:
restart: "no"
entrypoint: "node scenarios/app-disabled"

express-auto-sessions:
build:
context: express
args:
- NODE_VERSION
environment:
- BUGSNAG_API_KEY
- BUGSNAG_NOTIFY_ENDPOINT
- BUGSNAG_SESSIONS_ENDPOINT
networks:
default:
aliases:
- express-auto-sessions
restart: "no"
entrypoint: "node scenarios/app-auto-sessions"

restify:
build:
context: restify
Expand Down
113 changes: 113 additions & 0 deletions test/node/features/fixtures/express/scenarios/app-auto-sessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
var Bugsnag = require('@bugsnag/node')
var bugsnagExpress = require('@bugsnag/plugin-express')
var express = require('express')
var bodyParser = require('body-parser')

Bugsnag.start({
apiKey: process.env.BUGSNAG_API_KEY,
endpoints: {
notify: process.env.BUGSNAG_NOTIFY_ENDPOINT,
sessions: process.env.BUGSNAG_SESSIONS_ENDPOINT
},
featureFlags: [
{ name: 'from config 1', variant: '1234' },
{ name: 'from config 2' },
{ name: 'from config 3', variant: 'SHOULD BE REMOVED' }
],
autoTrackSessions: true,
plugins: [bugsnagExpress]
})

var middleware = Bugsnag.getPlugin('express')

var app = express()

app.use(middleware.requestHandler)

// If the server hasn't started sending something within 2 seconds
// it probably won't. So end the request and hurry the failing test
// along.
app.use(function (req, res, next) {
setTimeout(function () {
if (!res.headersSent) return res.sendStatus(500)
}, 2000)
next()
})

app.get('/', function (req, res) {
res.end('ok')
})

app.get('/sync', function (req, res) {
throw new Error('sync')
})

app.get('/async', function (req, res) {
setTimeout(function () {
throw new Error('async')
}, 100)
})

app.get('/next', function (req, res, next) {
next(new Error('next'))
})

app.get('/rejection-sync', function (req, res, next) {
Promise.reject(new Error('reject sync')).catch(next)
})

app.get('/rejection-async', function (req, res, next) {
setTimeout(function () {
Promise.reject(new Error('reject async')).catch(next)
}, 100)
})

app.get('/string-as-error', function (req, res, next) {
next('errrr')
})

app.get('/throw-non-error', function (req, res, next) {
throw 1 // eslint-disable-line
})

app.get('/handled', function (req, res, next) {
req.bugsnag.notify(new Error('handled'))
res.end('OK')
})

app.post('/bodytest', bodyParser.urlencoded(), function (req, res, next) {
throw new Error('request body')
})

app.post('/features/unhandled', bodyParser.urlencoded(), function (req, res, next) {
// the request body is an object of feature flag name -> variant
const featureFlags = Object.keys(req.body).map(name => ({ name, variant: req.body[name] }))

req.bugsnag.addFeatureFlags(featureFlags)
req.bugsnag.clearFeatureFlag('from config 3')

if (req.body.hasOwnProperty('clearAllFeatureFlags')) {
req.bugsnag.clearFeatureFlags()
}

throw new Error('oh no')
})

app.post('/features/handled', bodyParser.urlencoded(), function (req, res, next) {
// the request body is an object of feature flag name -> variant
const featureFlags = Object.keys(req.body).map(name => ({ name, variant: req.body[name] }))

req.bugsnag.addFeatureFlags(featureFlags)
req.bugsnag.clearFeatureFlag('from config 3')

if (req.body.hasOwnProperty('clearAllFeatureFlags')) {
req.bugsnag.clearFeatureFlags()
}

req.bugsnag.notify(new Error('oh no'))
res.end('OK')
})

app.use(middleware.errorHandler)

app.listen(80)

0 comments on commit 81a9fa0

Please sign in to comment.