forked from globocom/functions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[globocom#11] Introduces SandboxResponse
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
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |