changeset 1812:f98884ad7700

Test: avoiding forbidden Host header in js fetch tests.
author Dmitry Volyntsev <xeioex@nginx.com>
date Fri, 02 Dec 2022 17:46:32 -0800
parents 520fb74cce4c
children 0a489a9abc7a
files js_fetch.t stream_js_fetch.t stream_js_fetch_init.t
diffstat 3 files changed, 19 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/js_fetch.t
+++ b/js_fetch.t
@@ -89,11 +89,6 @@ http {
         location /property {
             js_content test.property;
         }
-    }
-
-    server {
-        listen       127.0.0.1:8080;
-        server_name  aaa;
 
         location /loc {
             js_content test.loc;
@@ -103,17 +98,8 @@ http {
     }
 
     server {
-        listen       127.0.0.1:8080;
-        server_name  bbb;
-
-        location /loc {
-            js_content test.loc;
-        }
-    }
-
-    server {
         listen       127.0.0.1:8081;
-        server_name  ccc;
+        server_name  localhost;
 
         location /loc {
             js_content test.loc;
@@ -150,14 +136,14 @@ my $p2 = port(8082);
             return JSON.stringify(retval);
         }
 
-        ngx.fetch(`http://127.0.0.1:$p0/\${loc}`, {headers: {Host: 'aaa'}})
+        ngx.fetch(`http://127.0.0.1:$p0/\${loc}`)
         .then(reply => reply[getter]())
         .then(data => r.return(200, query(data)))
         .catch(e => r.return(501, e.message))
     }
 
     function property(r) {
-        var opts = {headers:{Host: 'aaa'}};
+        var opts = {headers:{}};
 
         if (r.args.code) {
             opts.headers.code = r.args.code;
@@ -218,8 +204,8 @@ my $p2 = port(8082);
     function chain(r) {
         var results = [];
         var reqs = [
-             ['http://127.0.0.1:$p0/loc', {headers: {Host:'aaa'}}],
-             ['http://127.0.0.1:$p0/loc', {headers: {Host:'bbb'}}],
+             ['http://127.0.0.1:$p0/loc'],
+             ['http://127.0.0.1:$p1/loc'],
            ];
 
            function next(reply) {
@@ -317,16 +303,16 @@ my $p2 = port(8082);
         var tests = [
              [
               'http://127.0.0.1:$p0/loc',
-               { headers: {Code: 201, Host: 'aaa'}},
+               { headers: {Code: 201}},
              ],
              [
               'http://127.0.0.1:$p0/loc',
-               { method:'POST', headers: {Code: 401, Host: 'bbb'}, body: 'OK'},
+               { method:'POST', headers: {Code: 401}, body: 'OK'},
              ],
              [
               'http://127.0.0.1:$p1/loc',
                { method:'PATCH',
-                 headers: {foo:undefined, bar:'xxx', Host: 'ccc'}},
+                 headers: {bar:'xxx'}},
              ],
            ];
 
@@ -366,10 +352,9 @@ my $p2 = port(8082);
     function loc(r) {
         var v = r.variables;
         var body = str(r.requestText);
-        var foo = str(r.headersIn.foo);
         var bar = str(r.headersIn.bar);
         var c = r.headersIn.code ? Number(r.headersIn.code) : 200;
-        r.return(c, `\${v.host}:\${v.request_method}:\${foo}:\${bar}:\${body}`);
+        r.return(c, `\${v.request_method}:\${bar}:\${body}`);
     }
 
      export default {njs: test_njs, body, broken, broken_response, body_special,
@@ -383,9 +368,9 @@ EOF
 
 ###############################################################################
 
-like(http_get('/body?getter=arrayBuffer&loc=loc'), qr/200 OK.*"aaa:GET:::"$/s,
+like(http_get('/body?getter=arrayBuffer&loc=loc'), qr/200 OK.*"GET::"$/s,
 	'fetch body arrayBuffer');
-like(http_get('/body?getter=text&loc=loc'), qr/200 OK.*"aaa:GET:::"$/s,
+like(http_get('/body?getter=text&loc=loc'), qr/200 OK.*"GET::"$/s,
 	'fetch body text');
 like(http_get('/body?getter=json&loc=json&path=b.c'),
 	qr/200 OK.*"FIELD"$/s, 'fetch body json');
@@ -428,9 +413,9 @@ like(http_get('/header?loc=duplicate_hea
 like(http_get('/header?loc=chunked/big&h=BAR&readBody=1'), qr/200 OK.*xxx$/s,
 	'fetch chunked header');
 is(get_json('/multi'),
-	'[{"b":"aaa:GET:::","c":201,"u":"http://127.0.0.1:'.$p0.'/loc"},' .
-	'{"b":"bbb:POST:::OK","c":401,"u":"http://127.0.0.1:'.$p0.'/loc"},' .
-	'{"b":"ccc:PATCH::xxx:","c":200,"u":"http://127.0.0.1:'.$p1.'/loc"}]',
+	'[{"b":"GET::","c":201,"u":"http://127.0.0.1:'.$p0.'/loc"},' .
+	'{"b":"PATCH:xxx:","c":200,"u":"http://127.0.0.1:'.$p1.'/loc"},' .
+	'{"b":"POST::OK","c":401,"u":"http://127.0.0.1:'.$p0.'/loc"}]',
 	'fetch multi');
 like(http_get('/multi?throw=1'), qr/500/s, 'fetch destructor');
 is(get_json('/broken'),
--- a/stream_js_fetch.t
+++ b/stream_js_fetch.t
@@ -45,11 +45,6 @@ http {
         location /njs {
             js_content test.njs;
         }
-    }
-
-    server {
-        listen       127.0.0.1:8080;
-        server_name  aaa;
 
         location /validate {
             js_content test.validate;
@@ -118,8 +113,7 @@ my $p = port(8080);
                 s.off('upstream');
 
                 let reply = await ngx.fetch('http://127.0.0.1:$p/validate',
-                                            {body: collect.slice(2,4),
-                                             headers: {Host:'aaa'}});
+                                            {body: collect.slice(2,4)});
 
                 (reply.status == 200) ? s.done(): s.deny();
 
@@ -139,8 +133,7 @@ my $p = port(8080);
                 s.off('upstream');
 
                 let reply = await ngx.fetch('http://127.0.0.1:$p/validate',
-                                            {body: collect.slice(2,4),
-                                             headers: {Host:'aaa'}});
+                                            {body: collect.slice(2,4)});
 
                 if (reply.status == 200) {
                     s.send(collect.slice(4), flags);
@@ -153,15 +146,13 @@ my $p = port(8080);
     }
 
     async function access_ok(s) {
-        let reply = await ngx.fetch('http://127.0.0.1:$p/success',
-                                    {headers: {Host:'aaa'}});
+        let reply = await ngx.fetch('http://127.0.0.1:$p/success');
 
         (reply.status == 200) ? s.allow(): s.deny();
     }
 
     async function access_nok(s) {
-        let reply = await ngx.fetch('http://127.0.0.1:$p/fail',
-                                    {headers: {Host:'aaa'}});
+        let reply = await ngx.fetch('http://127.0.0.1:$p/fail');
 
         (reply.status == 200) ? s.allow(): s.deny();
     }
--- a/stream_js_fetch_init.t
+++ b/stream_js_fetch_init.t
@@ -57,11 +57,6 @@ http {
         location /njs {
             js_content test.njs;
         }
-    }
-
-    server {
-        listen       127.0.0.1:8080;
-        server_name  aaa;
 
         location /success {
             return 200;
@@ -79,8 +74,7 @@ my $p = port(8080);
     }
 
     async function access_ok(s) {
-        let reply = await ngx.fetch('http://127.0.0.1:$p/success',
-                                    {headers: {Host:'aaa'}});
+        let reply = await ngx.fetch('http://127.0.0.1:$p/success');
 
         (reply.status == 200) ? s.allow(): s.deny();
     }