1# vim:set ft= ts=4 sw=4 et fdm=marker:
2use Test::Nginx::Socket::Lua;
3
4#worker_connections(1014);
5#master_process_enabled(1);
6#log_level('warn');
7#no_nginx_manager();
8
9#repeat_each(1);
10repeat_each(2);
11
12plan tests => repeat_each() * (blocks() * 2 + 1);
13
14#no_diff();
15no_long_string();
16run_tests();
17
18__DATA__
19
20=== TEST 1: basic test passing
21--- config
22    location /lua {
23        lua_need_request_body on;
24        client_max_body_size 100k;
25        client_body_buffer_size 100k;
26
27        access_by_lua '
28            -- check the client IP addr is in our black list
29            if ngx.var.remote_addr == "132.5.72.3" then
30                ngx.exit(ngx.HTTP_FORBIDDEN)
31            end
32
33            -- check if the request body contains bad words
34            if ngx.var.request_body and string.match(ngx.var.request_body, "fuck") then
35                return ngx.redirect("/terms_of_use.html")
36            end
37
38            -- tests passed
39        ';
40
41        echo Logged in;
42    }
43--- request
44GET /lua
45--- response_body
46Logged in
47
48
49
50=== TEST 2: bad words in request body
51--- config
52    location /lua {
53        lua_need_request_body on;
54        client_max_body_size 100k;
55        client_body_buffer_size 100k;
56
57        access_by_lua '
58            -- check the client IP addr is in our black list
59            if ngx.var.remote_addr == "132.5.72.3" then
60                ngx.exit(ngx.HTTP_FORBIDDEN)
61            end
62
63            -- check if the request body contains bad words
64            if ngx.var.request_body and string.match(ngx.var.request_body, "fuck") then
65                return ngx.redirect("/terms_of_use.html")
66            end
67
68            -- tests passed
69        ';
70
71        echo Logged in;
72    }
73--- request
74POST /lua
75He fucks himself!
76--- response_body_like: 302 Found
77--- response_headers_like
78Location: /terms_of_use\.html
79--- error_code: 302
80
81
82
83=== TEST 3: client IP
84--- config
85    location /lua {
86        lua_need_request_body on;
87        client_max_body_size 100k;
88        client_body_buffer_size 100k;
89
90        access_by_lua '
91            -- check the client IP addr is in our black list
92            if ngx.var.remote_addr == "127.0.0.1" then
93                ngx.exit(ngx.HTTP_FORBIDDEN)
94            end
95
96            -- check if the request body contains bad words
97            if ngx.var.request_body and string.match(ngx.var.request_body, "fuck") then
98                return ngx.redirect("/terms_of_use.html")
99            end
100
101            -- tests passed
102        ';
103
104        echo Logged in;
105    }
106--- request
107GET /lua
108--- response_body_like: 403 Forbidden
109--- error_code: 403
110