Skip to content

Commit 563771d

Browse files
jbergstroemShigeki Ohtsu
authored and
Shigeki Ohtsu
committed
test: split parts out of host-headers test into its own test
this makes the separation between http and https testing cleaner PR-URL: #1049 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Shigeki Ohtsu <[email protected]>
1 parent 671fbd5 commit 563771d

File tree

2 files changed

+110
-90
lines changed

2 files changed

+110
-90
lines changed
-90
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
var http = require('http'),
2-
fs = require('fs'),
32
common = require('../common'),
43
assert = require('assert'),
5-
options = {
6-
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
7-
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
8-
},
94
httpServer = http.createServer(reqHandler);
105

11-
if(common.hasCrypto) {
12-
var https = require('https'),
13-
httpsServer = https.createServer(options, reqHandler);
14-
} else {
15-
console.log('1..0 # Skipped: missing crypto');
16-
}
17-
186
function reqHandler(req, res) {
197
console.log('Got request: ' + req.headers.host + ' ' + req.url);
208
if (req.url === '/setHostFalse5') {
@@ -46,9 +34,6 @@ function testHttp() {
4634
console.log('back from http request. counter = ' + counter);
4735
if (counter === 0) {
4836
httpServer.close();
49-
if(common.hasCrypto) {
50-
testHttps();
51-
}
5237
}
5338
res.resume();
5439
}
@@ -104,78 +89,3 @@ function testHttp() {
10489
}, cb).on('error', thrower).end();
10590
});
10691
}
107-
108-
function testHttps() {
109-
110-
console.log('testing https on port ' + common.PORT);
111-
112-
var counter = 0;
113-
114-
function cb(res) {
115-
counter--;
116-
console.log('back from https request. counter = ' + counter);
117-
if (counter === 0) {
118-
httpsServer.close();
119-
console.log('ok');
120-
}
121-
res.resume();
122-
}
123-
124-
httpsServer.listen(common.PORT, function(er) {
125-
if (er) throw er;
126-
127-
https.get({
128-
method: 'GET',
129-
path: '/' + (counter++),
130-
host: 'localhost',
131-
//agent: false,
132-
port: common.PORT,
133-
rejectUnauthorized: false
134-
}, cb).on('error', thrower);
135-
136-
https.request({
137-
method: 'GET',
138-
path: '/' + (counter++),
139-
host: 'localhost',
140-
//agent: false,
141-
port: common.PORT,
142-
rejectUnauthorized: false
143-
}, cb).on('error', thrower).end();
144-
145-
https.request({
146-
method: 'POST',
147-
path: '/' + (counter++),
148-
host: 'localhost',
149-
//agent: false,
150-
port: common.PORT,
151-
rejectUnauthorized: false
152-
}, cb).on('error', thrower).end();
153-
154-
https.request({
155-
method: 'PUT',
156-
path: '/' + (counter++),
157-
host: 'localhost',
158-
//agent: false,
159-
port: common.PORT,
160-
rejectUnauthorized: false
161-
}, cb).on('error', thrower).end();
162-
163-
https.request({
164-
method: 'DELETE',
165-
path: '/' + (counter++),
166-
host: 'localhost',
167-
//agent: false,
168-
port: common.PORT,
169-
rejectUnauthorized: false
170-
}, cb).on('error', thrower).end();
171-
172-
https.get({
173-
method: 'GET',
174-
path: '/setHostFalse' + (counter++),
175-
host: 'localhost',
176-
setHost: false,
177-
port: common.PORT,
178-
rejectUnauthorized: false
179-
}, cb).on('error', thrower).end();
180-
});
181-
}
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
if (!common.hasCrypto) {
5+
console.log('1..0 # Skipped: missing crypto');
6+
process.exit();
7+
}
8+
var https = require('https');
9+
10+
var fs = require('fs'),
11+
options = {
12+
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
13+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
14+
},
15+
httpsServer = https.createServer(options, reqHandler);
16+
17+
function reqHandler(req, res) {
18+
console.log('Got request: ' + req.headers.host + ' ' + req.url);
19+
if (req.url === '/setHostFalse5') {
20+
assert.equal(req.headers.host, undefined);
21+
} else {
22+
assert.equal(req.headers.host, 'localhost:' + common.PORT,
23+
'Wrong host header for req[' + req.url + ']: ' +
24+
req.headers.host);
25+
}
26+
res.writeHead(200, {});
27+
//process.nextTick(function() { res.end('ok'); });
28+
res.end('ok');
29+
}
30+
31+
function thrower(er) {
32+
throw er;
33+
}
34+
35+
testHttps();
36+
37+
function testHttps() {
38+
39+
console.log('testing https on port ' + common.PORT);
40+
41+
var counter = 0;
42+
43+
function cb(res) {
44+
counter--;
45+
console.log('back from https request. counter = ' + counter);
46+
if (counter === 0) {
47+
httpsServer.close();
48+
console.log('ok');
49+
}
50+
res.resume();
51+
}
52+
53+
httpsServer.listen(common.PORT, function(er) {
54+
if (er) throw er;
55+
56+
https.get({
57+
method: 'GET',
58+
path: '/' + (counter++),
59+
host: 'localhost',
60+
//agent: false,
61+
port: common.PORT,
62+
rejectUnauthorized: false
63+
}, cb).on('error', thrower);
64+
65+
https.request({
66+
method: 'GET',
67+
path: '/' + (counter++),
68+
host: 'localhost',
69+
//agent: false,
70+
port: common.PORT,
71+
rejectUnauthorized: false
72+
}, cb).on('error', thrower).end();
73+
74+
https.request({
75+
method: 'POST',
76+
path: '/' + (counter++),
77+
host: 'localhost',
78+
//agent: false,
79+
port: common.PORT,
80+
rejectUnauthorized: false
81+
}, cb).on('error', thrower).end();
82+
83+
https.request({
84+
method: 'PUT',
85+
path: '/' + (counter++),
86+
host: 'localhost',
87+
//agent: false,
88+
port: common.PORT,
89+
rejectUnauthorized: false
90+
}, cb).on('error', thrower).end();
91+
92+
https.request({
93+
method: 'DELETE',
94+
path: '/' + (counter++),
95+
host: 'localhost',
96+
//agent: false,
97+
port: common.PORT,
98+
rejectUnauthorized: false
99+
}, cb).on('error', thrower).end();
100+
101+
https.get({
102+
method: 'GET',
103+
path: '/setHostFalse' + (counter++),
104+
host: 'localhost',
105+
setHost: false,
106+
port: common.PORT,
107+
rejectUnauthorized: false
108+
}, cb).on('error', thrower).end();
109+
});
110+
}

0 commit comments

Comments
 (0)