Skip to content

Commit

Permalink
[globocom#11] Introduces SandboxResponse
Browse files Browse the repository at this point in the history
As described on the issue globocom#11 this introuces the SandboxResponse object
that makes possible for the function to send custom body, headers and
status code
  • Loading branch information
pedrosnk committed Oct 10, 2016
1 parent a27e5bc commit d4b4cdb
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/domain/sandbox/SandboxResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

class SandboxResponse {
constructor({ callback }) {
this.callback = callback;
this.statusCode = 200;
this.headers = {};
}

set(key, value) {
this.headers[key] = value;
}

status(statusCode) {
this.statusCode = statusCode;
return this;
}

send(body) {
const status = this.statusCode || 200;
const headers = this.headers;
this.callback(null, { status, body, headers });
}

}

module.exports = SandboxResponse;
72 changes: 72 additions & 0 deletions test/unit/domain/sandbox/SandboxResponse.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const expect = require('chai').expect;
const SandboxResponse = require('../../../../lib/domain/sandbox/SandboxResponse');

describe('SandboxResponse', () => {
describe('create a response successfully', () => {
let res;
let codeResponse;

before((done) => {
const callbackFn = (err, responseData) => {
codeResponse = responseData;
done();
};
res = new SandboxResponse({
callback: callbackFn,
});
res.send({ result: 'ok' });
});

it('should sends the response with default status of 200', () => {
expect(codeResponse.body).to.be.eql({ result: 'ok' });
expect(codeResponse.status).to.be.eql(200);
});

it('should sends an empty header object when none is defined', () => {
expect(codeResponse.headers).to.be.eql({});
});
});

describe('create a response with created status code (201)', () => {
let res;
let codeResponse;

before((done) => {
const callbackFn = (err, responseData) => {
codeResponse = responseData;
done();
};
res = new SandboxResponse({
callback: callbackFn,
});
res.status(201).send({ content: { name: 'foobar' } });
});

it('should sends the response with status of 201', () => {
expect(codeResponse.body).to.be.eql({ content: { name: 'foobar' } });
expect(codeResponse.status).to.be.eql(201);
});
});

describe('set headers to be send as response', () => {
let res;
let codeResponse;

before((done) => {
const callbackFn = (err, responseData) => {
codeResponse = responseData;
done();
};
res = new SandboxResponse({
callback: callbackFn,
});
res.set('X-FOO', 'bar');
res.send({ result: 'ok' });
});

it('should sends the response with default status of 200', () => {
expect(codeResponse.body).to.be.eql({ result: 'ok' });
expect(codeResponse.status).to.be.eql(200);
});
});
});

0 comments on commit d4b4cdb

Please sign in to comment.