1# vi:filetype=perl
2
3use lib 'lib';
4use Test::Nginx::Socket;
5
6repeat_each(2);
7
8plan tests => repeat_each() * (blocks() * 3);
9
10run_tests();
11
12__DATA__
13
14=== TEST 1: '
15--- config
16    location /test {
17        set                 $test "he'llo";
18        postgres_escape     $escaped $test;
19        echo                $escaped;
20    }
21--- request
22GET /test
23--- error_code: 200
24--- response_headers
25Content-Type: text/plain
26--- response_body
27'he''llo'
28--- timeout: 10
29
30
31
32=== TEST 2: \
33--- config
34    location /test {
35        set                 $test "he\\llo";
36        postgres_escape     $escaped $test;
37        echo                $escaped;
38    }
39--- request
40GET /test
41--- error_code: 200
42--- response_headers
43Content-Type: text/plain
44--- response_body
45'he\\llo'
46--- timeout: 10
47
48
49
50=== TEST 3: \'
51--- config
52    location /test {
53        set                 $test "he\\'llo";
54        postgres_escape     $escaped $test;
55        echo                $escaped;
56    }
57--- request
58GET /test
59--- error_code: 200
60--- response_headers
61Content-Type: text/plain
62--- response_body
63'he\\''llo'
64--- timeout: 10
65
66
67
68=== TEST 4: NULL
69--- config
70    location /test {
71        postgres_escape     $escaped $remote_user;
72        echo                $escaped;
73    }
74--- request
75GET /test
76--- error_code: 200
77--- response_headers
78Content-Type: text/plain
79--- response_body
80NULL
81--- timeout: 10
82
83
84
85=== TEST 5: empty string
86--- config
87    location /test {
88        set $empty          "";
89        postgres_escape     $escaped $empty;
90        echo                $escaped;
91    }
92--- request
93GET /test
94--- error_code: 200
95--- response_headers
96Content-Type: text/plain
97--- response_body
98NULL
99--- timeout: 10
100
101
102
103=== TEST 6: UTF-8
104--- config
105    location /test {
106        set $utf8           "你好";
107        postgres_escape     $escaped $utf8;
108        echo                $escaped;
109    }
110--- request
111GET /test
112--- error_code: 200
113--- response_headers
114Content-Type: text/plain
115--- response_body
116'你好'
117--- timeout: 10
118
119
120
121=== TEST 7: user arg
122--- config
123    location /test {
124        postgres_escape     $escaped $arg_say;
125        echo                $escaped;
126    }
127--- request
128GET /test?say=he'llo!
129--- error_code: 200
130--- response_headers
131Content-Type: text/plain
132--- response_body
133'he''llo!'
134--- timeout: 10
135
136
137
138=== TEST 8: NULL (empty)
139--- config
140    location /test {
141        postgres_escape     $escaped =$remote_user;
142        echo                $escaped;
143    }
144--- request
145GET /test
146--- error_code: 200
147--- response_headers
148Content-Type: text/plain
149--- response_body
150''
151--- timeout: 10
152
153
154
155=== TEST 9: empty string (empty)
156--- config
157    location /test {
158        set $empty          "";
159        postgres_escape     $escaped =$empty;
160        echo                $escaped;
161    }
162--- request
163GET /test
164--- error_code: 200
165--- response_headers
166Content-Type: text/plain
167--- response_body
168''
169--- timeout: 10
170
171
172
173=== TEST 10: in-place escape
174--- config
175    location /test {
176        set                 $test "t'\\est";
177        postgres_escape     $test;
178        echo                $test;
179    }
180--- request
181GET /test
182--- error_code: 200
183--- response_headers
184Content-Type: text/plain
185--- response_body
186't''\\est'
187--- timeout: 10
188
189
190
191=== TEST 11: re-useable variable name (test1)
192--- config
193    location /test1 {
194        set                 $a "a";
195        postgres_escape     $escaped $a;
196        echo                $escaped;
197    }
198    location /test2 {
199        set                 $b "b";
200        postgres_escape     $escaped $b;
201        echo                $escaped;
202    }
203--- request
204GET /test1
205--- error_code: 200
206--- response_headers
207Content-Type: text/plain
208--- response_body
209'a'
210--- timeout: 10
211
212
213
214=== TEST 12: re-useable variable name (test2)
215--- config
216    location /test1 {
217        set                 $a "a";
218        postgres_escape     $escaped $a;
219        echo                $escaped;
220    }
221    location /test2 {
222        set                 $b "b";
223        postgres_escape     $escaped $b;
224        echo                $escaped;
225    }
226--- request
227GET /test2
228--- error_code: 200
229--- response_headers
230Content-Type: text/plain
231--- response_body
232'b'
233--- timeout: 10
234
235
236
237=== TEST 13: concatenate multiple sources
238--- config
239    location /test {
240        set                 $test "t'\\est";
241        set                 $hello " he'llo";
242        postgres_escape     $escaped "$test$hello world!";
243        echo                $escaped;
244    }
245--- request
246GET /test
247--- error_code: 200
248--- response_headers
249Content-Type: text/plain
250--- response_body
251't''\\est he''llo world!'
252--- timeout: 10
253
254
255
256=== TEST 14: concatenate multiple empty sources
257--- config
258    location /test {
259        set                 $a "";
260        set                 $b "";
261        postgres_escape     $escaped "$a$b";
262        echo                $escaped;
263    }
264--- request
265GET /test
266--- error_code: 200
267--- response_headers
268Content-Type: text/plain
269--- response_body
270NULL
271--- timeout: 10
272
273
274
275=== TEST 15: concatenate multiple empty sources (empty)
276--- config
277    location /test {
278        set                 $a "";
279        set                 $b "";
280        postgres_escape     $escaped "=$a$b";
281        echo                $escaped;
282    }
283--- request
284GET /test
285--- error_code: 200
286--- response_headers
287Content-Type: text/plain
288--- response_body
289''
290--- timeout: 10
291
292
293
294=== TEST 16: in-place escape on empty string
295--- config
296    location /test {
297        set                 $test "";
298        postgres_escape     $test;
299        echo                $test;
300    }
301--- request
302GET /test
303--- error_code: 200
304--- response_headers
305Content-Type: text/plain
306--- response_body
307NULL
308--- timeout: 10
309
310
311
312=== TEST 17: in-place escape on empty string (empty)
313--- config
314    location /test {
315        set                 $test "";
316        postgres_escape     =$test;
317        echo                $test;
318    }
319--- request
320GET /test
321--- error_code: 200
322--- response_headers
323Content-Type: text/plain
324--- response_body
325''
326--- timeout: 10
327
328
329
330=== TEST 18: escape anonymous regex capture
331--- config
332    location ~ /(.*) {
333        postgres_escape     $escaped $1;
334        echo                $escaped;
335    }
336--- request
337GET /test
338--- error_code: 200
339--- response_headers
340Content-Type: text/plain
341--- response_body
342'test'
343--- timeout: 10
344
345
346
347=== TEST 19: escape named regex capture
348--- config
349    location ~ /(?<test>.*) {
350        postgres_escape     $escaped $test;
351        echo                $escaped;
352    }
353--- request
354GET /test
355--- error_code: 200
356--- response_headers
357Content-Type: text/plain
358--- response_body
359'test'
360--- timeout: 10
361--- skip_nginx: 3: < 0.8.25
362