Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mockResponse - tests to verify write() and end() with payloads #1

Merged
merged 1 commit into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/mockResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function createResponse(options) {
var _data = '';
var _buffer = new Buffer(0);
var _chunks = [];
var _size = 0;
var _size = 0;
var _encoding = options.encoding;

var _redirectUrl = '';
Expand Down Expand Up @@ -676,10 +676,27 @@ function createResponse(options) {
return _data;
};

/**
* Function: _getBuffer
*
* The buffer containing data to be sent to the user.
* Non-empty if Buffers were given in calls to write() and end()
*/
mockResponse._getBuffer = function() {
return _buffer;
};


/**
* Function: _getChunks
*
* The buffer containing data to be sent to the user.
* Non-empty if Buffers were given in calls to write() and end()
*/
mockResponse._getChunks = function() {
return _chunks;
};

/**
* Function: _getStatusCode
*
Expand Down
81 changes: 81 additions & 0 deletions test/lib/mockResponse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,46 @@ describe('mockResponse', function() {
});

describe('.write()', function() {
var response;

beforeEach(function() {
response = mockResponse.createResponse();
});

it('should accept a string and hold it in _data', function() {
var payload1 = 'payload1';
var encoding = 'utf8';
response.write(payload1, encoding);
expect(response._getData()).to.equal(payload1);
expect(response.getEncoding()).to.equal(encoding);
});

it('should accept multiple strings and concatenate them in _data', function() {
var payload1 = 'payload1';
var payload2 = 'payload2';
response.write(payload1);
response.write(payload2);
expect(response._getData()).to.equal(payload1 + payload2);
});

it('should accept a buffer and hold it in _chunks', function() {
var payload1 = 'payload1';
response.write(new Buffer(payload1));
var chunks = response._getChunks();
expect(chunks.length).to.eql(1);
expect(chunks[0].toString()).to.equal(payload1);
});

it('should accept multiple buffers and hold them in _chunks', function() {
var payload1 = 'payload1';
var payload2 = 'payload2';
response.write(new Buffer(payload1));
response.write(new Buffer(payload2));
var chunks = response._getChunks();
expect(chunks.length).to.eql(2);
expect(chunks[0].toString()).to.equal(payload1);
expect(chunks[1].toString()).to.equal(payload2);
});

it('should inherit from Node OutogingMessage.write()');

Expand All @@ -871,12 +911,53 @@ describe('mockResponse', function() {
expect(emits).to.eql(1);
});

it('writes to _data if a string is supplied', function() {
var payload1 = 'payload1';
var encoding = 'utf8';
response.end(payload1, encoding);
expect(response._getData()).to.equal(payload1);
expect(response.getEncoding()).to.equal(encoding);
});

it('writes to _buffer if a Buffer is supplied', function() {
var payload1 = 'payload1';
response.end(new Buffer(payload1));
var buffer = response._getBuffer();
expect(buffer.toString()).to.equal(payload1);
});

it('should inherit from Node OutogingMessage.end()');

});

});

describe('write() + end() interactions', function() {
var response;

beforeEach(function() {
response = mockResponse.createResponse();
});

it('should accept strings through write() and end() and concatenate them in _data', function() {
var payload1 = 'payload1';
var payload2 = 'payload2';
response.write(payload1);
response.end(payload2);
expect(response._getData()).to.equal(payload1 + payload2);
});

it('should accept buffers through write() and end() and concatenate them in _buffer', function() {
var payload1 = 'payload1';
var payload2 = 'payload2';
response.write(new Buffer(payload1));
response.end(new Buffer(payload2));
var buffer = response._getBuffer();
expect(buffer.toString()).to.equal(payload1 + payload2);
});

});

// TODO: fix in 2.0; methods should be inherited from Node WritableStream
describe('node WritableStream methods', function() {

Expand Down