Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Coverage. For #11

  • Loading branch information...
commit a544365e0a53ce4c902ea5ffd8d2a69054521d60 1 parent b9bc7b0
@hueniverse hueniverse authored
Showing with 108 additions and 12 deletions.
  1. +1 −1  Makefile
  2. +8 −10 lib/index.js
  3. +99 −1 test/index.js
View
2  Makefile
@@ -1,7 +1,7 @@
test:
@node node_modules/lab/bin/lab
test-cov:
- @node node_modules/lab/bin/lab -r threshold -t 100
+ @node node_modules/lab/bin/lab -t 100
test-cov-html:
@node node_modules/lab/bin/lab -r html -o coverage.html
View
18 lib/index.js
@@ -71,7 +71,10 @@ internals.Request.prototype._read = function (size) {
setImmediate(function () {
if (self._shot.isDone) {
- self.push(null);
+ if (self._shot.simulate.end !== false) { // 'end' defaults to true
+ self.push(null);
+ }
+
return;
}
@@ -192,15 +195,10 @@ internals.finish = function (response, req, onEnd) {
var raw = [];
var rawLength = 0;
- if (response.output &&
- response.output instanceof Array &&
- response.output.length) {
-
- for (var i = 0, il = response.output.length; i < il; ++i) {
- var chunk = (response.output[i] instanceof Buffer ? response.output[i] : new Buffer(response.output[i], response.outputEncodings[i]));
- raw.push(chunk);
- rawLength += chunk.length;
- }
+ for (var i = 0, il = response.output.length; i < il; ++i) {
+ var chunk = (response.output[i] instanceof Buffer ? response.output[i] : new Buffer(response.output[i], response.outputEncodings[i]));
+ raw.push(chunk);
+ rawLength += chunk.length;
}
var rawBuffer = Buffer.concat(raw, rawLength);
View
100 test/index.js
@@ -54,7 +54,7 @@ describe('Shot', function () {
res.end(req.headers.host + '|' + req.url);
};
- Shot.inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }, function (res) {
+ Shot.inject(dispatch, { url: 'http://example.com:8080/hello' }, function (res) {
expect(res.headers.date).to.exist;
expect(res.headers.connection).to.exist;
@@ -79,6 +79,21 @@ describe('Shot', function () {
});
});
+ it('leaves user-agent unmodified', function (done) {
+
+ var dispatch = function (req, res) {
+
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
+ res.end(req.headers['user-agent']);
+ };
+
+ Shot.inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello', headers: { 'user-agent': 'duper' } }, function (res) {
+
+ expect(res.payload).to.equal('duper');
+ done();
+ });
+ });
+
it('returns chunked payload', function (done) {
var dispatch = function (req, res) {
@@ -300,6 +315,37 @@ describe('Shot', function () {
done();
});
});
+
+ it('echos object payload without payload', function (done) {
+
+ var dispatch = function (req, res) {
+
+ res.writeHead(200);
+ req.pipe(res);
+ };
+
+ Shot.inject(dispatch, { method: 'post', url: '/test' }, function (res) {
+
+ expect(res.payload).to.equal('');
+ done();
+ });
+ });
+
+ it('retains content-type header', function (done) {
+
+ var dispatch = function (req, res) {
+
+ res.writeHead(200, { 'content-type': req.headers['content-type'] });
+ req.pipe(res);
+ };
+
+ Shot.inject(dispatch, { method: 'post', url: '/test', payload: { a: 1 }, headers: { 'content-type': 'something' } }, function (res) {
+
+ expect(res.headers['content-type']).to.equal('something');
+ expect(res.payload).to.equal('{"a":1}');
+ done();
+ });
+ });
});
describe('#writeHead', function () {
@@ -409,6 +455,58 @@ describe('Shot', function () {
});
});
+ it('simulates no end without payload', function (done) {
+
+ var end = false;
+ var dispatch = function (req, res) {
+
+ req.resume();
+ req.on('end', function () {
+
+ end = true;
+ });
+ };
+
+ var replied = false;
+ Shot.inject(dispatch, { method: 'get', url: '/', simulate: { end: false } }, function (res) {
+
+ replied = true;
+ });
+
+ setTimeout(function () {
+
+ expect(end).to.equal(false);
+ expect(replied).to.equal(false);
+ done();
+ }, 10);
+ });
+
+ it('simulates no end with payload', function (done) {
+
+ var end = false;
+ var dispatch = function (req, res) {
+
+ req.resume();
+ req.on('end', function () {
+
+ end = true;
+ });
+ };
+
+ var replied = false;
+ Shot.inject(dispatch, { method: 'get', url: '/', payload: '1234567', simulate: { end: false } }, function (res) {
+
+ replied = true;
+ });
+
+ setTimeout(function () {
+
+ expect(end).to.equal(false);
+ expect(replied).to.equal(false);
+ done();
+ }, 10);
+ });
+
it('simulates close', function (done) {
var dispatch = function (req, res) {
Please sign in to comment.
Something went wrong with that request. Please try again.