forked from http-party/http-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
304.test.js
236 lines (199 loc) · 5.75 KB
/
304.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
'use strict';
const test = require('tap').test;
const ecstatic = require('../lib/core');
const http = require('http');
const request = require('request');
const path = require('path');
const portfinder = require('portfinder');
const root = `${__dirname}/public`;
const baseDir = 'base';
test('304_not_modified_strong', (t) => {
portfinder.getPort((err, port) => {
const file = 'a.txt';
const server = http.createServer(
ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
weakEtags: false,
weakCompare: false,
})
);
server.listen(port, () => {
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
request.get({
uri,
followRedirect: false,
}, (err, res) => {
if (err) {
t.fail(err);
}
t.equal(res.statusCode, 200, 'first request should be a 200');
request.get({
uri,
followRedirect: false,
headers: { 'if-modified-since': res.headers['last-modified'] },
}, (err2, res2) => {
if (err2) {
t.fail(err2);
}
t.equal(res2.statusCode, 304, 'second request should be a 304');
t.equal(res2.headers.etag.indexOf('"'), 0, 'should return a strong etag');
server.close();
t.end();
});
});
});
});
});
test('304_not_modified_weak', (t) => {
portfinder.getPort((err, port) => {
const file = 'b.txt';
const server = http.createServer(
ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
weakCompare: false,
})
);
server.listen(port, () => {
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
const now = (new Date()).toString();
request.get({
uri,
followRedirect: false,
}, (err, res) => {
if (err) {
t.fail(err);
}
t.equal(res.statusCode, 200, 'first request should be a 200');
request.get({
uri,
followRedirect: false,
headers: { 'if-modified-since': now },
}, (err2, res2) => {
if (err2) t.fail(err2);
t.equal(res2.statusCode, 304, 'second request should be a 304');
t.equal(res2.headers.etag.indexOf('W/'), 0, 'should return a weak etag');
server.close();
t.end();
});
});
});
});
});
test('304_not_modified_strong_compare', (t) => {
portfinder.getPort((err, port) => {
const file = 'b.txt';
const server = http.createServer(
ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
weakEtags: false,
weakCompare: false,
})
);
server.listen(port, () => {
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
const now = (new Date()).toString();
let etag = null;
request.get({
uri,
followRedirect: false,
}, (err, res) => {
if (err) {
t.fail(err);
}
t.equal(res.statusCode, 200, 'first request should be a 200');
etag = res.headers.etag;
request.get({
uri,
followRedirect: false,
headers: { 'if-modified-since': now, 'if-none-match': etag },
}, (err2, res2) => {
if (err2) {
t.fail(err2);
}
t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304');
request.get({
uri,
followRedirect: false,
headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` },
}, (err3, res3) => {
if (err3) {
t.fail(err3);
}
// Note that if both if-modified-since and if-none-match are
// provided, the server MUST NOT return a response status of 304
// unless doing so is consistent with all of the conditional
// header fields in the request
// https://www.ietf.org/rfc/rfc2616.txt
t.equal(res3.statusCode, 200, 'third request with a weak etag should be 200');
server.close();
t.end();
});
});
});
});
});
});
test('304_not_modified_weak_compare', (t) => {
portfinder.getPort((err, port) => {
const file = 'c.js';
const server = http.createServer(
ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
weakEtags: false,
})
);
server.listen(port, () => {
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
const now = (new Date()).toString();
let etag = null;
request.get({
uri,
followRedirect: false,
}, (err, res) => {
if (err) {
t.fail(err);
}
t.equal(res.statusCode, 200, 'first request should be a 200');
etag = res.headers.etag;
request.get({
uri,
followRedirect: false,
headers: { 'if-modified-since': now, 'if-none-match': etag },
}, (err2, res2) => {
if (err2) {
t.fail(err2);
}
t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304');
request.get({
uri,
followRedirect: false,
headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` },
}, (err3, res3) => {
if (err3) {
t.fail(err3);
}
t.equal(res3.statusCode, 304, 'third request with a weak etag should be 304');
server.close();
t.end();
});
});
});
});
});
});