Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Resource: getStream for empty string #249

Merged
merged 6 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary but doesn't hurt


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