Skip to content

Commit 8c48c0a

Browse files
committed
get a good port every time
fixes #723
1 parent b9bca4e commit 8c48c0a

9 files changed

+420
-410
lines changed

test/304.test.js

Lines changed: 165 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -5,163 +5,167 @@ const ecstatic = require('../lib/core');
55
const http = require('http');
66
const request = require('request');
77
const path = require('path');
8+
const portfinder = require('portfinder');
89

910
const root = `${__dirname}/public`;
1011
const baseDir = 'base';
1112

1213
test('304_not_modified_strong', (t) => {
13-
const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
14-
const file = 'a.txt';
15-
16-
const server = http.createServer(
17-
ecstatic({
18-
root,
19-
gzip: true,
20-
baseDir,
21-
autoIndex: true,
22-
showDir: true,
23-
weakEtags: false,
24-
weakCompare: false,
25-
})
26-
);
27-
28-
server.listen(port, () => {
29-
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
30-
31-
request.get({
32-
uri,
33-
followRedirect: false,
34-
}, (err, res) => {
35-
if (err) {
36-
t.fail(err);
37-
}
38-
39-
t.equal(res.statusCode, 200, 'first request should be a 200');
14+
portfinder.getPort((err, port) => {
15+
const file = 'a.txt';
16+
17+
const server = http.createServer(
18+
ecstatic({
19+
root,
20+
gzip: true,
21+
baseDir,
22+
autoIndex: true,
23+
showDir: true,
24+
weakEtags: false,
25+
weakCompare: false,
26+
})
27+
);
28+
29+
server.listen(port, () => {
30+
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
4031

4132
request.get({
4233
uri,
4334
followRedirect: false,
44-
headers: { 'if-modified-since': res.headers['last-modified'] },
45-
}, (err2, res2) => {
46-
if (err2) {
47-
t.fail(err2);
35+
}, (err, res) => {
36+
if (err) {
37+
t.fail(err);
4838
}
4939

50-
t.equal(res2.statusCode, 304, 'second request should be a 304');
51-
t.equal(res2.headers.etag.indexOf('"'), 0, 'should return a strong etag');
52-
server.close();
53-
t.end();
40+
t.equal(res.statusCode, 200, 'first request should be a 200');
41+
42+
request.get({
43+
uri,
44+
followRedirect: false,
45+
headers: { 'if-modified-since': res.headers['last-modified'] },
46+
}, (err2, res2) => {
47+
if (err2) {
48+
t.fail(err2);
49+
}
50+
51+
t.equal(res2.statusCode, 304, 'second request should be a 304');
52+
t.equal(res2.headers.etag.indexOf('"'), 0, 'should return a strong etag');
53+
server.close();
54+
t.end();
55+
});
5456
});
5557
});
5658
});
5759
});
5860

5961
test('304_not_modified_weak', (t) => {
60-
const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
61-
const file = 'b.txt';
62-
63-
const server = http.createServer(
64-
ecstatic({
65-
root,
66-
gzip: true,
67-
baseDir,
68-
autoIndex: true,
69-
showDir: true,
70-
weakCompare: false,
71-
})
72-
);
73-
74-
server.listen(port, () => {
75-
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
76-
const now = (new Date()).toString();
77-
78-
request.get({
79-
uri,
80-
followRedirect: false,
81-
}, (err, res) => {
82-
if (err) {
83-
t.fail(err);
84-
}
85-
86-
t.equal(res.statusCode, 200, 'first request should be a 200');
62+
portfinder.getPort((err, port) => {
63+
const file = 'b.txt';
64+
65+
const server = http.createServer(
66+
ecstatic({
67+
root,
68+
gzip: true,
69+
baseDir,
70+
autoIndex: true,
71+
showDir: true,
72+
weakCompare: false,
73+
})
74+
);
75+
76+
server.listen(port, () => {
77+
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
78+
const now = (new Date()).toString();
8779

8880
request.get({
8981
uri,
9082
followRedirect: false,
91-
headers: { 'if-modified-since': now },
92-
}, (err2, res2) => {
93-
if (err2) t.fail(err2);
94-
95-
t.equal(res2.statusCode, 304, 'second request should be a 304');
96-
t.equal(res2.headers.etag.indexOf('W/'), 0, 'should return a weak etag');
97-
server.close();
98-
t.end();
83+
}, (err, res) => {
84+
if (err) {
85+
t.fail(err);
86+
}
87+
88+
t.equal(res.statusCode, 200, 'first request should be a 200');
89+
90+
request.get({
91+
uri,
92+
followRedirect: false,
93+
headers: { 'if-modified-since': now },
94+
}, (err2, res2) => {
95+
if (err2) t.fail(err2);
96+
97+
t.equal(res2.statusCode, 304, 'second request should be a 304');
98+
t.equal(res2.headers.etag.indexOf('W/'), 0, 'should return a weak etag');
99+
server.close();
100+
t.end();
101+
});
99102
});
100103
});
101104
});
102105
});
103106

104107
test('304_not_modified_strong_compare', (t) => {
105-
const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
106-
const file = 'b.txt';
107-
108-
const server = http.createServer(
109-
ecstatic({
110-
root,
111-
gzip: true,
112-
baseDir,
113-
autoIndex: true,
114-
showDir: true,
115-
weakEtags: false,
116-
weakCompare: false,
117-
})
118-
);
119-
120-
server.listen(port, () => {
121-
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
122-
const now = (new Date()).toString();
123-
let etag = null;
124-
125-
request.get({
126-
uri,
127-
followRedirect: false,
128-
}, (err, res) => {
129-
if (err) {
130-
t.fail(err);
131-
}
132-
133-
t.equal(res.statusCode, 200, 'first request should be a 200');
134-
135-
etag = res.headers.etag;
108+
portfinder.getPort((err, port) => {
109+
const file = 'b.txt';
110+
111+
const server = http.createServer(
112+
ecstatic({
113+
root,
114+
gzip: true,
115+
baseDir,
116+
autoIndex: true,
117+
showDir: true,
118+
weakEtags: false,
119+
weakCompare: false,
120+
})
121+
);
122+
123+
server.listen(port, () => {
124+
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
125+
const now = (new Date()).toString();
126+
let etag = null;
136127

137128
request.get({
138129
uri,
139130
followRedirect: false,
140-
headers: { 'if-modified-since': now, 'if-none-match': etag },
141-
}, (err2, res2) => {
142-
if (err2) {
143-
t.fail(err2);
131+
}, (err, res) => {
132+
if (err) {
133+
t.fail(err);
144134
}
145135

146-
t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304');
136+
t.equal(res.statusCode, 200, 'first request should be a 200');
137+
138+
etag = res.headers.etag;
147139

148140
request.get({
149141
uri,
150142
followRedirect: false,
151-
headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` },
152-
}, (err3, res3) => {
153-
if (err3) {
154-
t.fail(err3);
143+
headers: { 'if-modified-since': now, 'if-none-match': etag },
144+
}, (err2, res2) => {
145+
if (err2) {
146+
t.fail(err2);
155147
}
156148

157-
// Note that if both if-modified-since and if-none-match are
158-
// provided, the server MUST NOT return a response status of 304
159-
// unless doing so is consistent with all of the conditional
160-
// header fields in the request
161-
// https://www.ietf.org/rfc/rfc2616.txt
162-
t.equal(res3.statusCode, 200, 'third request with a weak etag should be 200');
163-
server.close();
164-
t.end();
149+
t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304');
150+
151+
request.get({
152+
uri,
153+
followRedirect: false,
154+
headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` },
155+
}, (err3, res3) => {
156+
if (err3) {
157+
t.fail(err3);
158+
}
159+
160+
// Note that if both if-modified-since and if-none-match are
161+
// provided, the server MUST NOT return a response status of 304
162+
// unless doing so is consistent with all of the conditional
163+
// header fields in the request
164+
// https://www.ietf.org/rfc/rfc2616.txt
165+
t.equal(res3.statusCode, 200, 'third request with a weak etag should be 200');
166+
server.close();
167+
t.end();
168+
});
165169
});
166170
});
167171
});
@@ -170,60 +174,61 @@ test('304_not_modified_strong_compare', (t) => {
170174

171175

172176
test('304_not_modified_weak_compare', (t) => {
173-
const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
174-
const file = 'c.js';
175-
176-
const server = http.createServer(
177-
ecstatic({
178-
root,
179-
gzip: true,
180-
baseDir,
181-
autoIndex: true,
182-
showDir: true,
183-
weakEtags: false,
184-
})
185-
);
186-
187-
server.listen(port, () => {
188-
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
189-
const now = (new Date()).toString();
190-
let etag = null;
191-
192-
request.get({
193-
uri,
194-
followRedirect: false,
195-
}, (err, res) => {
196-
if (err) {
197-
t.fail(err);
198-
}
199-
200-
t.equal(res.statusCode, 200, 'first request should be a 200');
201-
202-
etag = res.headers.etag;
177+
portfinder.getPort((err, port) => {
178+
const file = 'c.js';
179+
180+
const server = http.createServer(
181+
ecstatic({
182+
root,
183+
gzip: true,
184+
baseDir,
185+
autoIndex: true,
186+
showDir: true,
187+
weakEtags: false,
188+
})
189+
);
190+
191+
server.listen(port, () => {
192+
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
193+
const now = (new Date()).toString();
194+
let etag = null;
203195

204196
request.get({
205197
uri,
206198
followRedirect: false,
207-
headers: { 'if-modified-since': now, 'if-none-match': etag },
208-
}, (err2, res2) => {
209-
if (err2) {
210-
t.fail(err2);
199+
}, (err, res) => {
200+
if (err) {
201+
t.fail(err);
211202
}
212203

213-
t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304');
204+
t.equal(res.statusCode, 200, 'first request should be a 200');
205+
206+
etag = res.headers.etag;
214207

215208
request.get({
216209
uri,
217210
followRedirect: false,
218-
headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` },
219-
}, (err3, res3) => {
220-
if (err3) {
221-
t.fail(err3);
211+
headers: { 'if-modified-since': now, 'if-none-match': etag },
212+
}, (err2, res2) => {
213+
if (err2) {
214+
t.fail(err2);
222215
}
223216

224-
t.equal(res3.statusCode, 304, 'third request with a weak etag should be 304');
225-
server.close();
226-
t.end();
217+
t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304');
218+
219+
request.get({
220+
uri,
221+
followRedirect: false,
222+
headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` },
223+
}, (err3, res3) => {
224+
if (err3) {
225+
t.fail(err3);
226+
}
227+
228+
t.equal(res3.statusCode, 304, 'third request with a weak etag should be 304');
229+
server.close();
230+
t.end();
231+
});
227232
});
228233
});
229234
});

0 commit comments

Comments
 (0)