/
openresty.t
647 lines (517 loc) · 19.1 KB
/
openresty.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# vi:filetype=
use lib 'lib';
use Test::Nginx::Socket skip_all => 'not working at all (missing data)';
#repeat_each(100);
repeat_each(2);
worker_connections(128);
workers(1);
#master_on;
log_level('warn');
plan tests => repeat_each() * 3 * blocks();
$ENV{TEST_NGINX_MYSQL_HOST} ||= '127.0.0.1';
$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306;
our $http_config = <<'_EOC_';
upstream backend {
drizzle_server $TEST_NGINX_MYSQL_HOST:$TEST_NGINX_MYSQL_PORT protocol=mysql
dbname=ngx_test user=ngx_test password=ngx_test;
}
_EOC_
our $config = <<'_EOC_';
xss_get on;
xss_callback_arg _callback;
rds_json on;
location = /auth {
#internal;
default_type 'application/json';
#internal;
eval_subrequest_in_memory off;
eval $res {
set_quote_sql_str $user $arg_user;
set $sql 'select count(*) res from users where name=$user';
drizzle_query $sql;
drizzle_pass backend;
#echo $sql;
#echo hi;
rds_json on;
rds_json_content_type application/octet-stream;
}
#echo $res;
if ($res ~ '"res":1') {
echo pass;
break;
}
if ($res !~ '"res":1') {
return 403;
break;
}
}
# XXX we should implement these in the ngx_xss module
location @err500 { rds_json_ret 500 "Internal Server Error"; }
location @err404 { rds_json_ret 404 "Not Found"; }
location @err400 { rds_json_ret 400 "Bad Request"; }
location @err403 { rds_json_ret 403 "Forbidden"; }
location @err502 { rds_json_ret 502 "Bad Gateway"; }
location @err503 { rds_json_ret 503 "Service Unavailable"; }
error_page 500 = @err500;
error_page 404 = @err404;
error_page 403 = @err403;
error_page 400 = @err400;
error_page 502 = @err502;
error_page 503 = @err503;
error_page 504 507 = @err500;
location = '/=/view/PostsByMonth/~/~' {
if ($arg_year !~ '^\d{4}$') {
rds_json_ret 400 'Bad "year" argument';
}
if ($arg_month !~ '^\d{1,2}$') {
rds_json_ret 400 'Bad "month" argument';
}
drizzle_query
"select id, title, day(created) as day
from posts
where year(created) = $arg_year and month(created) = $arg_month
order by created asc";
drizzle_pass backend;
}
location = '/=/view/RecentComments/~/~' {
set $offset $arg_offset;
set_if_empty $offset 0;
set $limit $arg_limit;
set_if_empty $limit 10;
if ($offset !~ '^\d+$') {
rds_json_ret 400 'Bad "offset" argument';
}
if ($limit !~ '^\d{1,2}$') {
rds_json_ret 400 'Bad "limit" argument';
}
drizzle_query
"select comments.id as id, post, sender, title
from posts, comments
where post = posts.id
order by comments.id desc
limit $offset, $limit";
drizzle_pass backend;
}
location = '/=/view/RecentPosts/~/~' {
set $offset $arg_offset;
set_if_empty $offset 0;
set $limit $arg_limit;
set_if_empty $limit 10;
if ($offset !~ '^\d+$') {
rds_json_ret 400 'Bad "offset" argument';
}
if ($limit !~ '^\d{1,2}$') {
rds_json_ret 400 'Bad "limit" argument';
}
drizzle_query
"select id, title
from posts
order by id desc
limit $offset, $limit";
drizzle_pass backend;
}
location = '/=/view/PrevNextPost/~/~' {
if ($arg_current !~ '^\d+$') {
rds_json_ret 400 'Bad "current" argument';
}
drizzle_query
"(select id, title
from posts
where id < $arg_current
order by id desc
limit 1)
union
(select id, title
from posts
where id > $arg_current
order by id asc
limit 1)";
drizzle_pass backend;
}
location = '/=/view/RowCount/~/~' {
if ($arg_model = 'Post') {
drizzle_query "select count(*) as count from posts";
drizzle_pass backend;
}
if ($arg_model = 'Comment') {
drizzle_query "select count(*) as count from comments";
drizzle_pass backend;
}
rds_json_ret 400 'Bad "model" argument';
}
location = '/=/view/PostCountByMonths/~/~' {
set $offset $arg_offset;
set_if_empty $offset 0;
set $limit $arg_limit;
set_if_empty $limit 10;
if ($offset !~ '^\d+$') {
rds_json_ret 400 'Bad "offset" argument';
}
if ($limit !~ '^\d{1,2}$') {
rds_json_ret 400 'Bad "limit" argument';
}
drizzle_query
"select date_format(created, '%Y-%m-01') `year_month`, count(*) count
from posts
group by `year_month`
order by `year_month` desc
limit $offset, $limit";
drizzle_pass backend;
}
location = '/=/view/FullPostsByMonth/~/~' {
set $count $arg_count;
set_if_empty $count 40;
if ($arg_year !~ '^(?:19|20)\d{2}$') {
rds_json_ret 400 'Bad "year" argument';
}
if ($arg_month !~ '^\d{1,2}$') {
rds_json_ret 400 'Bad "month" argument';
}
if ($arg_count !~ '^\d+$') {
rds_json_ret 400 'Bad "count" argument';
}
drizzle_query
"select * from posts
where year(created) = $arg_year and month(created) = $arg_month
order by id desc
limit $count";
drizzle_pass backend;
}
location = '/=/view/PrevNextArchive/~/~' {
if ($arg_now !~ '^\d{4}-\d{1,2}(?:-\d{1,2})?$') {
rds_json_ret 400 'Bad "now" argument';
}
if ($arg_month !~ '^\d{1,2}$') {
rds_json_ret 400 'Bad "month" argument';
}
drizzle_query
"(select 'next' as id, month(created) as month, year(created) as year
from posts
where created > $arg_now and month(created) <> $arg_month
order by created asc
limit 1)
union
(select 'prev' as id, month(created) as month, year(created) as year
from posts
where created < $arg_now and month(created) <> $arg_month
order by created desc
limit 1)";
drizzle_pass backend;
}
location = '/=/batch/GetSidebar/~/~' {
if ($arg_year !~ '^(?:19|20)\d{2}$') {
rds_json_ret 400 'Bad "year" argument';
}
if ($arg_month !~ '^\d{1,2}$') {
rds_json_ret 400 'Bad "month" argument';
}
default_type 'application/json';
echo '[';
echo_location_async '/=/view/PostsByMonth/~/~' "year=$arg_year&month=$arg_month";
echo ',';
echo_location_async '/=/view/RecentPosts/~/~' "offset=0&limit=6";
echo ',';
echo_location_async '/=/view/RecentComments/~/~' "offset=0&limit=6";
echo ',';
echo_location_async '/=/view/PostCountByMonths/~/~' "offset=0&limit=12";
echo ']';
}
location = '/=/batch/GetFullPost/~/~' {
if ($arg_id !~ '^\d+$') {
rds_json_ret 400 'Bad "id" argument';
}
default_type 'application/json';
echo '[';
echo_location_async "/=/model/Post/id/$arg_id";
echo ',';
echo_location_async "/=/view/PrevNextPost/~/~" "current=$arg_id";
echo ',';
echo_location_async "/=/model/Comment/post/$arg_id" "_order_by=id:desc";
echo ']';
}
location ~* '^/=/model/Post/id/(.*)$' {
set $id $1;
if ($id !~ '^\d+$') {
rds_json_ret 400 'Bad "id" value';
}
drizzle_query "select * from posts where id = $id";
drizzle_pass backend;
}
location ~* '^/=/model/Comment/post/(.*)$' {
set $post $1;
if ($post !~ '^\d+$') {
rds_json_ret 400 'Bad "post" value';
}
drizzle_query "select * from comments where post = $post";
drizzle_pass backend;
}
location = '/=/model/Post/~/~' {
if ($arg__offset !~ '^\d+$') {
rds_json_ret 400 'Bad "_offset" argument';
}
if ($arg__limit !~ '^\d{1,2}$') {
rds_json_ret 400 'Bad "_limit" argument';
}
if ($arg__order_by !~ '^([A-Za-z]\w*)%3A(desc|asc)$') {
rds_json_ret 400 'Bad "_order_by" argument';
}
set $col $1;
set $order $2;
drizzle_query
"select *
from posts
order by `$col` $order
limit $arg__offset, $arg__limit";
drizzle_pass backend;
}
location = '/=/batch/NewComment/~/~' {
default_type 'application/json';
set_unescape_uri $sender $arg_sender;
set_unescape_uri $email $arg_email;
set_unescape_uri $url $arg_url;
set_unescape_uri $body $arg_body;
set_unescape_uri $post_id $arg_post_id;
if ($sender !~ '\S') {
rds_json_ret 400 "Bad \"sender\" argument";
}
if ($email !~ '^[-A-Za-z0-9_.]+@[-A-Za-z0-9_.]+$') {
rds_json_ret 400 "Bad \"email\" argument";
}
if ($url !~ '^(?:\s*|https?://\S+)$') {
rds_json_ret 400 "Bad \"url\" argument: $url";
}
if ($body ~ '^\s*$') {
rds_json_ret 400 "Bad \"body\" argument";
}
if ($post_id !~ '^[1-9]\d*$') {
rds_json_ret 400 "Bad \"post_id\" argument";
}
set_quote_sql_str $sender;
set_quote_sql_str $email;
set_quote_sql_str $url;
set_quote_sql_str $body;
# XXX these operations should be put into a
# single transaction
echo '[';
echo_location '/=/action/RunSQL/~/~'
"insert into comments (sender, email, url, body, post)
values($sender, $email, $url, $body, $post_id)";
echo ',';
echo_location '/=/action/RunSQL/~/~'
"update posts
set comments = comments + 1
where id = $post_id";
echo ']';
}
location = '/=/action/RunSQL/~/~' {
internal;
drizzle_query $query_string;
drizzle_pass backend;
}
_EOC_
no_long_string();
run_tests();
#no_diff();
__DATA__
=== TEST 1: PostsByMonth view (no month arg)
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/view/PostsByMonth/~/~?_callback=foo
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
foo({"errcode":400,"errstr":"Bad \"month\" argument"});
=== TEST 2: PostsByMonth view (bad month)
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/view/PostsByMonth/~/~?month=1234&_callback=foo
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
foo({"errcode":400,"errstr":"Bad \"month\" argument"});
=== TEST 3: PostsByMonth view (emtpy result)
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/view/PostsByMonth/~/~?year=1984&month=2&_callback=bar
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
bar([]);
=== TEST 4: PostsByMonth view (non-emtpy result)
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/view/PostsByMonth/~/~?year=2009&month=10&_callback=foo
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
foo([{"id":114,"title":"Hacking on the Nginx echo module","day":15}]);
=== TEST 5: PostsByMonth view (non-emtpy result)
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/view/PostsByMonth/~/~?year=2009&month=12&_callback=foo
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
foo([{"id":117,"title":"Major updates to ngx_chunkin: lots of bug fixes and beginning of keep-alive support","day":4},{"id":118,"title":"ngx_memc: an extended version of ngx_memcached that supports set, add, delete, and many more commands","day":6},{"id":119,"title":"Test::Nginx::LWP and Test::Nginx::Socket are now on CPAN","day":8}]);
--- timeout: 90
=== TEST 6: GetSideBar
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/batch/GetSidebar/~/~?year=2009&month=10&_callback=foo
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
foo([
[{"id":114,"title":"Hacking on the Nginx echo module","day":15}],
[{"id":119,"title":"Test::Nginx::LWP and Test::Nginx::Socket are now on CPAN"},{"id":118,"title":"ngx_memc: an extended version of ngx_memcached that supports set, add, delete, and many more commands"},{"id":117,"title":"Major updates to ngx_chunkin: lots of bug fixes and beginning of keep-alive support"},{"id":116,"title":"The \"headers more\" module: scripting input and output filters in your Nginx config file"},{"id":115,"title":"The \"chunkin\" module: Experimental chunked input support for Nginx"},{"id":114,"title":"Hacking on the Nginx echo module"}],
[{"id":179,"post":101,"sender":"agentzh","title":"生活搜基于 Firefox 3.1 的 List Hunter 集群"},{"id":178,"post":101,"sender":"Winter","title":"生活搜基于 Firefox 3.1 的 List Hunter 集群"},{"id":177,"post":100,"sender":"Mountain","title":"漂在北京"},{"id":176,"post":106,"sender":"agentzh","title":"Text::SmartLinks: The Perl 6 love for Perl 5"},{"id":175,"post":106,"sender":"gosber","title":"Text::SmartLinks: The Perl 6 love for Perl 5"},{"id":174,"post":105,"sender":"cnangel","title":"SSH::Batch: Treating clusters as maths sets and intervals"}],
[{"year_month":"2009-12-01","count":3},{"year_month":"2009-11-01","count":2},{"year_month":"2009-10-01","count":1},{"year_month":"2009-09-01","count":5},{"year_month":"2009-05-01","count":2},{"year_month":"2009-04-01","count":3},{"year_month":"2009-02-01","count":2},{"year_month":"2008-12-01","count":3},{"year_month":"2008-11-01","count":2},{"year_month":"2008-10-01","count":2},{"year_month":"2008-09-01","count":4},{"year_month":"2008-08-01","count":2}]]
);
=== TEST 7: GetFullPost
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/batch/GetFullPost/~/~?id=116&_user=agentzh.Public&_callback=OpenResty.callbackMap%5B1264354204389%5D
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
--- SKIP
=== TEST 8: RowCount
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/view/RowCount/~/~?model=Post&_callback=foo
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
foo([{"count":118}]);
=== TEST 9: GetFullPost bug
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/model/Comment/post/67
--- response_headers
Content-Type: application/json
--- response_body_like: laser
--- error_code: 200
=== TEST 10: more field data error
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/model/Post/~/~?_limit=5&_order_by=id%3Adesc&_offset=100
--- response_headers
Content-Type: application/json
--- response_body_like: 测试
--- error_code: 200
--- SKIP
=== TEST 11: default arguments
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/view/RecentComments/~/~
--- response_headers
Content-Type: application/json
--- response_body chop
[{"id":179,"post":101,"sender":"agentzh","title":"生活搜基于 Firefox 3.1 的 List Hunter 集群"},{"id":178,"post":101,"sender":"Winter","title":"生活搜基于 Firefox 3.1 的 List Hunter 集群"},{"id":177,"post":100,"sender":"Mountain","title":"漂在北京"},{"id":176,"post":106,"sender":"agentzh","title":"Text::SmartLinks: The Perl 6 love for Perl 5"},{"id":175,"post":106,"sender":"gosber","title":"Text::SmartLinks: The Perl 6 love for Perl 5"},{"id":174,"post":105,"sender":"cnangel","title":"SSH::Batch: Treating clusters as maths sets and intervals"},{"id":173,"post":106,"sender":"cnangel","title":"Text::SmartLinks: The Perl 6 love for Perl 5"},{"id":172,"post":104,"sender":"agentzh","title":"My VDOM.pm & WebKit Cluster Talk at the April Meeting of Beijing Perl Workshop"},{"id":171,"post":104,"sender":"kindy","title":"My VDOM.pm & WebKit Cluster Talk at the April Meeting of Beijing Perl Workshop"},{"id":170,"post":104,"sender":"cnangel","title":"My VDOM.pm & WebKit Cluster Talk at the April Meeting of Beijing Perl Workshop"}]
--- error_code: 200
--- timeout: 3
=== TEST 12: post a comment with empty body
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/batch/NewComment/~/~?sender=agentzh&email=agentzh@gmail.com&body=&post_id=3
--- response_headers
Content-Type: application/json
--- response_body chop
{"errcode":400,"errstr":"Bad \"body\" argument"}
--- error_code: 200
=== TEST 13: post a comment with valid gmail
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/batch/NewComment/~/~?sender=agentzh&email=agentzh%40gmail.com&body=hi&post_id=
--- response_headers
Content-Type: application/json
--- response_body chop
{"errcode":400,"errstr":"Bad \"post_id\" argument"}
--- error_code: 200
=== TEST 14: try to run the internal location
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /=/action/RunSQL/~/~?select
--- response_headers
Content-Type: application/json
--- response_body chop
{"errcode":404,"errstr":"Not Found"}
--- error_code: 200
=== TEST 15: auth
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /auth?user=agentzh
--- response_headers
Content-Type: application/json
--- response_body
pass
--- error_code: 200
--- SKIP
=== TEST 16: auth
init db:
create table users (
id serial primary key,
name text not null,
password text not null
);
insert into users
(name, password)
values ('agentzh', 'some_pass');
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /auth?user=john
--- response_headers
Content-Type: application/json
--- response_body chop
{"errcode":403,"errstr":"Forbidden"}
--- error_code: 200
--- SKIP
=== TEST 17: auth
db init:
create table users (
id serial primary key,
name text not null,
password text not null
);
insert into users
(name, password)
values ('agentzh', 'some_pass');
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /test
--- response_headers
Content-Type: application/json
--- response_body
pass
--- error_code: 200
--- SKIP
=== TEST 18: default arguments (small pagesize)
--- http_config eval: $::http_config
--- config eval
"rds_json_buffer_size 1;
$::config"
--- request
GET /=/view/RecentComments/~/~
--- response_headers
Content-Type: application/json
--- response_body chop
[{"id":179,"post":101,"sender":"agentzh","title":"生活搜基于 Firefox 3.1 的 List Hunter 集群"},{"id":178,"post":101,"sender":"Winter","title":"生活搜基于 Firefox 3.1 的 List Hunter 集群"},{"id":177,"post":100,"sender":"Mountain","title":"漂在北京"},{"id":176,"post":106,"sender":"agentzh","title":"Text::SmartLinks: The Perl 6 love for Perl 5"},{"id":175,"post":106,"sender":"gosber","title":"Text::SmartLinks: The Perl 6 love for Perl 5"},{"id":174,"post":105,"sender":"cnangel","title":"SSH::Batch: Treating clusters as maths sets and intervals"},{"id":173,"post":106,"sender":"cnangel","title":"Text::SmartLinks: The Perl 6 love for Perl 5"},{"id":172,"post":104,"sender":"agentzh","title":"My VDOM.pm & WebKit Cluster Talk at the April Meeting of Beijing Perl Workshop"},{"id":171,"post":104,"sender":"kindy","title":"My VDOM.pm & WebKit Cluster Talk at the April Meeting of Beijing Perl Workshop"},{"id":170,"post":104,"sender":"cnangel","title":"My VDOM.pm & WebKit Cluster Talk at the April Meeting of Beijing Perl Workshop"}]
--- error_code: 200
--- timeout: 3