Skip to content

Commit

Permalink
native base64 encoding, fix for binary attachments (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamine authored and charlierudolph committed Jun 24, 2016
1 parent f6089be commit 3a2861d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
7 changes: 5 additions & 2 deletions lib/cucumber/listener/json_formatter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* jshint -W106 */
function JsonFormatter(options) {
var Cucumber = require('../../cucumber');
var base64 = require('base-64');

var self = Cucumber.Listener.Formatter(options);

Expand Down Expand Up @@ -86,8 +85,12 @@ function JsonFormatter(options) {

if (stepResult.hasAttachments()) {
currentStep.embeddings = stepResult.getAttachments().map(function (attachment) {
var data = attachment.getData();
if (!(data instanceof Buffer)) {
data = new Buffer(data);
}
return {
data: base64.encode(attachment.getData()),
data: data.toString('base64'),
mime_type: attachment.getMimeType(),
};
});
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
"node": ">=0.10"
},
"dependencies": {
"base-64": "^0.1.0",
"callsite": "^1.0.0",
"camel-case": "^3.0.0",
"cli-table": "^0.3.1",
Expand Down
15 changes: 13 additions & 2 deletions spec/cucumber/listener/json_formatter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require('../../support/spec_helper');

describe("Cucumber.Listener.JsonFormatter", function () {
var Cucumber = requireLib('cucumber');
var fs = require('fs');
var jsonFormatter, options;

beforeEach(function () {
Expand Down Expand Up @@ -244,7 +245,13 @@ describe("Cucumber.Listener.JsonFormatter", function () {
beforeEach(function (){
var attachment1 = createSpyWithStubs("first attachment", {getMimeType: "first mime type", getData: "first data"});
var attachment2 = createSpyWithStubs("second attachment", {getMimeType: "second mime type", getData: "second data"});
var attachments = [attachment1, attachment2];
var favicon = fs.readFileSync('example/images/favicon.png');
var attachment3 = createSpyWithStubs("third attachment", {
getMimeType: "image/png",
getData: favicon
});
this.faviconBase64 = favicon.toString('base64');
var attachments = [attachment1, attachment2, attachment3];
stepResult.hasAttachments.and.returnValue(true);
stepResult.getAttachments.and.returnValue(attachments);
jsonFormatter.handleStepResultEvent(stepResult);
Expand All @@ -257,7 +264,11 @@ describe("Cucumber.Listener.JsonFormatter", function () {
var features = JSON.parse(json);
expect(features[0].elements[0].steps[0].embeddings).toEqual([
{data: 'Zmlyc3QgZGF0YQ==', mime_type: 'first mime type'},
{data: 'c2Vjb25kIGRhdGE=', mime_type: 'second mime type'}
{data: 'c2Vjb25kIGRhdGE=', mime_type: 'second mime type'},
{
data: this.faviconBase64,
mime_type: 'image/png'
}
]);
});
});
Expand Down

0 comments on commit 3a2861d

Please sign in to comment.