1# vi:filetype=
2
3use lib 'lib';
4use Test::Nginx::Socket;
5
6repeat_each(2);
7
8plan tests => repeat_each() * 2 * blocks();
9
10$ENV{TEST_NGINX_MYSQL_HOST} ||= '127.0.0.1';
11$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306;
12
13our $http_config = <<'_EOC_';
14    upstream backend {
15        drizzle_server $TEST_NGINX_MYSQL_HOST:$TEST_NGINX_MYSQL_PORT protocol=mysql
16                       dbname=ngx_test user=ngx_test password=ngx_test;
17    }
18_EOC_
19
20no_diff();
21no_long_string();
22
23run_tests();
24
25__DATA__
26
27=== TEST 1: sanity
28--- http_config eval: $::http_config
29--- config
30    location /mysql {
31        drizzle_query '
32            select * from cats order by id asc
33        ';
34        drizzle_pass backend;
35        rds_json on;
36        rds_json_root rows;
37    }
38--- request
39GET /mysql
40--- response_body chomp
41{"rows":[{"id":2,"name":null},{"id":3,"name":"bob"}]}
42
43
44
45=== TEST 2: update (root)
46--- http_config eval: $::http_config
47--- config
48    location /mysql {
49        drizzle_pass backend;
50        #drizzle_dbname $dbname;
51        drizzle_query "update cats set name='bob' where name='bob'";
52        rds_json on;
53        rds_json_root data;
54    }
55--- request
56GET /mysql
57--- response_body chop
58{"errcode":0,"errstr":"Rows matched: 1  Changed: 0  Warnings: 0"}
59
60
61
62=== TEST 3: select empty result
63--- http_config eval: $::http_config
64--- config
65    location /mysql {
66        drizzle_pass backend;
67        drizzle_query "select * from cats where name='tom'";
68        rds_json on;
69        rds_json_root data;
70    }
71--- request
72GET /mysql
73--- response_body chop
74{"data":[]}
75
76
77
78=== TEST 4: sanity + compact
79--- http_config eval: $::http_config
80--- config
81    location /mysql {
82        drizzle_query '
83            select * from cats order by id asc
84        ';
85        drizzle_pass backend;
86        rds_json on;
87        rds_json_root rows;
88        rds_json_format compact;
89    }
90--- request
91GET /mysql
92--- response_body chomp
93{"rows":[["id","name"],[2,null],[3,"bob"]]}
94
95
96
97=== TEST 5: select empty result + compact
98--- http_config eval: $::http_config
99--- config
100    location /mysql {
101        drizzle_pass backend;
102        drizzle_query "select * from cats where name='tom'";
103        rds_json on;
104        rds_json_root data;
105        rds_json_format compact;
106    }
107--- request
108GET /mysql
109--- response_body chop
110{"data":[["id","name"]]}
111
112
113
114=== TEST 6: select empty result + compact + escaping
115--- http_config eval: $::http_config
116--- config
117    location /mysql {
118        drizzle_pass backend;
119        drizzle_query "select * from cats where name='tom'";
120        rds_json on;
121        rds_json_root "'\"\\:";
122        rds_json_format compact;
123    }
124--- request
125GET /mysql
126--- response_body chop
127{"'\"\\:":[["id","name"]]}
128
129
130
131=== TEST 7: success property (inherited)
132--- http_config eval: $::http_config
133--- config
134    rds_json_success_property "suc";
135    location /mysql {
136        drizzle_pass backend;
137        drizzle_query "select * from cats where name='tom'";
138        rds_json on;
139        rds_json_root "rows";
140        rds_json_format compact;
141    }
142--- request
143GET /mysql
144--- response_body chop
145{"suc":true,"rows":[["id","name"]]}
146
147
148
149=== TEST 8: success property (root inherited)
150--- http_config eval: $::http_config
151--- config
152    rds_json_root "rows";
153    location /mysql {
154        drizzle_pass backend;
155        drizzle_query "select * from cats where name='tom'";
156        rds_json on;
157        rds_json_success_property "suc";
158        rds_json_format compact;
159    }
160--- request
161GET /mysql
162--- response_body chop
163{"suc":true,"rows":[["id","name"]]}
164
165
166
167=== TEST 9: success property
168--- http_config eval: $::http_config
169--- config
170    location /mysql {
171        drizzle_pass backend;
172        drizzle_query "select * from cats where name='tom'";
173        rds_json on;
174        #rds_json_root "data";
175        rds_json_success_property "suc";
176        rds_json_format compact;
177    }
178--- request
179GET /mysql
180--- response_body chop
181{"suc":true,"data":[["id","name"]]}
182
183
184
185=== TEST 10: success property with an odd key
186--- http_config eval: $::http_config
187--- config
188    location /mysql {
189        drizzle_pass backend;
190        drizzle_query "select * from cats where name='tom'";
191        rds_json on;
192        rds_json_root "data";
193        rds_json_success_property "a\"'\\:";
194        rds_json_format compact;
195    }
196--- request
197GET /mysql
198--- response_body chop
199{"a\"'\\:":true,"data":[["id","name"]]}
200
201
202
203=== TEST 11: update (root + success prop)
204--- http_config eval: $::http_config
205--- config
206    location /mysql {
207        drizzle_pass backend;
208        #drizzle_dbname $dbname;
209        drizzle_query "update cats set name='bob' where name='bob'";
210        rds_json on;
211        rds_json_root rows;
212        rds_json_success_property success;
213    }
214--- request
215GET /mysql
216--- response_body chop
217{"success":true,"errcode":0,"errstr":"Rows matched: 1  Changed: 0  Warnings: 0"}
218
219
220
221=== TEST 12: update (user property)
222--- http_config eval: $::http_config
223--- config
224    location /mysql {
225        drizzle_pass backend;
226        #drizzle_dbname $dbname;
227        drizzle_query "update cats set name='bob' where name='bob'";
228        rds_json on;
229
230        set $name 'Jimmy';
231        rds_json_user_property name $name;
232    }
233--- request
234GET /mysql
235--- response_body chop
236{"name":"Jimmy","errcode":0,"errstr":"Rows matched: 1  Changed: 0  Warnings: 0"}
237
238
239
240=== TEST 13: update (multi user property)
241--- http_config eval: $::http_config
242--- config
243    location /mysql {
244        drizzle_pass backend;
245        #drizzle_dbname $dbname;
246        drizzle_query "update cats set name='bob' where name='bob'";
247        rds_json on;
248
249        set $name 'Jimmy"';
250        set $age 32;
251        rds_json_user_property name $name;
252        rds_json_user_property age $age;
253    }
254--- request
255GET /mysql
256--- response_body chop
257{"name":"Jimmy\"","age":"32","errcode":0,"errstr":"Rows matched: 1  Changed: 0  Warnings: 0"}
258
259
260
261=== TEST 14: compact + user property
262--- http_config eval: $::http_config
263--- config
264    location /mysql {
265        drizzle_query '
266            select * from cats order by id asc
267        ';
268        drizzle_pass backend;
269        rds_json on;
270        rds_json_root rows;
271        set $val 'bar"';
272        rds_json_user_property foo $val;
273        rds_json_format compact;
274    }
275--- request
276GET /mysql
277--- response_body chomp
278{"foo":"bar\"","rows":[["id","name"],[2,null],[3,"bob"]]}
279
280
281
282=== TEST 15: compact + user property + success
283--- http_config eval: $::http_config
284--- config
285    location /mysql {
286        drizzle_query '
287            select * from cats order by id asc
288        ';
289        drizzle_pass backend;
290
291        rds_json on;
292        rds_json_root rows;
293
294        rds_json_success_property suc;
295
296        set $val 'bar"';
297        rds_json_user_property foo $val;
298
299        set $baz '"baz"\\';
300        rds_json_user_property '\\bar' $baz;
301        rds_json_format compact;
302    }
303--- request
304GET /mysql
305--- response_body chomp
306{"suc":true,"foo":"bar\"","\\bar":"\"baz\"\\","rows":[["id","name"],[2,null],[3,"bob"]]}
307
308
309
310=== TEST 16: rds_json_ret with success property
311--- http_config eval: $::http_config
312--- config
313    location /ret {
314        rds_json_success_property ret;
315
316        rds_json_ret 400 'Non zero ret';
317    }
318--- request
319GET /ret
320--- response_body chomp
321{"errcode":400,"errstr":"Non zero ret","ret":false}
322
323
324
325=== TEST 17: rds_json_ret with user property
326--- http_config eval: $::http_config
327--- config
328    location /ret {
329        set $city 'beijing';
330        rds_json_user_property city $city;
331        rds_json_ret 400 'Non zero ret';
332    }
333--- request
334GET /ret
335--- response_body chomp
336{"errcode":400,"errstr":"Non zero ret","city":"beijing"}
337
338
339
340=== TEST 18: rds_json_ret with property
341--- http_config eval: $::http_config
342--- config
343    location /ret {
344        rds_json_success_property ret;
345
346        set $city 'beijing';
347        rds_json_user_property city $city;
348
349        rds_json_ret 400 'Non zero ret';
350    }
351--- request
352GET /ret
353--- response_body chomp
354{"errcode":400,"errstr":"Non zero ret","ret":false,"city":"beijing"}
355
356
357
358=== TEST 19: rds_json_ret when errcode equal 0
359--- http_config eval: $::http_config
360--- config
361    location /ret {
362        rds_json_success_property ret;
363
364        set $city 'beijing';
365        rds_json_user_property city $city;
366
367        rds_json_ret 0 'Zero errcode';
368    }
369--- request
370GET /ret
371--- response_body chomp
372{"errcode":0,"errstr":"Zero errcode","ret":true,"city":"beijing"}
373
374
375
376=== TEST 20: rds_json_ret with multiple user properties
377--- http_config eval: $::http_config
378--- config
379    location /ret {
380        set $city 'beijing';
381        rds_json_user_property city $city;
382        rds_json_user_property '"hi"' '"hello\n"';
383        rds_json_ret 400 'Non zero ret';
384    }
385--- request
386GET /ret
387--- response_body chomp
388{"errcode":400,"errstr":"Non zero ret","city":"beijing","\"hi\"":"\"hello\n\""}
389
390
391
392=== TEST 21: location internal rewrite
393--- config
394    location @err403 {
395        rds_json_success_property ret;
396        rds_json_ret 403 "Forbidden";
397    }
398
399    location /foo {
400        error_page 403 = @err403;
401        return 403;
402    }
403--- request
404    GET /foo
405--- response_body chop
406{"errcode":403,"errstr":"Forbidden","ret":false}
407
408
409
410=== TEST 22: rds_json_errcode_key
411--- config
412    location /foo {
413        rds_json_errcode_key "ecode";
414        rds_json_success_property ret;
415        rds_json_ret 403 "Forbidden";
416    }
417--- request
418    GET /foo
419--- response_body chop
420{"ecode":403,"errstr":"Forbidden","ret":false}
421
422
423
424=== TEST 23: rds_json_errcode_key
425--- config
426    rds_json_errcode_key "ecoderoot";
427
428    location /foo {
429        rds_json_success_property ret;
430        rds_json_ret 403 "Forbidden";
431    }
432--- request
433    GET /foo
434--- response_body chop
435{"ecoderoot":403,"errstr":"Forbidden","ret":false}
436
437
438
439=== TEST 24: rds_json_errcode_key
440--- config
441    rds_json_errcode_key "ecoderoot";
442
443    location /foo {
444        rds_json_errcode_key "ecode";
445        rds_json_success_property ret;
446        rds_json_ret 403 "Forbidden";
447    }
448--- request
449    GET /foo
450--- response_body chop
451{"ecode":403,"errstr":"Forbidden","ret":false}
452
453
454
455=== TEST 25: rds_json_errstr_key
456--- config
457    rds_json_errstr_key "msg";
458
459    location /foo {
460        rds_json_success_property ret;
461        rds_json_ret 403 "Forbidden";
462    }
463--- request
464    GET /foo
465--- response_body chop
466{"errcode":403,"msg":"Forbidden","ret":false}
467
468
469
470=== TEST 26: rds_json_errstr_key
471--- config
472    rds_json_errstr_key "msg";
473
474    location /foo {
475        rds_json_errstr_key "msg2";
476        rds_json_success_property ret;
477        rds_json_ret 403 "Forbidden";
478    }
479--- request
480    GET /foo
481--- response_body chop
482{"errcode":403,"msg2":"Forbidden","ret":false}
483
484
485
486=== TEST 27: rds_json_errstr_key & rds_json_root
487--- config
488    rds_json_errstr_key "msg2";
489    rds_json_root rows;
490
491    location /foo {
492        rds_json_success_property ret;
493        rds_json_ret 403 "Forbidden";
494    }
495--- request
496    GET /foo
497--- response_body chop
498{"errcode":403,"msg2":"Forbidden","ret":false}
499
500
501
502=== TEST 28: rds_json_errcode_key & rds_json_root
503--- config
504    rds_json_root rows;
505    rds_json_errcode_key "code";
506
507    location /foo {
508        rds_json_success_property ret;
509        rds_json_ret 403 "Forbidden";
510    }
511--- request
512    GET /foo
513--- response_body chop
514{"code":403,"errstr":"Forbidden","ret":false}
515
516
517
518=== TEST 29: update - custom errstr_key
519--- http_config eval: $::http_config
520--- config
521    location /mysql {
522        drizzle_pass backend;
523        #drizzle_dbname $dbname;
524        drizzle_query "update cats set name='bob' where name='bob'";
525        rds_json on;
526        rds_json_errcode_key "\"code\"";
527        rds_json_errstr_key "\"str\"";
528
529        set $name 'Jimmy"';
530        set $age 32;
531        rds_json_user_property name $name;
532        rds_json_user_property age $age;
533    }
534--- request
535GET /mysql
536--- response_body chop
537{"name":"Jimmy\"","age":"32","\"code\"":0,"\"str\"":"Rows matched: 1  Changed: 0  Warnings: 0"}
538
539
540
541=== TEST 30: select - custom errstr_key
542--- http_config eval: $::http_config
543--- config
544    location /mysql {
545        drizzle_pass backend;
546        drizzle_query "select 'aaa' as a, 'bbb' as b";
547        rds_json on;
548        rds_json_errcode_key "\"code\"";
549        rds_json_errstr_key "\"str\"";
550
551        rds_json_root data;
552
553        set $name 'Jimmy"';
554        set $age 32;
555        rds_json_user_property name $name;
556        rds_json_user_property age $age;
557    }
558--- request
559GET /mysql
560--- response_body chop
561{"name":"Jimmy\"","age":"32","data":[{"a":"aaa","b":"bbb"}]}
562