forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargeFileUploadTask.ts
More file actions
181 lines (170 loc) · 5.54 KB
/
Copy pathLargeFileUploadTask.ts
File metadata and controls
181 lines (170 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { assert } from "chai";
import { LargeFileUploadTask } from "../../src/tasks/LargeFileUploadTask";
import { getClient } from "../test-helper";
describe("LargeFileUploadTask.ts", () => {
/* tslint:disable: no-string-literal */
describe("Parsing Range", () => {
const name = "sample_image.jpg";
const arrayBuffer = new ArrayBuffer(80000);
const size = 100000;
const fileObj = {
content: arrayBuffer,
name,
size,
};
const uploadSession = {
url: "test url",
expiry: new Date(),
};
const options = {};
const uploadTask = new LargeFileUploadTask(getClient(), fileObj, uploadSession, options);
it("Should return default range for given undefined range", (done) => {
const range = uploadTask["parseRange"]([]);
assert.equal(range.minValue, -1);
assert.equal(range.maxValue, -1);
done();
});
it("Should return default range for given empty range", (done) => {
const range = uploadTask["parseRange"]([""]);
assert.equal(range.minValue, -1);
assert.equal(range.maxValue, -1);
done();
});
it("Should return valid range for given range with from and to values", (done) => {
const range = uploadTask["parseRange"](["100-200"]);
assert.equal(range.minValue, 100);
assert.equal(range.maxValue, 200);
done();
});
it("Should return valid range for given range without to value", (done) => {
const range = uploadTask["parseRange"](["0-"]);
assert.equal(range.minValue, 0);
assert.equal(range.maxValue, 99999);
done();
});
});
describe("Update Task Status", () => {
const name = "sample_image.jpg";
const arrayBuffer = new ArrayBuffer(80000);
const size = 100000;
const fileObj = {
content: arrayBuffer,
name,
size,
};
const uploadSession = {
url: "test url",
expiry: new Date(),
};
const options = {};
const uploadTask = new LargeFileUploadTask(getClient(), fileObj, uploadSession, options);
it("Should update status with expiration date and next expected ranges as given", (done) => {
const statusResponse = {
expirationDateTime: "2018-08-06T09:05:45.195Z",
nextExpectedRanges: ["100-2000"],
};
uploadTask["updateTaskStatus"](statusResponse);
assert.equal(uploadTask["nextRange"].minValue, 100);
assert.equal(uploadTask["nextRange"].maxValue, 2000);
done();
});
it("Should update status with given expiration date and (fileSize - 1) for next expected range maxValue", (done) => {
const statusResponse = {
expirationDateTime: "2018-08-06T09:05:45.195Z",
nextExpectedRanges: ["100-"],
};
uploadTask["updateTaskStatus"](statusResponse);
assert.equal(uploadTask["nextRange"].minValue, 100);
assert.equal(uploadTask["nextRange"].maxValue, 99999);
done();
});
});
describe("GetNextRange", () => {
const name = "sample_image.jpg";
const arrayBuffer = new ArrayBuffer(80000);
const size = 328680;
const fileObj = {
content: arrayBuffer,
name,
size,
};
const uploadSession = {
url: "test url",
expiry: new Date(),
};
const options = {
rangeSize: 327680,
};
const uploadTask = new LargeFileUploadTask(getClient(), fileObj, uploadSession, options);
it("Should return proper next range well within the file size", (done) => {
const nextRange = uploadTask.getNextRange();
assert.equal(nextRange.minValue, 0);
assert.equal(nextRange.maxValue, 327679);
done();
});
it("Should return next range maxValue equal to the file size", (done) => {
const statusResponse = {
expirationDateTime: "2018-08-06T09:05:45.195Z",
nextExpectedRanges: ["327680-"],
};
uploadTask["updateTaskStatus"](statusResponse);
const nextRange = uploadTask.getNextRange();
assert.equal(nextRange.minValue, 327680);
assert.equal(nextRange.maxValue, 328679);
done();
});
it("Should return next range as default(empty) range, this is for the upload task completed", (done) => {
const statusResponse = {
expirationDateTime: "2018-08-06T09:05:45.195Z",
nextExpectedRanges: [],
};
uploadTask["updateTaskStatus"](statusResponse);
const nextRange = uploadTask.getNextRange();
assert.equal(nextRange.minValue, -1);
assert.equal(nextRange.maxValue, -1);
done();
});
});
describe("Upload File", () => {
const name = "sample_image.jpg";
const arrayBuffer = new ArrayBuffer(80000);
const size = 328680;
const fileObj = {
content: arrayBuffer,
name,
size,
};
const uploadSession = {
url: "test url",
expiry: new Date(),
};
const options = {
rangeSize: 327680,
};
const uploadTask = new LargeFileUploadTask(getClient(), fileObj, uploadSession, options);
it("Should return an exception while trying to upload the file upload completed task", (done) => {
const statusResponse = {
expirationDateTime: "2018-08-06T09:05:45.195Z",
nextExpectedRanges: [],
};
uploadTask["updateTaskStatus"](statusResponse);
uploadTask
.upload()
.then((res) => {
throw new Error("Upload is working for upload completed task");
})
.catch((err) => {
assert.equal(err.name, "Invalid Session");
assert.equal(err.message, "Task with which you are trying to upload is already completed, Please check for your uploaded file");
done();
});
});
});
/* tslint:enable: no-string-literal */
});