Skip to content

Commit 40b7321

Browse files
committed
Fix issue with non-ASCII characters in Buffer and Stream attachments
1 parent b58fdbe commit 40b7321

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

features/attachments.feature

+25-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ Feature: Attachments
1919
"""
2020
var hooks = function () {
2121
this.Before(function(scenario, callback) {
22-
scenario.attach(new Buffer([100, 97, 116, 97]), 'image/png');
22+
var data = [];
23+
24+
for (var i = 0; i < 256; i++) {
25+
data.push(i);
26+
}
27+
28+
scenario.attach(new Buffer(data), 'image/png');
2329
callback();
2430
});
2531
};
@@ -57,7 +63,7 @@ Feature: Attachments
5763
"embeddings": [
5864
{
5965
"mime_type": "image/png",
60-
"data": "ZGF0YQ=="
66+
"data": "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=="
6167
}
6268
]
6369
},
@@ -98,22 +104,33 @@ Feature: Attachments
98104
var hooks = function () {
99105
this.Before(function(scenario, callback) {
100106
var Stream = require('stream');
101-
var versionParts = /v(\d+)\.(\d+)\.(\d+)/.exec(process.version);
102-
var major = parseInt(versionParts[0], 10);
103-
var minor = parseInt(versionParts[1], 10);
107+
var versionParts = process.version.match(/v(\d+)\.(\d+)\.(\d+)/);
108+
var major = parseInt(versionParts[1], 10);
109+
var minor = parseInt(versionParts[2], 10);
110+
var data1 = [];
111+
var data2 = [];
112+
113+
for (var i = 0; i < 128; i++) {
114+
data1.push(i);
115+
}
116+
117+
for (var i = 128; i < 256; i++) {
118+
data2.push(i);
119+
}
104120
105121
if (major > 0 || minor >= 10) {
106122
var stream = new Stream.Readable();
107123
stream._read = function() {};
108-
stream.push(new Buffer([100, 97, 116, 97]));
124+
stream.push(new Buffer(data1));
125+
stream.push(new Buffer(data2));
109126
stream.push(null);
110127
111128
scenario.attach(stream, 'image/png', function(error) {
112129
callback(error);
113130
});
114131
}
115132
else {
116-
scenario.attach(new Buffer([100, 97, 116, 97]), 'image/png');
133+
scenario.attach(new Buffer([].concat(data1, data2)), 'image/png');
117134
callback();
118135
}
119136
});
@@ -152,7 +169,7 @@ Feature: Attachments
152169
"embeddings": [
153170
{
154171
"mime_type": "image/png",
155-
"data": "ZGF0YQ=="
172+
"data": "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=="
156173
}
157174
]
158175
},

lib/cucumber/api/scenario.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ var Scenario = function (astTreeWalker, astScenario) {
2323

2424
data.on('data', function(chunk) {
2525
buffers.push(chunk);
26-
})
26+
});
2727
data.on('end', function() {
28-
astTreeWalker.attach(Buffer.concat(buffers).toString(), mimeType);
28+
astTreeWalker.attach(Buffer.concat(buffers).toString('binary'), mimeType);
2929

3030
callback();
3131
});
@@ -34,7 +34,7 @@ var Scenario = function (astTreeWalker, astScenario) {
3434
if (!mimeType)
3535
throw Error(Scenario.ATTACH_MISSING_MIME_TYPE_ARGUMENT);
3636

37-
astTreeWalker.attach(data.toString(), mimeType);
37+
astTreeWalker.attach(data.toString('binary'), mimeType);
3838

3939
if (callback) {
4040
callback();

spec/cucumber/api/scenario_spec.js

+48
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,33 @@ describe("Cucumber.Api.Scenario", function() {
164164
expect(callback).toHaveBeenCalled();
165165
});
166166
});
167+
168+
describe("when the stream finishes providing data and the data contains non-ASCII characters", function() {
169+
var data1, data2, text;
170+
171+
beforeEach(function() {
172+
data1 = [];
173+
data2 = [];
174+
175+
for (var i = 0; i < 256; i++) {
176+
data1.push(i);
177+
data2.push(i);
178+
}
179+
180+
dataListener(new Buffer(data1));
181+
dataListener(new Buffer(data2));
182+
text = String.fromCharCode.apply(null, [].concat(data1, data2));
183+
endListener();
184+
});
185+
186+
it("instructs the ast tree walker to create an attachment containing the contents of the stream", function() {
187+
expect(astTreeWalker.attach).toHaveBeenCalledWith(text, mimeType);
188+
});
189+
190+
it("calls back", function() {
191+
expect(callback).toHaveBeenCalled();
192+
});
193+
});
167194
})
168195
});
169196
}
@@ -203,6 +230,27 @@ describe("Cucumber.Api.Scenario", function() {
203230
expect(callback).not.toHaveBeenCalled();
204231
});
205232
});
233+
234+
describe("when the data contains non-ASCII characters", function() {
235+
var data, buffer, text;
236+
237+
beforeEach(function() {
238+
data = [];
239+
240+
for (var i = 0; i < 256; i++) {
241+
data.push(i);
242+
}
243+
244+
buffer = new Buffer(data);
245+
text = String.fromCharCode.apply(null, data);
246+
scenario.attach(buffer, mimeType, callback);
247+
});
248+
249+
it("instructs the ast tree walker to create an attachment containing the contents of the buffer", function() {
250+
scenario.attach(buffer, mimeType);
251+
expect(astTreeWalker.attach).toHaveBeenCalledWith(text, mimeType);
252+
});
253+
});
206254
});
207255

208256
describe("when the data is a string", function() {

0 commit comments

Comments
 (0)