[njs] Tests: improved js_body_filter.t tests.

noreply at nginx.com noreply at nginx.com
Wed Apr 30 05:51:02 UTC 2025


details:   https://github.com/nginx/njs/commit/7f901b6f87a29ab74d18040c18aebb5b5918b028
branches:  master
commit:    7f901b6f87a29ab74d18040c18aebb5b5918b028
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon, 28 Apr 2025 18:34:29 -0700
description:
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.

---
 nginx/t/js_body_filter.t | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/nginx/t/js_body_filter.t b/nginx/t/js_body_filter.t
index d0c74238..5fc8292a 100644
--- a/nginx/t/js_body_filter.t
+++ b/nginx/t/js_body_filter.t
@@ -46,6 +46,7 @@ http {
         }
 
         location /append {
+            js_header_filter test.clear_content_length;
             js_body_filter test.append;
             proxy_pass http://127.0.0.1:8081/source;
         }
@@ -67,11 +68,13 @@ http {
 
         location /filter {
             proxy_buffering off;
+            js_header_filter test.clear_content_length;
             js_body_filter test.filter;
             proxy_pass http://127.0.0.1:8081/source;
         }
 
         location /prepend {
+            js_header_filter test.clear_content_length;
             js_body_filter test.prepend;
             proxy_pass http://127.0.0.1:8081/source;
         }
@@ -108,6 +111,10 @@ $t->write_file('test.js', <<EOF);
         }
     }
 
+    function clear_content_length(r) {
+        delete r.headersOut['Content-Length'];
+    }
+
     var collect = Buffer.from([]);
     function buffer_type(r, data, flags) {
         collect = Buffer.concat([collect, data]);
@@ -134,6 +141,7 @@ $t->write_file('test.js', <<EOF);
         chunks.chain = chain;
 
         r.status = 200;
+        r.headersOut['Content-Length'] = chunks.reduce((a, b) => a + b.length, 0);
         r.sendHeader();
         chain(chunks, 0);
     }
@@ -170,15 +178,16 @@ $t->write_file('test.js', <<EOF);
     }
 
     export default {njs: test_njs, append, buffer_type, filter, forward,
-                    prepend, source, nonutf8_source};
+                    prepend, source, nonutf8_source, clear_content_length};
 
 EOF
 
-$t->try_run('no njs body filter')->plan(7);
+$t->try_run('no njs body filter')->plan(8);
 
 ###############################################################################
 
 like(http_get('/append'), qr/AAABBCDDDDXXX$/, 'append');
+unlike(http_get('/append'), qr/Content-Length/, 'append no content-length');
 like(http_get('/buffer_type'), qr/AAABBCDDDD$/, 'buffer type');
 like(http_get('/buffer_type_nonutf8'), qr/\xaa\xaa\xbb\xcc\xdd\xdd$/,
 	'buffer type nonutf8');


More information about the nginx-devel mailing list