Skip to content

Commit 990b74d

Browse files
committed
test: add upload error handling tests for abort signal and invalid responses
1 parent b40208d commit 990b74d

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

test/upload.js

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ describe("File upload", async function () {
221221
};
222222

223223
const uploadPromise = upload(fileOptions);
224-
expect(server.requests.length).to.be.equal(1);
225-
await sleep();
226-
// Simulate network error on upload API
227-
server.requests[0].error();
228-
await sleep();
224+
expect(server.requests.length).to.be.equal(1);
225+
await sleep();
226+
// Simulate network error on upload API
227+
server.requests[0].error();
228+
await sleep();
229229
try {
230230
await uploadPromise;
231231
throw new Error('Should have thrown error');
@@ -1333,4 +1333,63 @@ describe("File upload", async function () {
13331333
expect(ex.reason).to.be.equal("abort reason");
13341334
}
13351335
});
1336+
1337+
it("Already aborted signal should abort upload immediately", async function () {
1338+
const abortController = new AbortController();
1339+
// Abort the signal before calling upload
1340+
abortController.abort();
1341+
const fileOptions = {
1342+
...securityParameters,
1343+
fileName: "test_file_name",
1344+
file: "test_file",
1345+
signal: abortController.signal
1346+
};
1347+
try {
1348+
await upload(fileOptions);
1349+
throw new Error("Should have thrown error");
1350+
} catch (ex) {
1351+
expect(ex instanceof ImageKitAbortError).to.be.true;
1352+
expect(ex.reason && ex.reason.name).to.be.equal("AbortError");
1353+
}
1354+
});
1355+
1356+
it("Error during upload 4xx with invalid JSON response", async function () {
1357+
const fileOptions = {
1358+
...securityParameters,
1359+
fileName: "test_file_name",
1360+
file: "test_file"
1361+
};
1362+
const uploadPromise = upload(fileOptions);
1363+
// errorUploadResponse(400, `{sd`);
1364+
server.respondWith("POST", "https://upload.imagekit.io/api/v1/files/upload",
1365+
[
1366+
400,
1367+
{ "Content-Type": "application/json" },
1368+
"sdf"
1369+
]
1370+
);
1371+
server.respond();
1372+
try {
1373+
await uploadPromise;
1374+
throw new Error("Should have thrown error");
1375+
} catch (ex) {
1376+
expect(ex).to.be.instanceOf(SyntaxError);
1377+
}
1378+
});
1379+
1380+
it("Should return error for an invalid transformation object in upload", async function () {
1381+
const fileOptions = {
1382+
...securityParameters,
1383+
fileName: "test_file_name",
1384+
file: "test_file",
1385+
transformation: 123
1386+
};
1387+
try {
1388+
await upload(fileOptions);
1389+
throw new Error("Should have thrown error");
1390+
} catch (ex) {
1391+
expect(ex instanceof ImageKitInvalidRequestError).to.be.true;
1392+
expect(ex.message).to.be.equal("Invalid transformation parameter. Please include at least pre, post, or both.");
1393+
}
1394+
});
13361395
});

0 commit comments

Comments
 (0)