Skip to content

Commit

Permalink
Merge pull request #184 from chaijs/fix-178
Browse files Browse the repository at this point in the history
Fix 178
  • Loading branch information
vieiralucas committed Oct 31, 2017
2 parents 80c880c + 3d7ab70 commit cee5dbd
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -61,6 +61,24 @@ chai.request(app)
.get('/')
```

When passing an `app` to `request`; it will automatically open the server for
incoming requests (by calling `listen()`) and, once a request has been made
the server will automatically shut down (by calling `.close()`). If you want to
keep the server open, perhaps if you're making multiple requests, you must call
`.keepOpen()` after `.request()`, and manually close the server down:

```js
var requester = chai.request(app).keepOpen()

Promise.all([
requester.get('/a'),
requester.get('/b'),
])
.then(responses => ....)
.then(() => requester.close())
```


#### URL

You may also use a base url as the foundation of your request.
Expand Down
38 changes: 34 additions & 4 deletions lib/request.js
Expand Up @@ -214,13 +214,31 @@ module.exports = function (app) {
: app
, obj = {};

var keepOpen = false
if (typeof server !== 'string' && server && server.listen && server.address) {
if (!server.address()) {
server = server.listen(0)
}
}
obj.keepOpen = function() {
keepOpen = true
return this
}
obj.close = function(callback) {
if (server && server.close && keepOpen === false) {
server.close(callback);
}
return this
}
methods.forEach(function (method) {
obj[method] = function (path) {
return new Test(server, method, path);
return new Test(server, method, path)
.on('end', function() {
obj.close();
});
};
});
obj.del = obj.delete;

return obj;
};

Expand Down Expand Up @@ -257,8 +275,7 @@ function serverAddress (app, path) {
}
var addr = app.address();
if (!addr) {
app.listen(0);
addr = app.address();
throw new Error('Server is not listening')
}
var protocol = (app instanceof https.Server) ? 'https' : 'http';
// If address is "unroutable" IPv4/6 address, then set to localhost
Expand Down Expand Up @@ -286,9 +303,22 @@ function TestAgent(app) {
if (typeof app === 'function') app = http.createServer(app);
(Agent || Request).call(this);
this.app = app;
if (typeof app !== 'string' && app && app.listen && app.address && !app.address()) {
this.app = app.listen(0)
}
}
util.inherits(TestAgent, Agent || Request);

TestAgent.prototype.close = function close(callback) {
if (this.app && this.app.close) {
this.app.close(callback)
}
return this
}
TestAgent.prototype.keepOpen = function keepOpen() {
return this
}

// override HTTP verb methods
methods.forEach(function(method){
TestAgent.prototype[method] = function(url){
Expand Down
18 changes: 9 additions & 9 deletions package.json
Expand Up @@ -47,22 +47,22 @@
"querystring": "qs"
},
"dependencies": {
"cookiejar": "2.1.0",
"is-ip": "1.0.0",
"cookiejar": "^2.1.1",
"is-ip": "^2.0.0",
"methods": "^1.1.2",
"qs": "^6.2.0",
"qs": "^6.5.1",
"superagent": "^3.7.0"
},
"devDependencies": {
"simplifyify": "^2.0.1",
"chai": "4",
"coveralls": "^2.11.9",
"dox": "^0.8.1",
"coveralls": "^3.0.0",
"dox": "^0.9.0",
"es6-shim": "^0.35.1",
"http-server": "^0.9.0",
"http-server": "^0.10.0",
"istanbul": "^0.4.3",
"mocha": "^3.0.2",
"npm-run-all": "^3.0.0"
"mocha": "^4.0.1",
"npm-run-all": "^4.1.1",
"simplifyify": "^3.2.4"
},
"engines": {
"node": ">=4"
Expand Down
35 changes: 34 additions & 1 deletion test/request.js
Expand Up @@ -123,9 +123,10 @@ describe('request', function () {
request(app).get('/')
.set('X-API-Key', 'testing')
.end(function (err, res) {
if (err) return done(err)
res.should.have.status(200);
res.text.should.equal('hello universe');
done(err);
done();
});
});

Expand Down Expand Up @@ -169,9 +170,41 @@ describe('request', function () {
})
.then(function (res) {
res.text.should.equal('your cookie: mycookie=test');
agent.close()
})
.then(done, done);
});

it('automatically closes the server down once done with it', function (done) {
var server = require('http').createServer(function (req, res) {
res.writeHeader(200, { 'content-type' : 'text/plain' });
res.end('hello world');
});

request(server)
.get('/')
.end(function (err, res) {
res.should.have.status(200);
res.text.should.equal('hello world');
should.not.exist(server.address())
done(err)
});
});

it('can use keepOpen() to not close the server', function (done) {
var server = require('http').createServer(function (req, res) {
res.writeHeader(200, { 'content-type' : 'text/plain' });
res.end('hello world');
});
var cachedRequest = request(server).keepOpen();
server.listen = function () { throw new Error('listen was called when it shouldnt have been') }
cachedRequest.get('/') .end(function (err, res) {
cachedRequest.get('/').end(function (err2, res) {
server.close(function () { done(err || err2) })
})
});
});

});

isBrowser && describe('Browser', function () {
Expand Down

0 comments on commit cee5dbd

Please sign in to comment.