Skip to content

Commit

Permalink
[FIX] Resource: getStream for empty string (#249)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tobiasso85 committed Jun 24, 2020
1 parent b3eb22f commit bc5eafb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
59 changes: 49 additions & 10 deletions test/lib/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} 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({});
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit bc5eafb

Please sign in to comment.