@@ -6,93 +6,103 @@ var assert = require('assert'),
6
6
Singleton = require ( '../lib/honeybadger' ) ;
7
7
8
8
describe ( 'Express Middleware' , function ( ) {
9
- var subject , client_mock , client ;
10
- var error = new Error ( 'Badgers!' ) ;
9
+ let subject , client_mock , client ;
10
+ const error = new Error ( 'Badgers!' ) ;
11
11
12
- setup ( function ( ) {
12
+ beforeEach ( function ( ) {
13
13
client = Singleton . factory ( { apiKey : 'fake api key' } ) ;
14
14
subject = client . errorHandler ;
15
-
16
15
client_mock = sinon . mock ( client ) ;
17
16
} ) ;
18
17
19
- it ( 'calls next' , function ( ) {
20
- var app = express ( ) ;
21
- var expected = sinon . spy ( ) ;
18
+ it ( 'is sane' , function ( done ) {
19
+ const app = express ( ) ;
22
20
23
- app . use ( function ( req , res , next ) {
24
- throw ( error ) ;
25
- } ) ;
26
- app . use ( subject ) ;
27
- app . use ( function ( err , req , res , next ) {
28
- expected ( ) ;
21
+ app . get ( '/user' , function ( req , res ) {
22
+ res . status ( 200 ) . json ( { name : 'john' } ) ;
29
23
} ) ;
30
24
31
- request ( app . listen ( ) )
32
- . get ( '/' )
33
- . end ( function ( err , res ) {
34
- if ( err ) return done ( err ) ;
35
- assert ( expected . called ) ;
36
- done ( ) ;
37
- } ) ;
25
+ request ( app )
26
+ . get ( '/user' )
27
+ . set ( 'Accept' , 'application/json' )
28
+ . expect ( 'Content-Type' , / j s o n / )
29
+ . expect ( 200 , done ) ;
38
30
} ) ;
39
31
40
- it ( 'reports the error to Honeybadger' , function ( done ) {
41
- var app = express ( ) ;
32
+ it ( 'reports the error to Honeybadger and calls next error handler' , function ( done ) {
33
+ const app = express ( ) ;
34
+ const expected = sinon . spy ( ) ;
42
35
43
- app . use ( function ( req , res , next ) {
36
+ app . use ( client . requestHandler ) ;
37
+
38
+ app . get ( '/' , function ( req , res ) {
44
39
throw ( error ) ;
45
40
} ) ;
41
+
46
42
app . use ( subject ) ;
47
43
48
- request ( app . listen ( ) )
49
- . get ( '/' )
50
- . end ( function ( err , res ) {
51
- if ( err ) return done ( err ) ;
52
- client_mock . verify ( ) ;
53
- done ( ) ;
44
+ app . use ( function ( err , req , res , next ) {
45
+ expected ( ) ;
46
+ next ( ) ;
47
+ } ) ;
48
+
49
+ client_mock . expects ( 'notify' ) . once ( ) . withArgs ( error ) ;
50
+
51
+ request ( app )
52
+ . get ( '/' )
53
+ . expect ( 500 , function ( ) {
54
+ client_mock . verify ( ) ;
55
+ assert ( expected . calledOnce ) ;
56
+ done ( ) ;
54
57
} ) ;
55
58
} ) ;
56
59
57
- it ( 'reports async errors to Honeybadger' , function ( done ) {
58
- var app = express ( ) ;
60
+ it ( 'reports async errors to Honeybadger and calls next error handler' , function ( done ) {
61
+ const app = express ( ) ;
62
+ const expected = sinon . spy ( ) ;
59
63
60
64
app . use ( client . requestHandler ) ;
61
- app . use ( function ( req , res , next ) {
65
+
66
+ app . get ( '/' , function ( req , res ) {
62
67
setTimeout ( function asyncThrow ( ) {
63
68
throw ( error ) ;
64
69
} , 0 ) ;
65
70
} ) ;
71
+
66
72
app . use ( subject ) ;
67
73
74
+ app . use ( function ( err , req , res , next ) {
75
+ expected ( ) ;
76
+ next ( ) ;
77
+ } ) ;
78
+
68
79
client_mock . expects ( 'notify' ) . once ( ) . withArgs ( error ) ;
69
80
70
- request ( app . listen ( ) )
71
- . get ( '/' )
72
- . end ( function ( err , res ) {
73
- if ( err ) return done ( err ) ;
74
- client_mock . verify ( ) ;
75
- done ( ) ;
76
- } ) ;
81
+ request ( app )
82
+ . get ( '/' )
83
+ . expect ( 500 , function ( ) {
84
+ client_mock . verify ( ) ;
85
+ assert ( expected . calledOnce ) ;
86
+ done ( ) ;
87
+ } ) ;
77
88
} ) ;
78
89
79
90
it ( 'provides a noop metricsHandler' , function ( done ) {
80
- var app = express ( ) ;
81
- var spy = sinon . spy ( ) ;
91
+ const app = express ( ) ;
92
+ const expected = sinon . spy ( ) ;
82
93
83
94
app . use ( client . metricsHandler ) ;
84
95
app . use ( function ( req , res , next ) {
85
- spy ( ) ;
96
+ expected ( ) ;
86
97
next ( ) ;
87
98
} ) ;
88
99
89
- request ( app . listen ( ) )
90
- . get ( '/' )
91
- . end ( function ( err , res ) {
92
- if ( err ) return done ( err ) ;
93
- assert ( spy . calledOnce ) ;
94
- done ( ) ;
95
- } ) ;
100
+ request ( app )
101
+ . get ( '/' )
102
+ . expect ( 200 , function ( ) {
103
+ assert ( expected . calledOnce ) ;
104
+ done ( ) ;
105
+ } ) ;
96
106
} ) ;
97
107
} ) ;
98
108
0 commit comments