Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
fix: encoding of binary files send via endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mischah committed Dec 4, 2017
1 parent 2fb659a commit 677b572
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion server/api/setup/supportedMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = function (server, proposedRequest, settings, params, path) {

if (typeof proposedRequest.response === 'string') {
const filePath = Path.normalize(Path.join(__dirname, '../../../', proposedRequest.response));
response = Fs.readFileSync(filePath, 'utf8');
response = Fs.readFileSync(filePath);
}
else {
response = proposedRequest.response;
Expand Down
8 changes: 4 additions & 4 deletions test/server/api/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ lab.experiment('Setup endpoints', () => {
server.inject(request, (response) => {

Code.expect(response.statusCode).to.equal(200);
Code.expect(JSON.parse(response.result)).to.equal({ response: 'Yeah' });
Code.expect(JSON.parse(response.result)).to.equal({ response: '🐷' });

done();
});
Expand Down Expand Up @@ -168,7 +168,7 @@ lab.experiment('Setup endpoints', () => {
server.inject(request, (response) => {

Code.expect(response.statusCode).to.equal(200);
Code.expect(JSON.parse(response.result)).to.equal({ response: 'Yeah' });
Code.expect(JSON.parse(response.result)).to.equal({ response: '🐷' });

done();
});
Expand All @@ -184,7 +184,7 @@ lab.experiment('Setup endpoints', () => {
server.inject(request, (response) => {

Code.expect(response.statusCode).to.equal(200);
Code.expect(JSON.parse(response.result)).to.equal({ response: 'Yeah' });
Code.expect(JSON.parse(response.result)).to.equal({ response: '🐷' });

done();
});
Expand Down Expand Up @@ -216,7 +216,7 @@ lab.experiment('Setup endpoints', () => {
server.inject(request, (response) => {

Code.expect(response.statusCode).to.equal(200);
Code.expect(JSON.parse(response.result)).to.equal({ response: 'Yeah' });
Code.expect(JSON.parse(response.result)).to.equal({ response: '🐷' });

done();
});
Expand Down
45 changes: 43 additions & 2 deletions test/server/api/fileTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const Lab = require('lab');
const Code = require('code');
const Config = require('../../../config');
const Hapi = require('hapi');
const Fs = require('fs');
const Path = require('path');
const SetupEndpoint = require('../../../server/api/setup/');

const apiUrlPrefix = Config.get('/apiUrlPrefix');
Expand All @@ -15,6 +17,12 @@ const Endpoint = SetupEndpoint({
requests: [{
response: '/test/server/api/fixtures/response.json'
}]
}, {
params: '/json/download',
requests: [{
response: '/test/server/api/fixtures/response.json',
sendFile: true
}]
}, {
params: '/text',
requests: [{
Expand Down Expand Up @@ -86,6 +94,7 @@ lab.experiment('Different file types', () => {
server.inject(request, (response) => {

Code.expect(response.headers['content-type']).to.equal('application/json; charset=utf-8');
Code.expect(JSON.parse(response.result)).to.equal({ response: '🐷' });

done();
});
Expand All @@ -101,7 +110,7 @@ lab.experiment('Different file types', () => {
server.inject(request, (response) => {

Code.expect(response.headers['content-type']).to.equal('text/plain; charset=utf-8');
Code.expect(response.result).to.equal('This is just a plain old text file.\n');
Code.expect(response.result).to.equal('This is just a plain old text file\n');

done();
});
Expand All @@ -117,7 +126,7 @@ lab.experiment('Different file types', () => {
server.inject(request, (response) => {

Code.expect(response.headers['content-type']).to.equal('text/html; charset=utf-8');
Code.expect(response.result).to.equal('<a href="https://github.com">GitHub</a>\n');
Code.expect(response.result).to.equal('<a href="https://github.com">GitHub 💖</a>\n');
Code.expect(response.statusCode).to.equal(201);

done();
Expand All @@ -130,6 +139,38 @@ lab.experiment('Different file types', () => {

lab.experiment('send files ', () => {

lab.test('ascii files have correctly encoded content', (done) => {

request = {
method: 'GET',
url: apiUrlPrefix + '/fileTypes/json/download'
};

server.inject(request, (response) => {

Code.expect(JSON.parse(response.result)).to.equal({ response: '🐷' });

done();
});
});

lab.test('binary files have correctly encoded content', (done) => {

request = {
method: 'GET',
url: apiUrlPrefix + '/fileTypes/pdf'
};

const fixtureContent = Fs.readFileSync(Path.normalize(Path.join(__dirname, '/fixtures/example.pdf'))).toString();

server.inject(request, (response) => {

Code.expect(response.result).to.equal(fixtureContent);

done();
});
});

lab.test('send files with the default content-type and the correct name`', (done) => {

request = {
Expand Down
2 changes: 1 addition & 1 deletion test/server/api/fixtures/response.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a href="https://github.com">GitHub</a>
<a href="https://github.com">GitHub 💖</a>
2 changes: 1 addition & 1 deletion test/server/api/fixtures/response.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"response": "Yeah"
"response": "🐷"
}
2 changes: 1 addition & 1 deletion test/server/api/fixtures/response.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is just a plain old text file.
This is just a plain old text file

0 comments on commit 677b572

Please sign in to comment.