1# vim:set ft= ts=4 sw=4 et:
2
3use t::Test;
4
5repeat_each(2);
6
7plan tests => repeat_each() * (3 * blocks());
8
9log_level 'warn';
10
11run_tests();
12
13__DATA__
14
15=== TEST 1: github issue #108: ngx.location.capture + redis.set_keepalive
16--- http_only
17--- http_config eval: $::GlobalConfig
18--- config
19    location /r1 {
20        default_type text/html;
21        set $port $TEST_NGINX_REDIS_PORT;
22        #lua_code_cache off;
23        lua_need_request_body on;
24        content_by_lua_file html/r1.lua;
25    }
26
27    location /r2 {
28        default_type text/html;
29        set $port $TEST_NGINX_REDIS_PORT;
30        #lua_code_cache off;
31        lua_need_request_body on;
32        content_by_lua_file html/r2.lua;
33    }
34
35    location /anyurl {
36        internal;
37        proxy_pass http://127.0.0.1:$server_port/dummy;
38    }
39
40    location = /dummy {
41        echo dummy;
42    }
43--- user_files
44>>> r1.lua
45local redis = require "resty.redis"
46local red = redis:new()
47local ok, err = red:connect("127.0.0.1", ngx.var.port)
48if not ok then
49    ngx.say("failed to connect: ", err)
50    return
51end
52local ok, err = red:flushall()
53if not ok then
54    ngx.say("failed to flushall: ", err)
55    return
56end
57ok, err = red:set_keepalive()
58if not ok then
59    ngx.say("failed to set keepalive: ", err)
60    return
61end
62local http_ress = ngx.location.capture("/r2") -- 1
63ngx.say("ok")
64
65>>> r2.lua
66local redis = require "resty.redis"
67local red = redis:new()
68local ok, err = red:connect("127.0.0.1", ngx.var.port) --2
69if not ok then
70    ngx.say("failed to connect: ", err)
71    return
72end
73local res = ngx.location.capture("/anyurl") --3
74--- request
75    GET /r1
76--- response_body
77ok
78--- no_error_log
79[error]
80
81
82
83=== TEST 2: exit(404) after I/O (ngx_lua github issue #110
84https://github.com/chaoslawful/lua-nginx-module/issues/110
85--- http_only
86--- http_config eval: $::GlobalConfig
87--- config
88    error_page 400 /400.html;
89    error_page 404 /404.html;
90    location /foo {
91        access_by_lua '
92            local redis = require "resty.redis"
93            local red = redis:new()
94
95            red:set_timeout(2000) -- 2 sec
96
97            -- ngx.log(ngx.ERR, "hello");
98
99            -- or connect to a unix domain socket file listened
100            -- by a redis server:
101            --     local ok, err = red:connect("unix:/path/to/redis.sock")
102
103            local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
104            if not ok then
105                ngx.log(ngx.ERR, "failed to connect: ", err)
106                return
107            end
108
109            res, err = red:set("dog", "an animal")
110            if not res then
111                ngx.log(ngx.ERR, "failed to set dog: ", err)
112                return
113            end
114
115            -- ngx.say("set dog: ", res)
116
117            local res, err = red:get("dog")
118            if err then
119                ngx.log(ngx.ERR, "failed to get dog: ", err)
120                return
121            end
122
123            if not res then
124                ngx.log(ngx.ERR, "dog not found.")
125                return
126            end
127
128            -- ngx.say("dog: ", res)
129
130            -- red:close()
131            local ok, err = red:set_keepalive(0, 100)
132            if not ok then
133                ngx.log(ngx.ERR, "failed to set keepalive: ", err)
134                return
135            end
136
137            ngx.exit(404)
138        ';
139        echo Hello;
140    }
141--- user_files
142>>> 400.html
143Bad request, dear...
144>>> 404.html
145Not found, dear...
146--- request
147    GET /foo
148--- response_body
149Not found, dear...
150--- error_code: 404
151--- no_error_log
152[error]
153
154
155
156=== TEST 3: set and get an empty string
157--- global_config eval: $::GlobalConfig
158--- server_config
159        content_by_lua '
160            local redis = require "resty.redis"
161            local red = redis:new()
162
163            red:set_timeout(1000) -- 1 sec
164
165            -- or connect to a unix domain socket file listened
166            -- by a redis server:
167            --     local ok, err = red:connect("unix:/path/to/redis.sock")
168
169            local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
170            if not ok then
171                ngx.say("failed to connect: ", err)
172                return
173            end
174
175            res, err = red:set("dog", "")
176            if not res then
177                ngx.say("failed to set dog: ", err)
178                return
179            end
180
181            ngx.say("set dog: ", res)
182
183            for i = 1, 2 do
184                local res, err = red:get("dog")
185                if err then
186                    ngx.say("failed to get dog: ", err)
187                    return
188                end
189
190                if not res then
191                    ngx.say("dog not found.")
192                    return
193                end
194
195                ngx.say("dog: ", res)
196            end
197
198            red:close()
199        ';
200--- response_body
201set dog: OK
202dog:
203dog:
204--- no_error_log
205[error]
206
207
208
209=== TEST 4: ngx.exec() after red:get()
210--- http_only
211--- http_config eval: $::GlobalConfig
212--- config
213    location /t {
214        content_by_lua '
215            local redis = require "resty.redis"
216            local red = redis:new()
217
218            red:set_timeout(1000) -- 1 sec
219
220            local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
221            if not ok then
222                ngx.say("failed to connect: ", err)
223                return
224            end
225
226            local res, err = red:get("dog")
227            if err then
228                ngx.say("failed to get dog: ", err)
229                return
230            end
231
232            ngx.exec("/hello")
233        ';
234    }
235
236    location = /hello {
237        echo hello world;
238    }
239
240--- request
241    GET /t
242--- response_body
243hello world
244--- no_error_log
245[error]
246