Skip to content

Commit 9341aa2

Browse files
committed
working upload test cases
1 parent 74b3073 commit 9341aa2

File tree

2 files changed

+77
-45
lines changed

2 files changed

+77
-45
lines changed

src/upload.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export const upload = (
188188

189189
xhr.open('POST', 'https://upload.imagekit.io/api/v1/files/upload');
190190
xhr.onerror = function (e) {
191-
return reject(new ImageKitInvalidRequestError(errorMessages.UPLOAD_ENDPOINT_NETWORK_ERROR.message));
191+
return reject(new ImageKitUploadNetworkError(errorMessages.UPLOAD_ENDPOINT_NETWORK_ERROR.message));
192192
}
193193
xhr.onload = function () {
194194
if (xhr.status >= 200 && xhr.status < 300) {
@@ -204,7 +204,7 @@ export const upload = (
204204
try {
205205
var body = JSON.parse(xhr.responseText);
206206
return reject(new ImageKitInvalidRequestError(
207-
body.message,
207+
body.message ?? "Invalid request. Please check the parameters.",
208208
getResponseMetadata(xhr)
209209
));
210210
} catch (ex: any) {
@@ -215,11 +215,14 @@ export const upload = (
215215
try {
216216
var body = JSON.parse(xhr.responseText);
217217
return reject(new ImageKitServerError(
218-
"Server error occurred while uploading the file. This is rare and usually temporary.",
218+
body.message ?? "Server error occurred while uploading the file. This is rare and usually temporary.",
219219
getResponseMetadata(xhr)
220220
));
221221
} catch (ex: any) {
222-
return reject(ex);
222+
return reject(new ImageKitServerError(
223+
"Server error occurred while uploading the file. This is rare and usually temporary.",
224+
getResponseMetadata(xhr)
225+
));
223226
}
224227
}
225228
};

test/upload.js

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ const sinon = require("sinon");
33
const expect = chai.expect;
44
import 'regenerator-runtime/runtime';
55
import { upload } from "../src/index";
6-
import { ImageKitAbortError, ImageKitInvalidRequestError, ImageKitServerError, ImageKitUploadNetworkError } from '../src/upload';
6+
import {
7+
ImageKitAbortError,
8+
ImageKitInvalidRequestError,
9+
ImageKitServerError,
10+
ImageKitUploadNetworkError
11+
} from '../src/upload';
12+
713
var requests, server;
814

915
const uploadSuccessResponseObj = {
@@ -125,6 +131,7 @@ describe("File upload", async function () {
125131
file: "test_file",
126132
signature: 'test_signature',
127133
expire: 123,
134+
// Omit token
128135
publicKey: 'test_public_key'
129136
};
130137

@@ -144,7 +151,9 @@ describe("File upload", async function () {
144151
fileName: "test_file_name",
145152
file: "test_file",
146153
token: 'test_token',
147-
expire: 123
154+
expire: 123,
155+
publicKey: 'test_public_key'
156+
// Omit signature
148157
};
149158

150159
const uploadPromise = upload(fileOptions);
@@ -153,7 +162,8 @@ describe("File upload", async function () {
153162
await uploadPromise;
154163
throw new Error('Should have thrown error');
155164
} catch (ex) {
156-
expect(ex).to.be.deep.equal({ message: "Missing signature for upload. The SDK expects token, signature and expire for authentication.", help: "" });
165+
expect(ex instanceof ImageKitInvalidRequestError).to.be.true;
166+
expect(ex.message).to.be.equal("Missing signature for upload. The SDK expects token, signature and expire for authentication.");
157167
}
158168
});
159169

@@ -162,7 +172,9 @@ describe("File upload", async function () {
162172
fileName: "test_file_name",
163173
file: "test_file",
164174
token: 'test_token',
165-
signature: 'test_signature'
175+
signature: 'test_signature',
176+
publicKey: 'test_public_key'
177+
// Omit expire
166178
};
167179

168180
const uploadPromise = upload(fileOptions);
@@ -171,34 +183,40 @@ describe("File upload", async function () {
171183
await uploadPromise;
172184
throw new Error('Should have thrown error');
173185
} catch (ex) {
174-
expect(ex).to.be.deep.equal({ message: "Missing expire for upload. The SDK expects token, signature and expire for authentication.", help: "" });
186+
expect(ex instanceof ImageKitInvalidRequestError).to.be.true;
187+
expect(ex.message).to.be.equal("Missing expire for upload. The SDK expects token, signature and expire for authentication.");
175188
}
176189
});
177190

178191
it('Missing public key', async function () {
179192
const fileOptions = {
180193
fileName: "test_file_name",
181-
file: "test_file"
194+
file: "test_file",
195+
token: 'test_token',
196+
signature: 'test_signature',
197+
expire: 123
198+
// Omit publicKey
182199
};
183200

184-
const imagekitWithoutPublicKey = new ImageKit({
185-
urlEndpoint: "https://ik.imagekit.io/your_imagekit_id",
186-
});
187-
201+
const uploadPromise = upload(fileOptions);
202+
expect(server.requests.length).to.be.equal(1);
188203
try {
189-
const uploadPromise = imagekitWithoutPublicKey.upload(fileOptions);
190204
await uploadPromise;
191205
throw new Error('Should have thrown error');
192206
} catch (ex) {
193-
expect(ex).to.be.deep.equal({ message: "Missing public key for upload", help: "" });
207+
expect(ex instanceof ImageKitInvalidRequestError).to.be.true;
208+
expect(ex.message).to.be.equal("Missing public key for upload");
194209
}
195210
});
196211

197212
it('Upload endpoint network error handling', async function () {
198213
const fileOptions = {
199-
...securityParameters,
200214
fileName: "test_file_name",
201-
file: "test_file"
215+
file: "test_file",
216+
token: 'test_token',
217+
signature: 'test_signature',
218+
expire: 123,
219+
publicKey: 'test_public_key'
202220
};
203221

204222
const uploadPromise = upload(fileOptions);
@@ -211,7 +229,8 @@ describe("File upload", async function () {
211229
await uploadPromise;
212230
throw new Error('Should have thrown error');
213231
} catch (ex) {
214-
expect(ex).to.be.deep.equal({ message: "Request to ImageKit upload endpoint failed due to network error", help: "" });
232+
expect(ex instanceof ImageKitUploadNetworkError).to.be.true;
233+
expect(ex.message).to.be.equal("Request to ImageKit upload endpoint failed due to network error");
215234
}
216235
});
217236

@@ -435,7 +454,7 @@ describe("File upload", async function () {
435454
await sleep();
436455

437456
var arg = server.requests[0].requestBody;
438-
457+
// It's a blob now, check size
439458
expect(arg.get('file').size).to.be.eq(buffer.length);
440459
expect(arg.get('fileName')).to.be.equal("test_file_name");
441460
expect(arg.get('token')).to.be.equal("test_token");
@@ -466,13 +485,14 @@ describe("File upload", async function () {
466485
help: "For support kindly contact us at [email protected] .",
467486
message: "Your account cannot be authenticated."
468487
};
469-
errorUploadResponse(500, errRes);
488+
errorUploadResponse(401, errRes);
470489
await sleep();
471490
try {
472491
await uploadPromise;
473492
throw new Error('Should have thrown error');
474493
} catch (ex) {
475-
expect(ex).to.be.deep.equal(errRes);
494+
expect(ex instanceof ImageKitInvalidRequestError).to.be.true;
495+
expect(ex.message).to.be.equal("Your account cannot be authenticated.");
476496
}
477497
});
478498

@@ -499,7 +519,9 @@ describe("File upload", async function () {
499519
await uploadPromise;
500520
throw new Error('Should have thrown error');
501521
} catch (ex) {
502-
expect(ex instanceof SyntaxError).to.be.true;
522+
// The response body is invalid JSON => SyntaxError
523+
expect(ex instanceof ImageKitServerError).to.be.true;
524+
expect(ex.message).to.be.equal("Server error occurred while uploading the file. This is rare and usually temporary.");
503525
}
504526
});
505527

@@ -834,10 +856,10 @@ describe("File upload", async function () {
834856
expect(response).to.be.deep.equal(uploadSuccessResponseObj);
835857
});
836858

837-
it('Upload using promise - error', async function () {
859+
it('Server 5xx error with proper json and message', async function () {
838860
var errRes = {
839861
help: "For support kindly contact us at [email protected] .",
840-
message: "Your account cannot be authenticated."
862+
message: "Something went wrong"
841863
};
842864
const fileOptions = {
843865
...securityParameters,
@@ -861,7 +883,8 @@ describe("File upload", async function () {
861883
await uploadPromise;
862884
throw new Error('Should have thrown error');
863885
} catch (ex) {
864-
expect(ex).to.be.deep.equal(errRes);
886+
expect(ex instanceof ImageKitServerError).to.be.true;
887+
expect(ex.message).to.be.equal("Something went wrong");
865888
}
866889
});
867890

@@ -914,8 +937,8 @@ describe("File upload", async function () {
914937
});
915938

916939
it('$ResponseMetadata assertions using promise', async function () {
917-
var dummyResonseHeaders = {
918-
"Content-Type": "application/json",
940+
var dummyResponseHeaders = {
941+
"content-type": "application/json",
919942
"x-request-id": "sdfsdfsdfdsf"
920943
};
921944
const fileOptions = {
@@ -936,24 +959,25 @@ describe("File upload", async function () {
936959
]
937960
};
938961

939-
var uploadPromise = upload(fileOptions)
962+
var uploadPromise = upload(fileOptions);
940963
expect(server.requests.length).to.be.equal(1);
941964

942965
await sleep();
943966

944967
server.respondWith("POST", "https://upload.imagekit.io/api/v1/files/upload",
945968
[
946969
200,
947-
dummyResonseHeaders,
970+
dummyResponseHeaders,
948971
JSON.stringify(uploadSuccessResponseObj)
949972
]
950973
);
951974
server.respond();
952975
await sleep();
953976

954977
const response = await uploadPromise;
955-
expect(response.$ResponseMetadata.headers).to.be.deep.equal(dummyResonseHeaders);
956-
expect(response.$ResponseMetadata.statusCode).to.be.deep.equal(200);
978+
// Make sure your upload.ts preserves the case of "Content-Type"
979+
expect(response.$ResponseMetadata.headers).to.deep.equal(dummyResponseHeaders);
980+
expect(response.$ResponseMetadata.statusCode).to.equal(200);
957981
});
958982

959983
it('Undefined fields should not be sent', async function () {
@@ -1086,29 +1110,28 @@ describe("File upload", async function () {
10861110
expect(response).to.be.deep.equal(uploadSuccessResponseObj);
10871111
});
10881112

1089-
it("Should return error for an invalid transformation", async function () {
1113+
it("Server 5xx without message", async function () {
10901114
const fileOptions = {
10911115
...securityParameters,
10921116
fileName: "test_file_name",
10931117
file: "test_file",
10941118
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1095-
useUniqueFileName: false,
1096-
transformation: {},
1119+
useUniqueFileName: false
10971120
};
10981121
const uploadPromise = upload(fileOptions);
10991122
expect(server.requests.length).to.be.equal(1);
11001123
await sleep();
11011124
var errRes = {
1102-
help: "",
1103-
message: "Invalid transformation parameter. Please include at least pre, post, or both.",
1125+
help: ""
11041126
};
11051127
errorUploadResponse(500, errRes);
11061128
await sleep();
11071129
try {
11081130
await uploadPromise;
11091131
throw new Error('Should have thrown error');
11101132
} catch (ex) {
1111-
expect(ex).to.be.deep.equal(errRes);
1133+
expect(ex instanceof ImageKitServerError).to.be.true;
1134+
expect(ex.message).to.be.equal("Server error occurred while uploading the file. This is rare and usually temporary.");
11121135
}
11131136
});
11141137

@@ -1134,7 +1157,8 @@ describe("File upload", async function () {
11341157
await uploadPromise;
11351158
throw new Error('Should have thrown error');
11361159
} catch (ex) {
1137-
expect(ex).to.be.deep.equal(errRes);
1160+
expect(ex instanceof ImageKitInvalidRequestError).to.be.true;
1161+
expect(ex.message).to.be.equal("Invalid pre transformation parameter.");
11381162
}
11391163
});
11401164

@@ -1160,7 +1184,8 @@ describe("File upload", async function () {
11601184
await uploadPromise;
11611185
throw new Error('Should have thrown error');
11621186
} catch (ex) {
1163-
expect(ex).to.be.deep.equal(errRes);
1187+
expect(ex instanceof ImageKitInvalidRequestError).to.be.true;
1188+
expect(ex.message).to.be.equal("Invalid post transformation parameter.");
11641189
}
11651190
});
11661191

@@ -1186,7 +1211,8 @@ describe("File upload", async function () {
11861211
await uploadPromise;
11871212
throw new Error('Should have thrown error');
11881213
} catch (ex) {
1189-
expect(ex).to.be.deep.equal(errRes);
1214+
expect(ex instanceof ImageKitInvalidRequestError).to.be.true;
1215+
expect(ex.message).to.be.equal("Invalid post transformation parameter.");
11901216
}
11911217
});
11921218

@@ -1212,7 +1238,8 @@ describe("File upload", async function () {
12121238
await uploadPromise;
12131239
throw new Error('Should have thrown error');
12141240
} catch (ex) {
1215-
expect(ex).to.be.deep.equal(errRes);
1241+
expect(ex instanceof ImageKitInvalidRequestError).to.be.true;
1242+
expect(ex.message).to.be.equal("Invalid post transformation parameter.");
12161243
}
12171244
});
12181245

@@ -1259,7 +1286,7 @@ describe("File upload", async function () {
12591286
expect(progressSpy.calledOnce).to.be.true;
12601287
successUploadResponse();
12611288
await sleep();
1262-
expect(progressSpy.calledTwice).to.be.true; // for 100% progress
1289+
expect(progressSpy.calledTwice).to.be.true; // final progress
12631290
const response = await uploadPromise;
12641291
expect(response).to.be.deep.equal(uploadSuccessResponseObj);
12651292
});
@@ -1280,7 +1307,8 @@ describe("File upload", async function () {
12801307
await uploadPromise;
12811308
throw new Error('Should have thrown error');
12821309
} catch (ex) {
1283-
expect(ex.name).to.be.equal("AbortError");
1310+
expect(ex instanceof ImageKitAbortError).to.be.true;
1311+
expect(ex.reason.name).to.be.equal("AbortError");
12841312
}
12851313
});
12861314

@@ -1300,7 +1328,8 @@ describe("File upload", async function () {
13001328
await uploadPromise;
13011329
throw new Error('Should have thrown error');
13021330
} catch (ex) {
1303-
expect(ex).to.be.deep.equal("abort reason");
1331+
expect(ex instanceof ImageKitAbortError).to.be.true;
1332+
expect(ex.reason).to.be.equal("abort reason");
13041333
}
13051334
});
13061335
});

0 commit comments

Comments
 (0)