tests: do not use mocking

fixes #1059
parent a2d87be0
var assert = require('assert');
var connect = require('..');
var http = require('http');
var request = require('supertest');
......@@ -15,12 +16,9 @@ describe('app.use()', function(){
res.end(req.url);
});
app.handle({ method: 'GET', url: 'http://example.com/foo' }, {
end: function(str){
str.should.equal('http://example.com/foo');
done();
}
});
rawrequest(app)
.get('http://example.com/foo')
.expect(200, 'http://example.com/foo', done);
});
describe('with a connect app', function(){
......@@ -29,12 +27,9 @@ describe('app.use()', function(){
res.end(req.url);
});
app.handle({ method: 'GET', url: '/proxy?url=http://example.com/blog/post/1' }, {
end: function(str){
str.should.equal('/?url=http://example.com/blog/post/1');
done();
}
});
rawrequest(app)
.get('/proxy?url=http://example.com/blog/post/1')
.expect(200, '/?url=http://example.com/blog/post/1', done);
});
it('should adjust FQDN req.url', function(done){
......@@ -42,12 +37,9 @@ describe('app.use()', function(){
res.end(req.url);
});
app.handle({ method: 'GET', url: 'http://example.com/blog/post/1' }, {
end: function(str){
str.should.equal('http://example.com/post/1');
done();
}
});
rawrequest(app)
.get('http://example.com/blog/post/1')
.expect(200, 'http://example.com/post/1', done);
});
it('should adjust FQDN req.url with multiple handlers', function(done){
......@@ -59,12 +51,9 @@ describe('app.use()', function(){
res.end(req.url);
});
app.handle({ method: 'GET', url: 'http://example.com/blog/post/1' }, {
end: function(str){
str.should.equal('http://example.com/post/1');
done();
}
});
rawrequest(app)
.get('http://example.com/blog/post/1')
.expect(200, 'http://example.com/post/1', done);
});
it('should adjust FQDN req.url with multiple routed handlers', function(done) {
......@@ -75,12 +64,59 @@ describe('app.use()', function(){
res.end(req.url);
});
app.handle({ method: 'GET', url: 'http://example.com/blog/post/1' }, {
end: function(str){
str.should.equal('http://example.com/post/1');
done();
}
});
rawrequest(app)
.get('http://example.com/blog/post/1')
.expect(200, 'http://example.com/post/1', done);
});
});
});
function rawrequest(app) {
var _path;
var server = http.createServer(app);
function expect(status, body, callback) {
server.listen(function(){
var addr = this.address();
var hostname = addr.family === 'IPv6' ? '[::1]' : '127.0.0.1';
var port = addr.port;
var req = http.get({
host: hostname,
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
};
}
var assert = require('assert');
var connect = require('..');
var http = require('http');
var request = require('supertest');
var should = require('should');
......@@ -16,8 +18,6 @@ describe('app', function(){
});
it('should work as middleware', function(done){
var http = require('http');
// custom server handler array
var handlers = [connect(), function(req, res, next){
res.writeHead(200, {'Content-Type': 'text/plain'});
......@@ -66,14 +66,9 @@ describe('app', function(){
describe('404 handler', function(){
it('should escape the 404 response body', function(done){
app.handle({ method: 'GET', url: '/foo/<script>stuff</script>' }, {
setHeader: function(){},
end: function(str){
this.statusCode.should.equal(404);
str.should.equal('Cannot GET /foo/&lt;script&gt;stuff&lt;/script&gt;\n');
done();
}
});
rawrequest(app)
.get('/foo/<script>stuff</script>')
.expect(404, 'Cannot GET /foo/&lt;script&gt;stuff&lt;/script&gt;\n', done);
});
it('shoud not fire after headers sent', function(done){
......@@ -168,3 +163,53 @@ 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 hostname = addr.family === 'IPv6' ? '[::1]' : '127.0.0.1';
var port = addr.port;
var req = http.get({
host: hostname,
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
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment