-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathrange.test.js
131 lines (118 loc) · 3.81 KB
/
range.test.js
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
'use strict';
const test = require('tap').test;
const ecstatic = require('../lib/core');
const http = require('http');
const request = require('request');
const eol = require('eol');
test('range', (t) => {
t.plan(4);
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
t.on('end', () => { server.close(); });
server.listen(0, () => {
const port = server.address().port;
const opts = {
uri: `http://localhost:${port}/e.html`,
headers: { range: '3-5' },
};
request.get(opts, (err, res, body) => {
t.error(err);
t.equal(res.statusCode, 206, 'partial content status code');
t.equal(body, 'e!!');
t.equal(parseInt(res.headers['content-length'], 10), body.length);
});
});
});
test('range past the end', (t) => {
t.plan(4);
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
t.on('end', () => { server.close(); });
server.listen(0, () => {
const port = server.address().port;
const opts = {
uri: `http://localhost:${port}/e.html`,
headers: { range: '3-500' },
};
request.get(opts, (err, res, body) => {
t.error(err);
t.equal(res.statusCode, 206, 'partial content status code');
t.equal(eol.lf(body), 'e!!</b>\n');
t.equal(parseInt(res.headers['content-length'], 10), body.length);
});
});
});
test('NaN range', (t) => {
t.plan(3);
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
t.on('end', () => { server.close(); });
server.listen(0, () => {
const port = server.address().port;
const opts = {
uri: `http://localhost:${port}/e.html`,
headers: { range: 'abc-def' },
};
request.get(opts, (err, res, body) => {
t.error(err);
t.equal(res.statusCode, 416, 'range error status code');
t.equal(body, 'Requested range not satisfiable');
});
});
});
test('flipped range', (t) => {
t.plan(3);
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
t.on('end', () => { server.close(); });
server.listen(0, () => {
const port = server.address().port;
const opts = {
uri: `http://localhost:${port}/e.html`,
headers: { range: '333-222' },
};
request.get(opts, (err, res, body) => {
t.error(err);
t.equal(res.statusCode, 416, 'range error status code');
t.equal(body, 'Requested range not satisfiable');
});
});
});
test('partial range', (t) => {
// 1 test is platform depedent "res.headers['content-range']"
t.plan(5);
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
t.on('end', () => { server.close(); });
server.listen(0, () => {
const port = server.address().port;
const opts = {
uri: `http://localhost:${port}/e.html`,
headers: { range: '3-' },
};
request.get(opts, (err, res, body) => {
t.error(err);
t.equal(res.statusCode, 206, 'partial content status code');
t.equal(eol.lf(body), 'e!!</b>\n');
t.equal(parseInt(res.headers['content-length'], 10), body.length);
if (eol.lf(body) != body) { // on Windows, depending on Git settings
t.equal(res.headers['content-range'], 'bytes 3-11/12');
} else {
t.equal(res.headers['content-range'], 'bytes 3-10/11');
}
});
});
});
test('include last-modified, etag and cache-control headers', (t) => {
t.plan(4);
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
t.on('end', () => { server.close(); });
server.listen(0, () => {
const port = server.address().port;
const opts = {
uri: `http://localhost:${port}/e.html`,
headers: { range: '3-5' },
};
request.get(opts, (err, res) => {
t.error(err);
t.ok(res.headers['cache-control']);
t.ok(res.headers['last-modified']);
t.ok(res.headers.etag);
});
});
});