comparison js_buffer.t @ 1627:456bf219d768

Tests: adapted js buffer tests to renaming body properties.
author Dmitry Volyntsev <xeioex@nginx.com>
date Fri, 27 Nov 2020 13:10:55 +0000
parents a35445ae8de7
children f89770df737a
comparison
equal deleted inserted replaced
1626:a35445ae8de7 1627:456bf219d768
60 60
61 location /res_body { 61 location /res_body {
62 js_content test.res_body; 62 js_content test.res_body;
63 } 63 }
64 64
65 location /res_text {
66 js_content test.res_text;
67 }
68
65 location /binary_var { 69 location /binary_var {
66 js_content test.binary_var; 70 js_content test.binary_var;
67 } 71 }
68 72
69 location /p/ { 73 location /p/ {
93 body = Buffer.concat([body, Buffer.from(r.args.text)]); 97 body = Buffer.concat([body, Buffer.from(r.args.text)]);
94 r.return(200, body); 98 r.return(200, body);
95 } 99 }
96 100
97 function req_body(r) { 101 function req_body(r) {
98 var body = r.reqBody; 102 var body = r.requestBuffer;
99 var view = new DataView(body.buffer); 103 var view = new DataView(body.buffer);
100 view.setInt8(2, 'c'.charCodeAt(0)); 104 view.setInt8(2, 'c'.charCodeAt(0));
101 r.return(200, JSON.parse(body).c.b); 105 r.return(200, JSON.parse(body).c.b);
102 } 106 }
103 107
108 function type(v) {return Buffer.isBuffer(v) ? 'buffer' : (typeof v);}
109
104 function res_body(r) { 110 function res_body(r) {
105 r.subrequest('/p/sub1') 111 r.subrequest('/p/sub1')
106 .then(reply => { 112 .then(reply => {
107 var body = reply.resBody; 113 var body = reply.responseBuffer;
108 var view = new DataView(body.buffer); 114 var view = new DataView(body.buffer);
109 view.setInt8(2, 'c'.charCodeAt(0)); 115 view.setInt8(2, 'c'.charCodeAt(0));
110 r.return(200, JSON.stringify(JSON.parse(body))); 116 body = JSON.parse(body);
117 body.type = type(reply.responseBuffer);
118 r.return(200, JSON.stringify(body));
119 })
120 }
121
122 function res_text(r) {
123 r.subrequest('/p/sub1')
124 .then(reply => {
125 var body = JSON.parse(reply.responseText);
126 body.type = type(reply.responseText);
127 r.return(200, JSON.stringify(body));
111 }) 128 })
112 } 129 }
113 130
114 function binary_var(r) { 131 function binary_var(r) {
115 var test = r.rawVariables.binary_remote_addr 132 var test = r.rawVariables.binary_remote_addr
116 .equals(Buffer.from([127,0,0,1])); 133 .equals(Buffer.from([127,0,0,1]));
117 r.return(200, test); 134 r.return(200, test);
118 } 135 }
119 136
120 export default {njs: test_njs, return: test_return, req_body, res_body, 137 export default {njs: test_njs, return: test_return, req_body, res_body,
121 binary_var}; 138 res_text, binary_var};
122 139
123 EOF 140 EOF
124 141
125 $t->try_run('no njs buffer')->plan(4); 142 $t->try_run('no njs buffer')->plan(5);
126 143
127 ############################################################################### 144 ###############################################################################
128 145
129 TODO: { 146 TODO: {
130 local $TODO = 'not yet' 147 local $TODO = 'not yet'
131 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0'; 148 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0';
132 149
133 like(http_get('/return?text=FOO'), qr/200 OK.*body: FOO$/s, 150 like(http_get('/return?text=FOO'), qr/200 OK.*body: FOO$/s,
134 'return buffer'); 151 'return buffer');
135 like(http_post('/req_body'), qr/200 OK.*BAR$/s, 'req body'); 152 like(http_post('/req_body'), qr/200 OK.*BAR$/s, 'request buffer');
136 is(get_json('/res_body'), '{"c":{"b":1}}', 'res body'); 153 is(get_json('/res_body'), '{"c":{"b":1},"type":"buffer"}', 'response buffer');
154 is(get_json('/res_text'), '{"a":{"b":1},"type":"string"}', 'response text');
137 like(http_get('/binary_var'), qr/200 OK.*true$/s, 155 like(http_get('/binary_var'), qr/200 OK.*true$/s,
138 'binary var'); 156 'binary var');
139 157
140 } 158 }
141 159