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
@@ -32,32 +15,27 @@ describe('app.listen()', function(){
32
15
} )
33
16
34
17
it ( 'should listen on the requested port' , function ( done ) {
35
- var expectedResponseBody = 'hello world' ;
36
- var server ;
37
18
var app = express ( )
38
- . get ( '/' , function ( req , res ) {
39
- res . send ( expectedResponseBody ) ;
40
- } ) ;
41
19
42
- getPort ( function ( openPortError , port ) {
43
- if ( openPortError !== null ) {
44
- return done ( openPortError ) ;
45
- }
20
+ app . get ( '/' , function ( req , res ) {
21
+ res . json ( { port : req . socket . address ( ) . port } )
22
+ } )
23
+
24
+ getPort ( function ( error , port ) {
25
+ if ( error ) return done ( error )
46
26
47
- server = app . listen ( port , function ( ) {
48
- makeGetRequest ( port , function ( getError , responseBody ) {
49
- try {
50
- assert . strictEqual ( getError , null ) ;
51
- assert . strictEqual ( responseBody , expectedResponseBody )
52
- done ( )
53
- } catch ( error ) {
54
- done ( error ) ;
55
- } finally {
56
- server . close ( ) ;
57
- }
58
- } ) ;
59
- } ) ;
60
- } ) ;
27
+ assert . strictEqual ( typeof port , 'number' )
28
+
29
+ var server = app . listen ( port , function ( ) {
30
+ request ( server )
31
+ . get ( '/' )
32
+ . expect ( 200 , { port : port } , function ( error ) {
33
+ server . close ( function ( ) {
34
+ done ( error )
35
+ } )
36
+ } )
37
+ } )
38
+ } )
61
39
} )
62
40
} )
63
41
0 commit comments