22
33var net = require ( 'net' )
44var express = require ( '../' )
5- var http = require ( 'http' ) ;
65var assert = require ( 'assert' ) ;
6+ var request = require ( 'supertest' )
77
88describe ( 'app.listen()' , function ( ) {
9- function makeGetRequest ( port , cb ) {
10- http
11- . get ( 'http://localhost:' + port , function ( res ) {
12- var data = ''
13- res . on ( 'data' , function ( chunk ) {
14- data += chunk ;
15- } ) ;
16-
17- res . on ( 'end' , function ( ) {
18- cb ( null , data ) ;
19- } ) ;
20- } )
21- . on ( 'error' , function ( error ) {
22- cb ( error , null ) ;
23- } ) ;
24- }
25-
269 it ( 'should wrap with an HTTP server' , function ( done ) {
2710 var app = express ( ) ;
2811
@@ -33,32 +16,27 @@ describe('app.listen()', function(){
3316 } )
3417
3518 it ( 'should listen on the requested port' , function ( done ) {
36- var expectedResponseBody = 'hello world' ;
37- var server ;
3819 var app = express ( )
39- . get ( '/' , function ( req , res ) {
40- res . send ( expectedResponseBody ) ;
41- } ) ;
4220
43- getPort ( function ( openPortError , port ) {
44- if ( openPortError !== null ) {
45- return done ( openPortError ) ;
46- }
21+ app . get ( '/' , function ( req , res ) {
22+ res . json ( { port : req . socket . address ( ) . port } )
23+ } )
24+
25+ getPort ( function ( error , port ) {
26+ if ( error ) return done ( error )
4727
48- server = app . listen ( port , function ( ) {
49- makeGetRequest ( port , function ( getError , responseBody ) {
50- try {
51- assert . strictEqual ( getError , null ) ;
52- assert . strictEqual ( responseBody , expectedResponseBody )
53- done ( )
54- } catch ( error ) {
55- done ( error ) ;
56- } finally {
57- server . close ( ) ;
58- }
59- } ) ;
60- } ) ;
61- } ) ;
28+ assert . strictEqual ( typeof port , 'number' )
29+
30+ var server = app . listen ( port , function ( ) {
31+ request ( server )
32+ . get ( '/' )
33+ . expect ( 200 , { port : port } , function ( error ) {
34+ server . close ( function ( ) {
35+ done ( error )
36+ } )
37+ } )
38+ } )
39+ } )
6240 } )
6341} )
6442
0 commit comments