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

Commit 677b572

Browse files
committed
fix: encoding of binary files send via endpoints
1 parent 2fb659a commit 677b572

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

server/api/setup/supportedMethod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = function (server, proposedRequest, settings, params, path) {
3131

3232
if (typeof proposedRequest.response === 'string') {
3333
const filePath = Path.normalize(Path.join(__dirname, '../../../', proposedRequest.response));
34-
response = Fs.readFileSync(filePath, 'utf8');
34+
response = Fs.readFileSync(filePath);
3535
}
3636
else {
3737
response = proposedRequest.response;

test/server/api/endpoint.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ lab.experiment('Setup endpoints', () => {
136136
server.inject(request, (response) => {
137137

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

141141
done();
142142
});
@@ -168,7 +168,7 @@ lab.experiment('Setup endpoints', () => {
168168
server.inject(request, (response) => {
169169

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

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

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

189189
done();
190190
});
@@ -216,7 +216,7 @@ lab.experiment('Setup endpoints', () => {
216216
server.inject(request, (response) => {
217217

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

221221
done();
222222
});

test/server/api/fileTypes.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const Lab = require('lab');
44
const Code = require('code');
55
const Config = require('../../../config');
66
const Hapi = require('hapi');
7+
const Fs = require('fs');
8+
const Path = require('path');
79
const SetupEndpoint = require('../../../server/api/setup/');
810

911
const apiUrlPrefix = Config.get('/apiUrlPrefix');
@@ -15,6 +17,12 @@ const Endpoint = SetupEndpoint({
1517
requests: [{
1618
response: '/test/server/api/fixtures/response.json'
1719
}]
20+
}, {
21+
params: '/json/download',
22+
requests: [{
23+
response: '/test/server/api/fixtures/response.json',
24+
sendFile: true
25+
}]
1826
}, {
1927
params: '/text',
2028
requests: [{
@@ -86,6 +94,7 @@ lab.experiment('Different file types', () => {
8694
server.inject(request, (response) => {
8795

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

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

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

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

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

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

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

142+
lab.test('ascii files have correctly encoded content', (done) => {
143+
144+
request = {
145+
method: 'GET',
146+
url: apiUrlPrefix + '/fileTypes/json/download'
147+
};
148+
149+
server.inject(request, (response) => {
150+
151+
Code.expect(JSON.parse(response.result)).to.equal({ response: '🐷' });
152+
153+
done();
154+
});
155+
});
156+
157+
lab.test('binary files have correctly encoded content', (done) => {
158+
159+
request = {
160+
method: 'GET',
161+
url: apiUrlPrefix + '/fileTypes/pdf'
162+
};
163+
164+
const fixtureContent = Fs.readFileSync(Path.normalize(Path.join(__dirname, '/fixtures/example.pdf'))).toString();
165+
166+
server.inject(request, (response) => {
167+
168+
Code.expect(response.result).to.equal(fixtureContent);
169+
170+
done();
171+
});
172+
});
173+
133174
lab.test('send files with the default content-type and the correct name`', (done) => {
134175

135176
request = {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<a href="https://github.com">GitHub</a>
1+
<a href="https://github.com">GitHub 💖</a>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"response": "Yeah"
2+
"response": "🐷"
33
}

test/server/api/fixtures/response.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This is just a plain old text file.
1+
This is just a plain old text file

0 commit comments

Comments
 (0)