forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamUpload.ts
More file actions
75 lines (67 loc) · 2.41 KB
/
Copy pathStreamUpload.ts
File metadata and controls
75 lines (67 loc) · 2.41 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
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/* eslint-disable @typescript-eslint/no-unused-vars */
import "isomorphic-fetch";
import { assert } from "chai";
import { Readable } from "stream";
import { GraphClientError } from "../../../src";
import { StreamUpload } from "../../../src/tasks/FileUploadTask/FileObjectClasses/StreamUpload";
const fileName = "Test_File_Name";
describe("StreamUpload.test", () => {
it("Should return slice with defined size less than complete range size", async () => {
const totalRangesize = 36;
const sliceSize = 20;
const buf = Buffer.alloc(totalRangesize, "a");
const readStream = new Readable({
read() {
this.push(buf);
this.push(null);
},
});
const upload = new StreamUpload(readStream, fileName, totalRangesize);
const slice = await upload.sliceFile({ minValue: 0, maxValue: sliceSize - 1 });
assert.isDefined(slice);
assert.equal(sliceSize, (slice as Buffer).length);
assert.equal(readStream.readableLength, 16);
});
});
it("Should return slice ", async () => {
const totalRangesize = 36;
const buf = Buffer.alloc(totalRangesize, "a");
const readStream = new Readable({
read() {
this.push(buf);
this.push(null);
},
});
const upload = new StreamUpload(readStream, fileName, totalRangesize);
const slice = await upload.sliceFile({ minValue: 0, maxValue: totalRangesize - 1 });
assert.isDefined(slice);
assert.equal(totalRangesize, (slice as Buffer).length);
assert.equal(readStream.readableLength, 0);
});
it("Should throw error if stream ends before complete range size is read", async () => {
const totalsize = 6;
const sliceSize = 20;
const buf = Buffer.alloc(totalsize, "a");
const readStream = new Readable({
read() {
this.push(buf);
this.push(null);
},
});
try {
const upload = new StreamUpload(readStream, fileName, totalsize);
const slice = await upload.sliceFile({ minValue: 0, maxValue: sliceSize - 1 });
if (slice) {
throw Error("Test failed. Expected error now thrown");
}
} catch (err) {
assert.instanceOf(err, GraphClientError);
assert.equal(err.message, "Stream ended before reading required range size");
}
});