From 3a2861d458e30f24ea04c21635a1c9abc8c5ef4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjam=C3=ADn=20Eidelman?= Date: Fri, 24 Jun 2016 08:11:51 +0200 Subject: [PATCH] native base64 encoding, fix for binary attachments (#589) --- lib/cucumber/listener/json_formatter.js | 7 +++++-- package.json | 1 - spec/cucumber/listener/json_formatter_spec.js | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/cucumber/listener/json_formatter.js b/lib/cucumber/listener/json_formatter.js index 0c1a0760a..5520facb4 100644 --- a/lib/cucumber/listener/json_formatter.js +++ b/lib/cucumber/listener/json_formatter.js @@ -1,7 +1,6 @@ /* jshint -W106 */ function JsonFormatter(options) { var Cucumber = require('../../cucumber'); - var base64 = require('base-64'); var self = Cucumber.Listener.Formatter(options); @@ -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(), }; }); diff --git a/package.json b/package.json index 75aff913f..fada6cdf8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/spec/cucumber/listener/json_formatter_spec.js b/spec/cucumber/listener/json_formatter_spec.js index 1e2f9ce84..d567677c7 100644 --- a/spec/cucumber/listener/json_formatter_spec.js +++ b/spec/cucumber/listener/json_formatter_spec.js @@ -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 () { @@ -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); @@ -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' + } ]); }); });