22 * Module dependencies.
33 */
44
5- var request = require ( 'superagent' )
6- , util = require ( 'util' )
7- , http = require ( 'http' )
8- , https = require ( 'https' )
9- , assert = require ( 'assert' )
10- , Request = request . Request ;
5+ var request = require ( 'superagent' ) ;
6+ var util = require ( 'util' ) ;
7+ var http = require ( 'http' ) ;
8+ var https = require ( 'https' ) ;
9+ var assert = require ( 'assert' ) ;
10+ var Request = request . Request ;
1111
1212/**
1313 * Expose `Test`.
@@ -31,7 +31,7 @@ function Test(app, method, path) {
3131 this . buffer ( ) ;
3232 this . app = app ;
3333 this . _asserts = [ ] ;
34- this . url = 'string' == typeof app
34+ this . url = typeof app === 'string'
3535 ? app + path
3636 : this . serverAddress ( app , path ) ;
3737}
@@ -51,11 +51,14 @@ Test.prototype.__proto__ = Request.prototype;
5151 * @api private
5252 */
5353
54- Test . prototype . serverAddress = function ( app , path ) {
54+ Test . prototype . serverAddress = function ( app , path ) {
5555 var addr = app . address ( ) ;
56+ var port ;
57+ var protocol ;
58+
5659 if ( ! addr ) this . _server = app . listen ( 0 ) ;
57- var port = app . address ( ) . port ;
58- var protocol = app instanceof https . Server ? 'https' : 'http' ;
60+ port = app . address ( ) . port ;
61+ protocol = app instanceof https . Server ? 'https' : 'http' ;
5962 return protocol + '://127.0.0.1:' + port + path ;
6063} ;
6164
@@ -75,27 +78,28 @@ Test.prototype.serverAddress = function(app, path){
7578 * @api public
7679 */
7780
78- Test . prototype . expect = function ( a , b , c ) {
81+ Test . prototype . expect = function ( a , b , c ) {
7982 // callback
80- if ( 'function' == typeof a ) {
83+ if ( typeof a === 'function' ) {
8184 this . _asserts . push ( a ) ;
8285 return this ;
8386 }
84- if ( 'function' == typeof b ) this . end ( b ) ;
85- if ( 'function' == typeof c ) this . end ( c ) ;
87+ if ( typeof b === 'function' ) this . end ( b ) ;
88+ if ( typeof c === 'function' ) this . end ( c ) ;
8689
8790 // status
88- if ( 'number' == typeof a ) {
91+ if ( typeof a === 'number' ) {
8992 this . _asserts . push ( this . _assertStatus . bind ( this , a ) ) ;
9093 // body
91- if ( 'function' != typeof b && arguments . length > 1 )
94+ if ( typeof b !== 'function' && arguments . length > 1 ) {
9295 this . _asserts . push ( this . _assertBody . bind ( this , b ) ) ;
96+ }
9397 return this ;
9498 }
9599
96100 // header field
97- if ( 'string' == typeof b || 'number' == typeof b || b instanceof RegExp ) {
98- this . _asserts . push ( this . _assertHeader . bind ( this , { name : '' + a , value : b } ) ) ;
101+ if ( typeof b === 'string' || typeof b === 'number' || b instanceof RegExp ) {
102+ this . _asserts . push ( this . _assertHeader . bind ( this , { name : '' + a , value : b } ) ) ;
99103 return this ;
100104 }
101105
@@ -113,17 +117,17 @@ Test.prototype.expect = function(a, b, c){
113117 * @api public
114118 */
115119
116- Test . prototype . end = function ( fn ) {
120+ Test . prototype . end = function ( fn ) {
117121 var self = this ;
118122 var server = this . _server ;
119123 var end = Request . prototype . end ;
120124
121- end . call ( this , function ( err , res ) {
125+ end . call ( this , function ( err , res ) {
122126 if ( server && server . _handle ) return server . close ( assert ) ;
123127
124128 assert ( ) ;
125129
126- function assert ( ) {
130+ function assert ( ) {
127131 self . assert ( err , res , fn ) ;
128132 }
129133 } ) ;
@@ -140,18 +144,19 @@ Test.prototype.end = function(fn){
140144 * @api private
141145 */
142146
143- Test . prototype . assert = function ( resError , res , fn ) {
147+ Test . prototype . assert = function ( resError , res , fn ) {
144148 var error ;
149+ var i ;
145150
146151 // asserts
147- for ( var i = 0 ; i < this . _asserts . length && ! error ; ++ i ) {
152+ for ( i = 0 ; i < this . _asserts . length && ! error ; ++ i ) {
148153 error = this . _assertFunction ( this . _asserts [ i ] , res ) ;
149154 }
150155
151156 // set unexpected superagent error if no other error has occurred.
152- if ( ! error && resError instanceof Error &&
153- ( ! res || resError . status !== res . status ) )
157+ if ( ! error && resError instanceof Error && ( ! res || resError . status !== res . status ) ) {
154158 error = resError ;
159+ }
155160
156161 fn . call ( this , error || null , res ) ;
157162} ;
@@ -167,20 +172,23 @@ Test.prototype.assert = function(resError, res, fn){
167172
168173Test . prototype . _assertBody = function ( body , res ) {
169174 var isregexp = body instanceof RegExp ;
175+ var a ;
176+ var b ;
177+
170178 // parsed
171- if ( 'object' == typeof body && ! isregexp ) {
179+ if ( typeof body === 'object' && ! isregexp ) {
172180 try {
173181 assert . deepEqual ( body , res . body ) ;
174182 } catch ( err ) {
175- var a = util . inspect ( body ) ;
176- var b = util . inspect ( res . body ) ;
183+ a = util . inspect ( body ) ;
184+ b = util . inspect ( res . body ) ;
177185 return error ( 'expected ' + a + ' response body, got ' + b , body , res . body ) ;
178186 }
179187 } else {
180188 // string
181189 if ( body !== res . text ) {
182- var a = util . inspect ( body ) ;
183- var b = util . inspect ( res . text ) ;
190+ a = util . inspect ( body ) ;
191+ b = util . inspect ( res . text ) ;
184192
185193 // regexp
186194 if ( isregexp ) {
@@ -206,11 +214,19 @@ Test.prototype._assertBody = function(body, res) {
206214Test . prototype . _assertHeader = function ( header , res ) {
207215 var field = header . name ;
208216 var actual = res . header [ field . toLowerCase ( ) ] ;
209- if ( null == actual ) return new Error ( 'expected "' + field + '" header field' ) ;
210217 var fieldExpected = header . value ;
211- if ( fieldExpected == actual ) return ;
218+
219+ if ( typeof actual === 'undefined' ) return new Error ( 'expected "' + field + '" header field' ) ;
220+ if ( fieldExpected == actual ) return ; // eslint-disable-line eqeqeq
221+ /* if ((actual instanceof Array && actual.toString() === fieldExpected) ||
222+ fieldExpected === actual) {
223+ return;
224+ } */
212225 if ( fieldExpected instanceof RegExp ) {
213- if ( ! fieldExpected . test ( actual ) ) return new Error ( 'expected "' + field + '" matching ' + fieldExpected + ', got "' + actual + '"' ) ;
226+ if ( ! fieldExpected . test ( actual ) ) {
227+ return new Error ( 'expected "' + field + '" matching ' +
228+ fieldExpected + ', got "' + actual + '"' ) ;
229+ }
214230 } else {
215231 return new Error ( 'expected "' + field + '" of "' + fieldExpected + '", got "' + actual + '"' ) ;
216232 }
@@ -226,9 +242,11 @@ Test.prototype._assertHeader = function(header, res) {
226242 */
227243
228244Test . prototype . _assertStatus = function ( status , res ) {
245+ var a ;
246+ var b ;
229247 if ( res . status !== status ) {
230- var a = http . STATUS_CODES [ status ] ;
231- var b = http . STATUS_CODES [ res . status ] ;
248+ a = http . STATUS_CODES [ status ] ;
249+ b = http . STATUS_CODES [ res . status ] ;
232250 return new Error ( 'expected ' + status + ' "' + a + '", got ' + res . status + ' "' + b + '"' ) ;
233251 }
234252} ;
@@ -245,7 +263,7 @@ Test.prototype._assertFunction = function(check, res) {
245263 var err ;
246264 try {
247265 err = check ( res ) ;
248- } catch ( e ) {
266+ } catch ( e ) {
249267 err = e ;
250268 }
251269 if ( err instanceof Error ) return err ;
0 commit comments