1# vi:filetype=
2
3use lib 'lib';
4use Test::Nginx::Socket;
5
6repeat_each(2);
7
8plan tests => repeat_each() * 3 * blocks();
9
10$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306;
11
12our $http_config = <<'_EOC_';
13    upstream backend {
14        drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql
15                       dbname=ngx_test user=ngx_test password=ngx_test;
16    }
17_EOC_
18
19#no_diff();
20no_long_string();
21
22run_tests();
23
24__DATA__
25
26=== TEST 1: sanity
27--- http_config eval: $::http_config
28--- config
29    location /mysql {
30        drizzle_query "
31            select * from dogs order by name asc;
32        ";
33        drizzle_pass backend;
34        rds_csv on;
35    }
36--- request
37POST /mysql
38sql=select%20*%20from%20cats;
39--- response_headers
40Content-Type: text/csv; header=presence
41--- response_body eval
42qq{male,name,height\r
430,"\rkay",0.005\r
440,ab;c,0.005\r
450,foo\tbar,21\r
460,"hello ""tom",3.14\r
470,"hey
48dad",7\r
491,"hi,ya",-3\r
50}
51
52
53
54=== TEST 2: using ; as the field separator
55--- http_config eval: $::http_config
56--- config
57    location /mysql {
58        drizzle_query "
59            select * from dogs order by name asc;
60        ";
61        drizzle_pass backend;
62        rds_csv_field_separator ';';
63        rds_csv on;
64    }
65--- request
66POST /mysql
67sql=select%20*%20from%20cats;
68--- response_headers
69Content-Type: text/csv; header=presence
70--- response_body eval
71qq{male;name;height\r
720;"\rkay";0.005\r
730;"ab;c";0.005\r
740;foo\tbar;21\r
750;"hello ""tom";3.14\r
760;"hey
77dad";7\r
781;hi,ya;-3\r
79}
80
81
82
83=== TEST 3: using tab as the field separator
84--- http_config eval: $::http_config
85--- config
86    location /mysql {
87        drizzle_query "
88            select * from dogs order by name asc;
89        ";
90        drizzle_pass backend;
91        rds_csv_field_separator '\t';
92        rds_csv on;
93    }
94--- request
95POST /mysql
96sql=select%20*%20from%20cats;
97--- response_headers
98Content-Type: text/csv; header=presence
99--- response_body eval
100qq{male\tname\theight\r
1010\t"\rkay"\t0.005\r
1020\tab;c\t0.005\r
1030\t"foo\tbar"\t21\r
1040\t"hello ""tom"\t3.14\r
1050\t"hey
106dad"\t7\r
1071\thi,ya\t-3\r
108}
109
110
111
112=== TEST 4: explicitly using \r\n as the row terminator
113--- http_config eval: $::http_config
114--- config
115    location /mysql {
116        drizzle_query "
117            select * from dogs order by name asc;
118        ";
119        drizzle_pass backend;
120        rds_csv_field_separator '\t';
121        rds_csv_row_terminator '\r\n';
122        rds_csv on;
123    }
124--- request
125POST /mysql
126sql=select%20*%20from%20cats;
127--- response_headers
128Content-Type: text/csv; header=presence
129--- response_body eval
130qq{male\tname\theight\r
1310\t"\rkay"\t0.005\r
1320\tab;c\t0.005\r
1330\t"foo\tbar"\t21\r
1340\t"hello ""tom"\t3.14\r
1350\t"hey
136dad"\t7\r
1371\thi,ya\t-3\r
138}
139
140
141
142=== TEST 5: using \n as the row terminator
143--- http_config eval: $::http_config
144--- config
145    location /mysql {
146        drizzle_query "
147            select * from dogs order by name asc;
148        ";
149        drizzle_pass backend;
150        rds_csv_field_separator '\t';
151        rds_csv_row_terminator '\n';
152        rds_csv on;
153    }
154--- request
155POST /mysql
156sql=select%20*%20from%20cats;
157--- response_headers
158Content-Type: text/csv; header=presence
159--- response_body eval
160qq{male\tname\theight
1610\t"\rkay"\t0.005
1620\tab;c\t0.005
1630\t"foo\tbar"\t21
1640\t"hello ""tom"\t3.14
1650\t"hey
166dad"\t7
1671\thi,ya\t-3
168}
169
170
171
172=== TEST 6: explicitly field name header on
173--- http_config eval: $::http_config
174--- config
175    location /mysql {
176        drizzle_query "
177            select * from dogs order by name asc;
178        ";
179        drizzle_pass backend;
180        rds_csv_field_separator '\t';
181        rds_csv_row_terminator '\n';
182        rds_csv_field_name_header on;
183        rds_csv on;
184    }
185--- request
186POST /mysql
187sql=select%20*%20from%20cats;
188--- response_headers
189Content-Type: text/csv; header=presence
190--- response_body eval
191qq{male\tname\theight
1920\t"\rkay"\t0.005
1930\tab;c\t0.005
1940\t"foo\tbar"\t21
1950\t"hello ""tom"\t3.14
1960\t"hey
197dad"\t7
1981\thi,ya\t-3
199}
200
201
202
203=== TEST 7: explicitly field name header off
204--- http_config eval: $::http_config
205--- config
206    location /mysql {
207        drizzle_query "
208            select * from dogs order by name asc;
209        ";
210        drizzle_pass backend;
211        rds_csv_field_separator '\t';
212        rds_csv_row_terminator '\n';
213        rds_csv_field_name_header off;
214        rds_csv on;
215    }
216--- request
217POST /mysql
218sql=select%20*%20from%20cats;
219--- response_headers
220Content-Type: text/csv; header=absence
221--- response_body eval
222qq{0\t"\rkay"\t0.005
2230\tab;c\t0.005
2240\t"foo\tbar"\t21
2250\t"hello ""tom"\t3.14
2260\t"hey
227dad"\t7
2281\thi,ya\t-3
229}
230
231
232
233=== TEST 8: the "charset" directive does not affect us
234--- http_config eval: $::http_config
235--- config
236    location /mysql {
237        charset "gbk";
238        drizzle_query "
239            select * from dogs order by name asc;
240        ";
241        drizzle_pass backend;
242        rds_csv_field_separator '\t';
243        rds_csv_row_terminator '\n';
244        rds_csv_field_name_header off;
245        rds_csv on;
246    }
247--- request
248POST /mysql
249sql=select%20*%20from%20cats;
250--- response_headers
251Content-Type: text/csv; header=absence
252--- response_body eval
253qq{0\t"\rkay"\t0.005
2540\tab;c\t0.005
2550\t"foo\tbar"\t21
2560\t"hello ""tom"\t3.14
2570\t"hey
258dad"\t7
2591\thi,ya\t-3
260}
261
262
263
264=== TEST 9: set content type
265--- http_config eval: $::http_config
266--- config
267    location /mysql {
268        drizzle_query "
269            select * from dogs order by name asc;
270        ";
271        drizzle_pass backend;
272        rds_csv_field_separator '\t';
273        rds_csv_row_terminator '\n';
274        rds_csv_field_name_header off;
275        rds_csv_content_type "text/tab-separated-values";
276        rds_csv on;
277    }
278--- request
279GET /mysql
280--- response_headers
281Content-Type: text/tab-separated-values
282--- response_body eval
283qq{0\t"\rkay"\t0.005
2840\tab;c\t0.005
2850\t"foo\tbar"\t21
2860\t"hello ""tom"\t3.14
2870\t"hey
288dad"\t7
2891\thi,ya\t-3
290}
291
292
293
294=== TEST 10: set content type (bigger buffer size)
295--- http_config eval: $::http_config
296--- config
297    location /mysql {
298        drizzle_query "
299            select * from dogs order by name asc;
300        ";
301        drizzle_pass backend;
302        rds_csv_field_separator '\t';
303        rds_csv_row_terminator '\n';
304        rds_csv_field_name_header off;
305        rds_csv_content_type "text/tab-separated-values";
306        rds_csv on;
307        rds_csv_buffer_size 8k;
308    }
309--- request
310GET /mysql
311--- response_headers
312Content-Type: text/tab-separated-values
313--- response_body eval
314qq{0\t"\rkay"\t0.005
3150\tab;c\t0.005
3160\t"foo\tbar"\t21
3170\t"hello ""tom"\t3.14
3180\t"hey
319dad"\t7
3201\thi,ya\t-3
321}
322
323
324
325=== TEST 11: escaping column names
326--- http_config eval: $::http_config
327--- config
328    location /mysql {
329        drizzle_query "
330            select `\"name\"`, height from birds order by height limit 1;
331        ";
332        drizzle_pass backend;
333        rds_csv on;
334        rds_csv_row_terminator "\n";
335    }
336--- request
337GET /mysql
338--- response_headers
339Content-Type: text/csv; header=presence
340--- response_body
341"""name""",height
342"hi,ya",-3
343
344