Skip to content

Commit b84d8bd

Browse files
committed
test coverage increase
1 parent c45e1eb commit b84d8bd

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

test/upload.js

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ describe("File upload", function () {
107107

108108
imagekit.upload(undefined, callback);
109109
expect(server.requests.length).to.be.equal(0);
110-
// await sleep()
111110
expect(callback.calledOnce).to.be.true;
112111
sinon.assert.calledWith(callback, { help: "", message: "Invalid uploadOptions parameter" }, null);
113112
});
@@ -208,6 +207,57 @@ describe("File upload", function () {
208207
sinon.assert.calledWith(callback, { message: "Invalid response from authenticationEndpoint. The SDK expects a JSON response with three fields i.e. signature, token and expire.", help: "" }, null);
209208
});
210209

210+
it('Auth endpoint 200 status with bad body', async function () {
211+
const fileOptions = {
212+
fileName: "test_file_name",
213+
file: "test_file"
214+
};
215+
216+
var callback = sinon.spy();
217+
218+
imagekit.upload(fileOptions, callback);
219+
220+
expect(server.requests.length).to.be.equal(2);
221+
222+
// Simulate non 200 response on authentication endpoint
223+
server.respondWith("GET", initializationParams.authenticationEndpoint,
224+
[
225+
200,
226+
{ "Content-Type": "application/json" },
227+
"invalid json"
228+
]);
229+
server.respond();
230+
await sleep();
231+
sinon.assert.calledWith(callback, { message: "Invalid response from authenticationEndpoint. The SDK expects a JSON response with three fields i.e. signature, token and expire.", help: "" }, null);
232+
});
233+
234+
it('Auth endpoint 200 status missing token', async function () {
235+
const fileOptions = {
236+
fileName: "test_file_name",
237+
file: "test_file"
238+
};
239+
240+
var callback = sinon.spy();
241+
242+
imagekit.upload(fileOptions, callback);
243+
244+
expect(server.requests.length).to.be.equal(2);
245+
246+
// Simulate non 200 response on authentication endpoint
247+
server.respondWith("GET", initializationParams.authenticationEndpoint,
248+
[
249+
200,
250+
{ "Content-Type": "application/json" },
251+
JSON.stringify({
252+
signature: "sig",
253+
timestamp: "123"
254+
})
255+
]);
256+
server.respond();
257+
await sleep();
258+
sinon.assert.calledWith(callback, { message: "Invalid response from authenticationEndpoint. The SDK expects a JSON response with three fields i.e. signature, token and expire.", help: "" }, null);
259+
});
260+
211261
it('Upload endpoint network error handling', async function () {
212262
const fileOptions = {
213263
fileName: "test_file_name",
@@ -509,6 +559,60 @@ describe("File upload", function () {
509559
sinon.assert.calledWith(callback, errRes, null);
510560
});
511561

562+
it('Error during upload non 2xx with bad body', async function () {
563+
const fileOptions = {
564+
fileName: "test_file_name",
565+
file: "test_file"
566+
};
567+
568+
var callback = sinon.spy();
569+
570+
imagekit.upload(fileOptions, callback);
571+
572+
expect(server.requests.length).to.be.equal(2);
573+
successSignature();
574+
await sleep();
575+
server.respondWith("POST", "https://upload.imagekit.io/api/v1/files/upload",
576+
[
577+
500,
578+
{ "Content-Type": "application/json" },
579+
"sdf"
580+
]
581+
);
582+
server.respond();
583+
await sleep();
584+
expect(callback.calledOnce).to.be.true;
585+
var error = callback.args[0][0];
586+
expect(error instanceof SyntaxError).to.be.true;
587+
});
588+
589+
it('Error during upload 2xx with bad body', async function () {
590+
const fileOptions = {
591+
fileName: "test_file_name",
592+
file: "test_file"
593+
};
594+
595+
var callback = sinon.spy();
596+
597+
imagekit.upload(fileOptions, callback);
598+
599+
expect(server.requests.length).to.be.equal(2);
600+
successSignature();
601+
await sleep();
602+
server.respondWith("POST", "https://upload.imagekit.io/api/v1/files/upload",
603+
[
604+
200,
605+
{ "Content-Type": "application/json" },
606+
"sdf"
607+
]
608+
);
609+
server.respond();
610+
await sleep();
611+
expect(callback.calledOnce).to.be.true;
612+
var error = callback.args[0][0];
613+
expect(error instanceof SyntaxError).to.be.true;
614+
});
615+
512616
it('Upload via URL', async function () {
513617
const fileOptions = {
514618
fileName: "test_file_name",

0 commit comments

Comments
 (0)