Skip to content

Commit 7f901b6

Browse files
committed
Tests: improved js_body_filter.t tests.
Dropping Content-Length header in locations where response body length is modified. This is not strictly needed for the test itself, but can serve as an example for a typical body modification task.
1 parent 1496ed3 commit 7f901b6

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

nginx/t/js_body_filter.t

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ http {
4646
}
4747
4848
location /append {
49+
js_header_filter test.clear_content_length;
4950
js_body_filter test.append;
5051
proxy_pass http://127.0.0.1:8081/source;
5152
}
@@ -67,11 +68,13 @@ http {
6768
6869
location /filter {
6970
proxy_buffering off;
71+
js_header_filter test.clear_content_length;
7072
js_body_filter test.filter;
7173
proxy_pass http://127.0.0.1:8081/source;
7274
}
7375
7476
location /prepend {
77+
js_header_filter test.clear_content_length;
7578
js_body_filter test.prepend;
7679
proxy_pass http://127.0.0.1:8081/source;
7780
}
@@ -108,6 +111,10 @@ $t->write_file('test.js', <<EOF);
108111
}
109112
}
110113
114+
function clear_content_length(r) {
115+
delete r.headersOut['Content-Length'];
116+
}
117+
111118
var collect = Buffer.from([]);
112119
function buffer_type(r, data, flags) {
113120
collect = Buffer.concat([collect, data]);
@@ -134,6 +141,7 @@ $t->write_file('test.js', <<EOF);
134141
chunks.chain = chain;
135142
136143
r.status = 200;
144+
r.headersOut['Content-Length'] = chunks.reduce((a, b) => a + b.length, 0);
137145
r.sendHeader();
138146
chain(chunks, 0);
139147
}
@@ -170,15 +178,16 @@ $t->write_file('test.js', <<EOF);
170178
}
171179
172180
export default {njs: test_njs, append, buffer_type, filter, forward,
173-
prepend, source, nonutf8_source};
181+
prepend, source, nonutf8_source, clear_content_length};
174182
175183
EOF
176184

177-
$t->try_run('no njs body filter')->plan(7);
185+
$t->try_run('no njs body filter')->plan(8);
178186

179187
###############################################################################
180188

181189
like(http_get('/append'), qr/AAABBCDDDDXXX$/, 'append');
190+
unlike(http_get('/append'), qr/Content-Length/, 'append no content-length');
182191
like(http_get('/buffer_type'), qr/AAABBCDDDD$/, 'buffer type');
183192
like(http_get('/buffer_type_nonutf8'), qr/\xaa\xaa\xbb\xcc\xdd\xdd$/,
184193
'buffer type nonutf8');

0 commit comments

Comments
 (0)