Skip to content

Commit f38b602

Browse files
committed
Test: avoiding forbidden Host header in js fetch tests.
1 parent d81563f commit f38b602

File tree

3 files changed

+19
-49
lines changed

3 files changed

+19
-49
lines changed

js_fetch.t

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ http {
8989
location /property {
9090
js_content test.property;
9191
}
92-
}
93-
94-
server {
95-
listen 127.0.0.1:8080;
96-
server_name aaa;
9792
9893
location /loc {
9994
js_content test.loc;
@@ -102,18 +97,9 @@ http {
10297
location /json { }
10398
}
10499
105-
server {
106-
listen 127.0.0.1:8080;
107-
server_name bbb;
108-
109-
location /loc {
110-
js_content test.loc;
111-
}
112-
}
113-
114100
server {
115101
listen 127.0.0.1:8081;
116-
server_name ccc;
102+
server_name localhost;
117103
118104
location /loc {
119105
js_content test.loc;
@@ -150,14 +136,14 @@ $t->write_file('test.js', <<EOF);
150136
return JSON.stringify(retval);
151137
}
152138
153-
ngx.fetch(`http://127.0.0.1:$p0/\${loc}`, {headers: {Host: 'aaa'}})
139+
ngx.fetch(`http://127.0.0.1:$p0/\${loc}`)
154140
.then(reply => reply[getter]())
155141
.then(data => r.return(200, query(data)))
156142
.catch(e => r.return(501, e.message))
157143
}
158144
159145
function property(r) {
160-
var opts = {headers:{Host: 'aaa'}};
146+
var opts = {headers:{}};
161147
162148
if (r.args.code) {
163149
opts.headers.code = r.args.code;
@@ -218,8 +204,8 @@ $t->write_file('test.js', <<EOF);
218204
function chain(r) {
219205
var results = [];
220206
var reqs = [
221-
['http://127.0.0.1:$p0/loc', {headers: {Host:'aaa'}}],
222-
['http://127.0.0.1:$p0/loc', {headers: {Host:'bbb'}}],
207+
['http://127.0.0.1:$p0/loc'],
208+
['http://127.0.0.1:$p1/loc'],
223209
];
224210
225211
function next(reply) {
@@ -317,16 +303,16 @@ $t->write_file('test.js', <<EOF);
317303
var tests = [
318304
[
319305
'http://127.0.0.1:$p0/loc',
320-
{ headers: {Code: 201, Host: 'aaa'}},
306+
{ headers: {Code: 201}},
321307
],
322308
[
323309
'http://127.0.0.1:$p0/loc',
324-
{ method:'POST', headers: {Code: 401, Host: 'bbb'}, body: 'OK'},
310+
{ method:'POST', headers: {Code: 401}, body: 'OK'},
325311
],
326312
[
327313
'http://127.0.0.1:$p1/loc',
328314
{ method:'PATCH',
329-
headers: {foo:undefined, bar:'xxx', Host: 'ccc'}},
315+
headers: {bar:'xxx'}},
330316
],
331317
];
332318
@@ -366,10 +352,9 @@ $t->write_file('test.js', <<EOF);
366352
function loc(r) {
367353
var v = r.variables;
368354
var body = str(r.requestText);
369-
var foo = str(r.headersIn.foo);
370355
var bar = str(r.headersIn.bar);
371356
var c = r.headersIn.code ? Number(r.headersIn.code) : 200;
372-
r.return(c, `\${v.host}:\${v.request_method}:\${foo}:\${bar}:\${body}`);
357+
r.return(c, `\${v.request_method}:\${bar}:\${body}`);
373358
}
374359
375360
export default {njs: test_njs, body, broken, broken_response, body_special,
@@ -383,9 +368,9 @@ $t->waitforsocket('127.0.0.1:' . port(8082));
383368

384369
###############################################################################
385370

386-
like(http_get('/body?getter=arrayBuffer&loc=loc'), qr/200 OK.*"aaa:GET:::"$/s,
371+
like(http_get('/body?getter=arrayBuffer&loc=loc'), qr/200 OK.*"GET::"$/s,
387372
'fetch body arrayBuffer');
388-
like(http_get('/body?getter=text&loc=loc'), qr/200 OK.*"aaa:GET:::"$/s,
373+
like(http_get('/body?getter=text&loc=loc'), qr/200 OK.*"GET::"$/s,
389374
'fetch body text');
390375
like(http_get('/body?getter=json&loc=json&path=b.c'),
391376
qr/200 OK.*"FIELD"$/s, 'fetch body json');
@@ -428,9 +413,9 @@ like(http_get('/header?loc=duplicate_header&h=buz&method=has'),
428413
like(http_get('/header?loc=chunked/big&h=BAR&readBody=1'), qr/200 OK.*xxx$/s,
429414
'fetch chunked header');
430415
is(get_json('/multi'),
431-
'[{"b":"aaa:GET:::","c":201,"u":"http://127.0.0.1:'.$p0.'/loc"},' .
432-
'{"b":"bbb:POST:::OK","c":401,"u":"http://127.0.0.1:'.$p0.'/loc"},' .
433-
'{"b":"ccc:PATCH::xxx:","c":200,"u":"http://127.0.0.1:'.$p1.'/loc"}]',
416+
'[{"b":"GET::","c":201,"u":"http://127.0.0.1:'.$p0.'/loc"},' .
417+
'{"b":"PATCH:xxx:","c":200,"u":"http://127.0.0.1:'.$p1.'/loc"},' .
418+
'{"b":"POST::OK","c":401,"u":"http://127.0.0.1:'.$p0.'/loc"}]',
434419
'fetch multi');
435420
like(http_get('/multi?throw=1'), qr/500/s, 'fetch destructor');
436421
is(get_json('/broken'),

stream_js_fetch.t

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ http {
4545
location /njs {
4646
js_content test.njs;
4747
}
48-
}
49-
50-
server {
51-
listen 127.0.0.1:8080;
52-
server_name aaa;
5348
5449
location /validate {
5550
js_content test.validate;
@@ -118,8 +113,7 @@ $t->write_file('test.js', <<EOF);
118113
s.off('upstream');
119114
120115
let reply = await ngx.fetch('http://127.0.0.1:$p/validate',
121-
{body: collect.slice(2,4),
122-
headers: {Host:'aaa'}});
116+
{body: collect.slice(2,4)});
123117
124118
(reply.status == 200) ? s.done(): s.deny();
125119
@@ -139,8 +133,7 @@ $t->write_file('test.js', <<EOF);
139133
s.off('upstream');
140134
141135
let reply = await ngx.fetch('http://127.0.0.1:$p/validate',
142-
{body: collect.slice(2,4),
143-
headers: {Host:'aaa'}});
136+
{body: collect.slice(2,4)});
144137
145138
if (reply.status == 200) {
146139
s.send(collect.slice(4), flags);
@@ -153,15 +146,13 @@ $t->write_file('test.js', <<EOF);
153146
}
154147
155148
async function access_ok(s) {
156-
let reply = await ngx.fetch('http://127.0.0.1:$p/success',
157-
{headers: {Host:'aaa'}});
149+
let reply = await ngx.fetch('http://127.0.0.1:$p/success');
158150
159151
(reply.status == 200) ? s.allow(): s.deny();
160152
}
161153
162154
async function access_nok(s) {
163-
let reply = await ngx.fetch('http://127.0.0.1:$p/fail',
164-
{headers: {Host:'aaa'}});
155+
let reply = await ngx.fetch('http://127.0.0.1:$p/fail');
165156
166157
(reply.status == 200) ? s.allow(): s.deny();
167158
}

stream_js_fetch_init.t

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ http {
5757
location /njs {
5858
js_content test.njs;
5959
}
60-
}
61-
62-
server {
63-
listen 127.0.0.1:8080;
64-
server_name aaa;
6560
6661
location /success {
6762
return 200;
@@ -79,8 +74,7 @@ $t->write_file('test.js', <<EOF);
7974
}
8075
8176
async function access_ok(s) {
82-
let reply = await ngx.fetch('http://127.0.0.1:$p/success',
83-
{headers: {Host:'aaa'}});
77+
let reply = await ngx.fetch('http://127.0.0.1:$p/success');
8478
8579
(reply.status == 200) ? s.allow(): s.deny();
8680
}

0 commit comments

Comments
 (0)