Permalink
Browse files

tests: move raw request into support file

  • Loading branch information...
dougwilson committed Nov 6, 2017
1 parent 8de448f commit 40e090f3ecd8a5da8cd999eb50d26f4e176db2bf
Showing with 96 additions and 104 deletions.
  1. +1 −50 test/fqdn.js
  2. +1 −54 test/server.js
  3. +94 −0 test/support/rawagent.js
View
@@ -2,7 +2,7 @@
var assert = require('assert');
var connect = require('..');
var http = require('http');
var request = require('supertest');
var rawrequest = require('./support/rawagent')
describe('app.use()', function(){
var app;
@@ -80,52 +80,3 @@ describe('app.use()', function(){
});
});
});
function rawrequest(app) {
var _path;
var server = http.createServer(app);
function expect(status, body, callback) {
server.listen(function(){
var addr = this.address();
var port = addr.port;
var req = http.get({
host: '127.0.0.1',
path: _path,
port: port
});
req.on('response', function(res){
var buf = '';
res.setEncoding('utf8');
res.on('data', function(s){ buf += s });
res.on('end', function(){
var err = null;
try {
assert.equal(res.statusCode, status);
assert.equal(buf, body);
} catch (e) {
err = e;
}
server.close();
callback(err);
});
});
});
}
function get(path) {
_path = path;
return {
expect: expect
};
}
return {
get: get
};
}
View
@@ -2,6 +2,7 @@
var assert = require('assert');
var connect = require('..');
var http = require('http');
var rawrequest = require('./support/rawagent')
var request = require('supertest');
describe('app', function(){
@@ -226,57 +227,3 @@ describe('app', function(){
});
});
});
function rawrequest(app) {
var _path;
var server = http.createServer(app);
function expect(status, body, callback) {
server.listen(function(){
var addr = this.address();
var port = addr.port;
var req = http.get({
host: '127.0.0.1',
path: _path,
port: port
});
req.on('response', function(res){
var buf = '';
res.setEncoding('utf8');
res.on('data', function(s){ buf += s });
res.on('end', function(){
var err = null;
try {
assert.equal(res.statusCode, status);
if (body instanceof RegExp) {
assert.ok(body.test(buf), 'expected body ' + buf + ' to match ' + body)
} else {
assert.equal(buf, body, 'expected ' + body + ' response body, got ' + buf)
}
} catch (e) {
err = e;
}
server.close();
callback(err);
});
});
});
}
function get(path) {
_path = path;
return {
expect: expect
};
}
return {
get: get
};
}
View
@@ -0,0 +1,94 @@
'use strict'
var assert = require('assert')
var http = require('http')
module.exports = createRawAgent
function createRawAgent (app) {
return new RawAgent(app)
}
function RawAgent (app) {
this.app = app
this._open = 0
this._port = null
this._server = null
}
RawAgent.prototype.get = function get (path) {
return new RawRequest(this, 'GET', path)
}
RawAgent.prototype._close = function _close (cb) {
if (--this._open) {
return process.nextTick(cb)
}
this._server.close(cb)
}
RawAgent.prototype._start = function _start (cb) {
this._open++
if (this._port) {
return process.nextTick(cb)
}
if (!this._server) {
this._server = http.createServer(this.app).listen()
}
var agent = this
this._server.on('listening', function onListening () {
agent._port = this.address().port
cb()
})
}
function RawRequest (agent, method, path) {
this.agent = agent
this.method = method
this.path = path
}
RawRequest.prototype.expect = function expect (status, body, callback) {
var request = this
this.agent._start(function onStart () {
var req = http.request({
host: '127.0.0.1',
method: request.method,
path: request.path,
port: request.agent._port
})
req.on('response', function (res) {
var buf = ''
res.setEncoding('utf8')
res.on('data', function onData (s) { buf += s })
res.on('end', function onEnd () {
var err = null
try {
assert.equal(res.statusCode, status, 'expected ' + status + ' status, got ' + res.statusCode)
if (body instanceof RegExp) {
assert.ok(body.test(buf), 'expected body ' + buf + ' to match ' + body)
} else {
assert.equal(buf, body, 'expected ' + body + ' response body, got ' + buf)
}
} catch (e) {
err = e
}
request.agent._close(function onClose () {
callback(err)
})
})
})
req.end()
})
}

0 comments on commit 40e090f

Please sign in to comment.