From bc5eafb19bf71141b88dff749240354898849a66 Mon Sep 17 00:00:00 2001 From: Tobias Sorn Date: Wed, 24 Jun 2020 09:32:41 +0200 Subject: [PATCH] [FIX] Resource: getStream for empty string (#249) When a Resource is created parameter `string` can be passed. For an empty string `""` calling Resource#getStream threw an error: `Resource ${this._path} has no content` Now the empty string is properly set and the stream can be retrieved. --- lib/Resource.js | 2 +- test/lib/Resource.js | 59 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/lib/Resource.js b/lib/Resource.js index 14d3737a..860b68e6 100644 --- a/lib/Resource.js +++ b/lib/Resource.js @@ -73,7 +73,7 @@ class Resource { this._createStream = createStream || null; this._stream = stream || null; this._buffer = buffer || null; - if (string) { + if (typeof string === "string" || string instanceof String) { this._buffer = Buffer.from(string, "utf8"); } diff --git a/test/lib/Resource.js b/test/lib/Resource.js index 818c0591..c2400601 100644 --- a/test/lib/Resource.js +++ b/test/lib/Resource.js @@ -18,6 +18,27 @@ function createBasicResource() { return resource; } +/** + * Reads a readable stream and resolves with its content + * + * @param {stream.Readable} readableStream readable stream + * @returns {Promise} resolves with the read string + */ +const readStream = (readableStream) => { + return new Promise((resolve, reject) => { + let streamedResult = ""; + readableStream.on("data", (chunk) => { + streamedResult += chunk; + }); + readableStream.on("end", () => { + resolve(streamedResult); + }); + readableStream.on("error", (err) => { + reject(err); + }); + }); +}; + test("Resource: constructor with missing path parameter", (t) => { t.throws(() => { new Resource({}); @@ -62,20 +83,38 @@ test("Resource: getStream", async (t) => { buffer: Buffer.from("Content") }); - return new Promise(function(resolve, reject) { - let streamedResult = ""; - const readableStream = resource.getStream(); - readableStream.on("data", (chunk) => { - streamedResult += chunk; - }); - readableStream.on("end", () => { - resolve(streamedResult); - }); - }).then((result) => { + return readStream(resource.getStream()).then((result) => { t.is(result, "Content", "Stream has been read correctly"); }); }); +test("Resource: getStream for empty string", async (t) => { + t.plan(1); + + const resource = new Resource({ + path: "my/path/to/resource", + string: "" + }); + + return readStream(resource.getStream()).then((result) => { + t.is(result, "", "Stream has been read correctly for empty string"); + }); +}); + +test("Resource: getStream for empty string instance", async (t) => { + t.plan(1); + + const resource = new Resource({ + path: "my/path/to/resource", + // eslint-disable-next-line no-new-wrappers + string: new String("") + }); + + return readStream(resource.getStream()).then((result) => { + t.is(result, "", "Stream has been read correctly for empty string"); + }); +}); + test("Resource: getStream throwing an error", (t) => { const resource = new Resource({ path: "my/path/to/resource"