Skip to content

Commit 0085a9b

Browse files
tests: added test cases for escaping control and utf8 characters in ngx.req.add_header and ngx.resp.add_header
1 parent 0621e33 commit 0085a9b

File tree

2 files changed

+145
-1
lines changed

2 files changed

+145
-1
lines changed

t/ngx-req.t

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,83 @@ ok
235235
bad 'value' argument: string or table expected, got nil
236236
bad 'value' argument: string or table expected, got nil
237237
bad 'value' argument: non-empty table expected
238+
239+
240+
241+
=== TEST 12: ngx_req.add_header (header name with control characters)
242+
--- config
243+
location /bar {
244+
access_by_lua_block {
245+
local ngx_req = require "ngx.req"
246+
ngx_req.add_header("header\r\nabc", "value")
247+
}
248+
proxy_pass http://127.0.0.1:$server_port/foo;
249+
}
250+
251+
location = /foo {
252+
echo $echo_client_request_headers;
253+
}
254+
--- request
255+
GET /bar
256+
--- response_body_like chomp
257+
\bheader%0D%0Aabc: value\r\n
258+
259+
260+
261+
=== TEST 13: ngx_req.add_header (header value with control characters)
262+
--- config
263+
location /bar {
264+
access_by_lua_block {
265+
local ngx_req = require "ngx.req"
266+
ngx_req.add_header("header", "value\r\nabc")
267+
}
268+
proxy_pass http://127.0.0.1:$server_port/foo;
269+
}
270+
271+
location = /foo {
272+
echo $echo_client_request_headers;
273+
}
274+
--- request
275+
GET /bar
276+
--- response_body_like chomp
277+
\bheader: value%0D%0Aabc\r\n
278+
279+
280+
281+
=== TEST 14: ngx_req.add_header (header name with Chinese characters)
282+
--- config
283+
location /bar {
284+
access_by_lua_block {
285+
local ngx_req = require "ngx.req"
286+
ngx_req.add_header("header中文", "value")
287+
}
288+
proxy_pass http://127.0.0.1:$server_port/foo;
289+
}
290+
291+
location = /foo {
292+
echo $echo_client_request_headers;
293+
}
294+
--- request
295+
GET /bar
296+
--- response_body_like chomp
297+
\bheader%E4%B8%AD%E6%96%87: value
298+
299+
300+
301+
=== TEST 15: ngx_req.add_header (header value with Chinese characters)
302+
--- config
303+
location /bar {
304+
access_by_lua_block {
305+
local ngx_req = require "ngx.req"
306+
ngx_req.add_header("header", "value中文")
307+
}
308+
proxy_pass http://127.0.0.1:$server_port/foo;
309+
}
310+
311+
location = /foo {
312+
echo $echo_client_request_headers;
313+
}
314+
--- request
315+
GET /bar
316+
--- response_body_like chomp
317+
\bheader: value中文

t/ngx-resp.t

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use t::TestCore;
33

44
repeat_each(2);
55

6-
plan tests => repeat_each() * (blocks() * 3);
6+
plan tests => repeat_each() * (blocks() * 3 + 4);
77

88
add_block_preprocessor(sub {
99
my $block = shift;
@@ -120,3 +120,67 @@ Date: now
120120
}
121121
--- response_body
122122
Foo: aaa
123+
124+
125+
126+
=== TEST 7: ngx.resp.add_header (header name with control characters)
127+
--- config
128+
location = /t {
129+
content_by_lua_block {
130+
local ngx_resp = require "ngx.resp"
131+
ngx_resp.add_header("head\r\n", "value")
132+
ngx.say("OK")
133+
}
134+
}
135+
--- response_body
136+
OK
137+
--- response_headers
138+
head%0D%0A: value
139+
140+
141+
142+
=== TEST 8: ngx.resp.add_header (header value with control characters)
143+
--- config
144+
location = /t {
145+
content_by_lua_block {
146+
local ngx_resp = require "ngx.resp"
147+
ngx_resp.add_header("head", "value\r\n")
148+
ngx.say("OK")
149+
}
150+
}
151+
--- response_body
152+
OK
153+
--- response_headers
154+
head: value%0D%0A
155+
156+
157+
158+
=== TEST 9: ngx.resp.add_header (header name with Chinese characters)
159+
--- config
160+
location = /t {
161+
content_by_lua_block {
162+
local ngx_resp = require "ngx.resp"
163+
ngx_resp.add_header("head中文", "value")
164+
ngx.say("OK")
165+
}
166+
}
167+
--- response_body
168+
OK
169+
--- response_headers
170+
head%E4%B8%AD%E6%96%87: value
171+
172+
173+
174+
=== TEST 10: ngx.resp.add_header (header value with Chinese characters)
175+
--- config
176+
location = /t {
177+
content_by_lua_block {
178+
local ngx_resp = require "ngx.resp"
179+
ngx_resp.add_header("head", "value中文")
180+
ngx.say("OK")
181+
}
182+
}
183+
--- response_body
184+
OK
185+
--- response_headers
186+
head: value中文

0 commit comments

Comments
 (0)