comparison stream_js_fetch.t @ 1784:898598722ad4

Tests: improved stream fetch test with async callbacks.
author Dmitry Volyntsev <xeioex@nginx.com>
date Tue, 23 Aug 2022 19:40:05 -0700
parents 6b6a496ac984
children 2c08bfffe112
comparison
equal deleted inserted replaced
1783:95ba1e704b7b 1784:898598722ad4
83 } 83 }
84 84
85 function preread_verify(s) { 85 function preread_verify(s) {
86 var collect = Buffer.from([]); 86 var collect = Buffer.from([]);
87 87
88 s.on('upstream', function (data, flags) { 88 s.on('upstream', async function (data, flags) {
89 collect = Buffer.concat([collect, data]); 89 collect = Buffer.concat([collect, data]);
90 90
91 if (collect.length >= 4 && collect.readUInt16BE(0) == 0xabcd) { 91 if (collect.length >= 4 && collect.readUInt16BE(0) == 0xabcd) {
92 s.off('upstream'); 92 s.off('upstream');
93 ngx.fetch('http://127.0.0.1:$p/validate', 93
94 {body: collect.slice(2,4), headers: {Host:'aaa'}}) 94 let reply = await ngx.fetch('http://127.0.0.1:$p/validate',
95 .then(reply => (reply.status == 200) ? s.done(): s.deny()) 95 {body: collect.slice(2,4),
96 headers: {Host:'aaa'}});
97
98 (reply.status == 200) ? s.done(): s.deny();
96 99
97 } else if (collect.length) { 100 } else if (collect.length) {
98 s.deny(); 101 s.deny();
99 } 102 }
100 }); 103 });