Skip to content

Commit

Permalink
Build the response body with Buffer to prevent Express from setting d…
Browse files Browse the repository at this point in the history
…efault utf-8 charset.

Remove default encoding on request.
  • Loading branch information
marcelogo committed Mar 16, 2015
1 parent fa0e6c8 commit 9a49bc8
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 18 deletions.
1 change: 0 additions & 1 deletion lib/middleware/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var logger = require('../logger');

exports.getBody = function(req, res, next) {
req.body = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
req.body += chunk;
});
Expand Down
16 changes: 15 additions & 1 deletion lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@ exports.getRouteHandlers = function (method, parsedUrl, action) {

var getResponseHandler = function (specPairs) {
return function (req, res) {
var buildResponseBody = function(specBody){
switch (typeof specBody) {
case 'boolean':
case 'number':
case 'string':
return new Buffer(specBody);
case 'object':
return new Buffer(JSON.stringify(specBody));
default:
return specBody;
}
};

var matchRequests= function (specPair){
if (content.matches(req, specPair.request)) {
logger.log('[DRAKOV]'.red, action.method.green, parsedUrl.uriTemplate.yellow, (specPair.request && specPair.request.description ? specPair.request.description : action.name).blue);

specPair.response.headers.forEach(function (header) {
res.set(header.name, header.value);
});
res.status(+specPair.response.name).send(specPair.response.body);
res.status(+specPair.response.name);
res.send(buildResponseBody(specPair.response.body));
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion test/api/headers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('HEADERS', function(){
request.get('/things')
.set('Content-Type', 'application/json')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({'header':'json'})
.end(helper.endCb(done));
});
Expand Down
10 changes: 5 additions & 5 deletions test/api/multiple-examples-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('/api/multiple', function(){
request.get('/api/multiple')
.set('Custom-header', 'First')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({'first': 'response'})
.end(helper.endCb(done));
});
Expand All @@ -25,7 +25,7 @@ describe('/api/multiple', function(){
request.get('/api/multiple')
.set('Custom-header', 'Second')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({'second': 'response'})
.end(helper.endCb(done));
});
Expand All @@ -37,7 +37,7 @@ describe('/api/multiple', function(){
.set('Content-type', 'application/json')
.send({'first': 'example'})
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({'first': 'example','status': 'ok'})
.end(helper.endCb(done));
});
Expand All @@ -47,7 +47,7 @@ describe('/api/multiple', function(){
.set('Content-type', 'application/json')
.send({'second': 'example'})
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({'second': 'example','status': 'ok'})
.end(helper.endCb(done));
});
Expand All @@ -57,7 +57,7 @@ describe('/api/multiple', function(){
.set('Content-type', 'application/x-www-form-urlencoded')
.send({third: 'example'})
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({third: 'example',status: 'ok'})
.end(helper.endCb(done));
});
Expand Down
14 changes: 7 additions & 7 deletions test/api/query-parameter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Query Parameters', function(){
it('should respond with response specified in a endpoint with no parameters', function(done){
request.get('/api/query')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({id: 'raw'})
.end(helper.endCb(done));
});
Expand All @@ -24,7 +24,7 @@ describe('Query Parameters', function(){
it('should respond with response specified in a endpoint with "param1" parameter', function(done){
request.get('/api/query?param1=1')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({id: 'parameter1'})
.end(helper.endCb(done));
});
Expand All @@ -34,15 +34,15 @@ describe('Query Parameters', function(){
it('should respond with response specified in a endpoint with "param2" parameter', function(done){
request.get('/api/query?param2=2')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({id: 'parameter2'})
.end(helper.endCb(done));
});

it('should respond with response specified in a endpoint with "param2" parameter - scenario 2', function(done){
request.get('/api/query?param2=2&param7=7')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({id: 'parameter2'})
.end(helper.endCb(done));
});
Expand All @@ -52,14 +52,14 @@ describe('Query Parameters', function(){
it('should respond with response specified in a endpoint with "param1" and "param2" parameters - scenario 1', function(done){
request.get('/api/query?param1=1&param2=2')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({id: 'parameter1_parameter2'})
.end(helper.endCb(done));
});
it('should respond with response specified in a endpoint with "param1" and "param2" parameters - scenario 2 (inverted position)', function(done){
request.get('/api/query?param2=2&param1=1')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({id: 'parameter1_parameter2'})
.end(helper.endCb(done));
});
Expand All @@ -69,7 +69,7 @@ describe('Query Parameters', function(){
it('should respond with response specified in a endpoint with "param2" and "param3" parameters', function(done){
request.get('/api/query?param2=2&param3=3')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({id: 'parameter2_parameter3'})
.end(helper.endCb(done));
});
Expand Down
18 changes: 15 additions & 3 deletions test/api/simple-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Simple-API', function(){
it('should respond with json collection from contract example', function(done){
request.get('/api/things')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect([
{text:'Zip2',id: '1'},
{text: 'X.com', id: '2'},
Expand All @@ -33,7 +33,7 @@ describe('Simple-API', function(){
it('should respond with json object from contract example', function(done){
request.get('/api/things/1111')
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect([{text: 'Zip2', id: '1'}
])
.end(helper.endCb(done));
Expand All @@ -46,11 +46,23 @@ describe('Simple-API', function(){
.set('Content-type', 'application/json')
.send({text: 'Hyperspeed jet', id: '1'})
.expect(200)
.expect('Content-type', 'application/json; charset=utf-8')
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({text: 'Hyperspeed jet', id: '1'})
.end(helper.endCb(done));
});
});
});

describe('/api/charsetless', function(){
describe('GET', function(){
it('should not include charset on the response`s content-type', function(done){
request.get('/api/charsetless')
.expect(200)
.expect('Content-type', 'application/json')
.expect({'charset':'not present', 'id': '1'})
.end(helper.endCb(done));
});
});
});

});
14 changes: 14 additions & 0 deletions test/example/md/simple-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,17 @@ Update the text of the thing
+ Body

{ "like": true }


## Things undefined charset [/api/charsetless]

### Retrieve all the things and not add charset on the response [GET]

+ Response 200 (application/json)

+ Body

{
"charset":"not present",
"id": "1"
}

0 comments on commit 9a49bc8

Please sign in to comment.