Skip to content

Commit 7d6ba14

Browse files
committed
Tests: improved fetch tests by avoding match for specific messages.
1 parent f38b602 commit 7d6ba14

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

js_fetch.t

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ http {
7070
js_content test.chain;
7171
}
7272
73-
location /chunked {
74-
js_content test.chunked;
73+
location /chunked_ok {
74+
js_content test.chunked_ok;
75+
}
76+
77+
location /chunked_fail {
78+
js_content test.chunked_fail;
7579
}
7680
7781
location /header {
@@ -222,30 +226,48 @@ $t->write_file('test.js', <<EOF);
222226
next();
223227
}
224228
225-
function chunked(r) {
229+
function chunked_ok(r) {
226230
var results = [];
227231
var tests = [
228-
['http://127.0.0.1:$p2/big', {max_response_body_size:128000}],
229232
['http://127.0.0.1:$p2/big/ok', {max_response_body_size:128000}],
230-
['http://127.0.0.1:$p2/chunked'],
231233
['http://127.0.0.1:$p2/chunked/ok'],
232-
['http://127.0.0.1:$p2/chunked/big', {max_response_body_size:128}],
233234
['http://127.0.0.1:$p2/chunked/big'],
234235
];
235236
236237
function collect(v) {
237238
results.push(v);
238239
239240
if (results.length == tests.length) {
240-
results.sort();
241-
r.return(200, JSON.stringify(results));
241+
r.return(200);
242242
}
243243
}
244244
245245
tests.forEach(args => {
246246
ngx.fetch.apply(r, args)
247247
.then(reply => reply.text())
248248
.then(body => collect(body.length))
249+
})
250+
}
251+
252+
function chunked_fail(r) {
253+
var results = [];
254+
var tests = [
255+
['http://127.0.0.1:$p2/big', {max_response_body_size:128000}],
256+
['http://127.0.0.1:$p2/chunked'],
257+
['http://127.0.0.1:$p2/chunked/big', {max_response_body_size:128}],
258+
];
259+
260+
function collect(v) {
261+
results.push(v);
262+
263+
if (results.length == tests.length) {
264+
r.return(200);
265+
}
266+
}
267+
268+
tests.forEach(args => {
269+
ngx.fetch.apply(r, args)
270+
.then(reply => reply.text())
249271
.catch(e => collect(e.message))
250272
})
251273
}
@@ -358,10 +380,11 @@ $t->write_file('test.js', <<EOF);
358380
}
359381
360382
export default {njs: test_njs, body, broken, broken_response, body_special,
361-
chain, chunked, header, header_iter, multi, loc, property};
383+
chain, chunked_ok, chunked_fail, header, header_iter,
384+
multi, loc, property};
362385
EOF
363386

364-
$t->try_run('no njs.fetch')->plan(33);
387+
$t->try_run('no njs.fetch')->plan(34);
365388

366389
$t->run_daemon(\&http_daemon, port(8082));
367390
$t->waitforsocket('127.0.0.1:' . port(8082));
@@ -418,22 +441,10 @@ is(get_json('/multi'),
418441
'{"b":"POST::OK","c":401,"u":"http://127.0.0.1:'.$p0.'/loc"}]',
419442
'fetch multi');
420443
like(http_get('/multi?throw=1'), qr/500/s, 'fetch destructor');
421-
is(get_json('/broken'),
422-
'[' .
423-
'"connect failed",' .
424-
'"failed to convert url arg",' .
425-
'"invalid url"]', 'fetch broken');
426-
is(get_json('/broken_response'),
427-
'["invalid fetch content length",' .
428-
'"invalid fetch header",' .
429-
'"invalid fetch status line",' .
430-
'"prematurely closed connection",' .
431-
'"prematurely closed connection"]', 'fetch broken response');
432-
is(get_json('/chunked'),
433-
'[10,100010,25500,' .
434-
'"invalid fetch chunked response",' .
435-
'"prematurely closed connection",' .
436-
'"very large fetch chunked response"]', 'fetch chunked');
444+
like(http_get('/broken'), qr/200/s, 'fetch broken');
445+
like(http_get('/broken_response'), qr/200/s, 'fetch broken response');
446+
like(http_get('/chunked_ok'), qr/200/s, 'fetch chunked ok');
447+
like(http_get('/chunked_fail'), qr/200/s, 'fetch chunked fail');
437448
like(http_get('/chain'), qr/200 OK.*SUCCESS$/s, 'fetch chain');
438449

439450
TODO: {

0 commit comments

Comments
 (0)