2
2
3
3
var net = require ( 'net' )
4
4
var express = require ( '../' )
5
- var http = require ( 'http' ) ;
6
5
var assert = require ( 'assert' ) ;
6
+ var request = require ( 'supertest' )
7
7
8
8
describe ( '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
-
26
9
it ( 'should wrap with an HTTP server' , function ( done ) {
27
10
var app = express ( ) ;
28
11
@@ -33,32 +16,27 @@ describe('app.listen()', function(){
33
16
} )
34
17
35
18
it ( 'should listen on the requested port' , function ( done ) {
36
- var expectedResponseBody = 'hello world' ;
37
- var server ;
38
19
var app = express ( )
39
- . get ( '/' , function ( req , res ) {
40
- res . send ( expectedResponseBody ) ;
41
- } ) ;
42
20
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 )
47
27
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
+ } )
62
40
} )
63
41
} )
64
42
0 commit comments