forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_test.ts
189 lines (171 loc) · 5.04 KB
/
range_test.ts
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
182
183
184
185
186
187
188
189
// Copyright 2018-2021 the oak authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "./test_deps.ts";
import { concat, copyBytes } from "./deps.ts";
import { calculate } from "./etag.ts";
import { httpErrors } from "./httpError.ts";
import { ifRange, MultiPartStream, parseRange } from "./range.ts";
const { test } = Deno;
class MockFile implements Deno.Seeker, Deno.Reader, Deno.Closer {
#buf: Uint8Array;
#closed = false;
#offset = 0;
get closed() {
return this.#closed;
}
constructor(buf: Uint8Array) {
this.#buf = buf;
}
close() {
this.#closed = true;
}
read(p: Uint8Array): Promise<number | null> {
if (this.#offset >= this.#buf.length) {
return Promise.resolve(null);
}
const nread = Math.min(p.length, 16_384, this.#buf.length - this.#offset);
if (nread === 0) {
return Promise.resolve(0);
}
copyBytes(this.#buf.subarray(this.#offset, this.#offset + nread), p);
this.#offset += nread;
return Promise.resolve(nread);
}
seek(offset: number, whence: Deno.SeekMode): Promise<number> {
assert(whence === Deno.SeekMode.Start);
if (offset >= this.#buf.length) {
return Promise.reject(new RangeError("seeked pass end"));
}
this.#offset = offset;
return Promise.resolve(this.#offset);
}
}
test({
name: "ifRange() - timestamp - true",
fn() {
assert(ifRange("Wed, 21 Oct 2015 07:28:00 GMT", 1445412480000, {
size: 1024,
mtime: new Date(1445412480000),
}));
},
});
test({
name: "ifRange() - timestamp - false",
fn() {
assert(
!ifRange("Wed, 21 Oct 2014 07:28:00 GMT", 1445412480000, {
size: 1024,
mtime: new Date(1445412480000),
}),
);
},
});
test({
name: "ifRange() - etag - true",
fn() {
const content = new TextEncoder().encode("hello deno");
assert(ifRange(`"a-l+ghcNTLpmZ9DVs/87qbgBvpV0M"`, 1445412480000, content));
},
});
test({
name: "ifRange() - etag - false",
fn() {
const content = new TextEncoder().encode("hello deno");
assert(!ifRange(`"blah"`, 1445412480000, content));
},
});
test({
name: "ifRange() - weak etag - true",
fn() {
const etag = calculate({
size: 1024,
mtime: new Date(1445412480000),
});
assert(ifRange(etag, 1445412480000, {
size: 1024,
mtime: new Date(1445412480000),
}));
},
});
test({
name: "parseRange",
fn() {
assertEquals(parseRange(`bytes=0-199`, 400), [{ start: 0, end: 199 }]);
assertEquals(parseRange(`bytes=49-249`, 400), [{ start: 49, end: 249 }]);
assertEquals(parseRange(`bytes=0-`, 400), [{ start: 0, end: 399 }]);
assertEquals(parseRange(`bytes=-200`, 400), [{ start: 199, end: 399 }]);
assertEquals(parseRange(`bytes=0-199, 200-`, 400), [
{ start: 0, end: 199 },
{ start: 200, end: 399 },
]);
},
});
test({
name: "parseRange - errors",
fn() {
assertThrows(() => {
parseRange(`special=0-199`, 400);
}, httpErrors["RequestedRangeNotSatisfiable"]);
assertThrows(() => {
parseRange(`special=0-499`, 400);
}, httpErrors["RequestedRangeNotSatisfiable"]);
assertThrows(() => {
parseRange(`special=200-0`, 400);
}, httpErrors["RequestedRangeNotSatisfiable"]);
assertThrows(() => {
parseRange(`special=400-`, 400);
}, httpErrors["RequestedRangeNotSatisfiable"]);
},
});
test({
name: "MultiPartStream - Uint8Array",
async fn() {
const encoder = new TextEncoder();
const fixture = encoder.encode("hello deno");
const stream = new MultiPartStream(
fixture,
"txt",
[{ start: 0, end: 5 }, { start: 6, end: 9 }],
10,
"test_boundary",
);
const contentLength = stream.contentLength();
const parts = [];
for await (const part of stream) {
parts.push(part);
}
const decoder = new TextDecoder();
const actual = decoder.decode(concat(...parts));
assertEquals(
actual,
"\n--test_boundary\nContent-Type: text/plain; charset=utf-8\nContent-Range: 0-5/10\n\nhello \n--test_boundary\nContent-Type: text/plain; charset=utf-8\nContent-Range: 6-9/10\n\ndeno\n--test_boundary--\n",
);
assertEquals(actual.length, contentLength);
},
});
test({
name: "MultiPartStream - File",
async fn() {
const encoder = new TextEncoder();
const fixture = new MockFile(encoder.encode("hello deno"));
const stream = new MultiPartStream(
fixture,
"txt",
[{ start: 0, end: 5 }, { start: 6, end: 9 }],
10,
"test_boundary",
);
const contentLength = stream.contentLength();
const parts = [];
for await (const part of stream) {
parts.push(part);
}
const decoder = new TextDecoder();
const actual = decoder.decode(concat(...parts));
assertEquals(
actual,
"\n--test_boundary\nContent-Type: text/plain; charset=utf-8\nContent-Range: 0-5/10\n\nhello \n--test_boundary\nContent-Type: text/plain; charset=utf-8\nContent-Range: 6-9/10\n\ndeno\n--test_boundary--\n",
);
assertEquals(actual.length, contentLength);
assert(fixture.closed === true);
},
});