Skip to content

Commit

Permalink
use supertest and assert port
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 16, 2023
1 parent d2e177f commit f5bd4de
Showing 1 changed file with 19 additions and 41 deletions.
60 changes: 19 additions & 41 deletions test/app.listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,10 @@

var net = require('net')
var express = require('../')
var http = require('http');
var assert = require('assert');
var request = require('supertest')

describe('app.listen()', function(){
function makeGetRequest(port, cb){
http
.get('http://localhost:' + port, function(res){
var data = ''
res.on('data', function(chunk){
data += chunk;
});

res.on('end', function(){
cb(null, data);
});
})
.on('error', function(error) {
cb(error, null);
});
}

it('should wrap with an HTTP server', function(done){
var app = express();

Expand All @@ -33,32 +16,27 @@ describe('app.listen()', function(){
})

it('should listen on the requested port', function (done) {
var expectedResponseBody = 'hello world';
var server;
var app = express()
.get('/', function(req, res){
res.send(expectedResponseBody);
});

getPort(function (openPortError, port) {
if(openPortError !== null) {
return done(openPortError);
}
app.get('/', function (req, res) {
res.json({ port: req.socket.address().port })
})

getPort(function (error, port) {
if (error) return done(error)

server = app.listen(port, function(){
makeGetRequest(port, function(getError, responseBody) {
try {
assert.strictEqual(getError, null);
assert.strictEqual(responseBody, expectedResponseBody)
done()
} catch (error) {
done(error);
} finally {
server.close();
}
});
});
});
assert.strictEqual(typeof port, 'number')

var server = app.listen(port, function () {
request(server)
.get('/')
.expect(200, { port: port }, function (error) {
server.close(function () {
done(error)
})
})
})
})
})
})

Expand Down

0 comments on commit f5bd4de

Please sign in to comment.