1
# vim:set ft= ts=4 sw=4 et fdm=marker:
3
use Test::Nginx::Socket::Lua;
12
#worker_connections(1024);
14
plan tests => repeat_each() * (blocks() * 2 + 2);
16
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
17
$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306;
19
our $LuaCpath = $ENV{LUA_CPATH} ||
20
'/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;';
31
access_by_lua "ngx.exit(403);ngx.say('hi')";
32
content_by_lua 'ngx.exit(ngx.OK)';
37
--- response_body_like: 403 Forbidden
44
access_by_lua "ngx.exit(404);ngx.say('hi');";
45
content_by_lua 'ngx.exit(ngx.OK)';
50
--- response_body_like: 404 Not Found
54
=== TEST 3: throw 404 after sending the header and partial body
57
access_by_lua "ngx.say('hi');ngx.exit(404);ngx.say(', you')";
58
content_by_lua 'ngx.exit(ngx.OK)';
67
attempt to set status 404 via ngx.exit after sending out the response status 200
71
=== TEST 4: working with ngx_auth_request (succeeded)
75
if ngx.var.user == 'agentzh' then
80
content_by_lua 'ngx.exit(ngx.OK)';
96
=== TEST 5: working with ngx_auth_request (failed)
101
if ngx.var.user == 'agentzh' then
112
--- response_body_like: 403 Forbidden
116
=== TEST 6: working with ngx_auth_request (simplest form, w/o ngx_memc)
119
lua_package_cpath '$::LuaCpath';
121
drizzle_server 127.0.0.1:\$TEST_NGINX_MYSQL_PORT protocol=mysql
122
dbname=ngx_test user=ngx_test password=ngx_test;
123
drizzle_keepalive max=300 mode=single overflow=ignore;
130
set $memc_key $arg_key;
131
set $memc_exptime $arg_exptime;
133
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
136
location /conv-uid-mysql {
139
set $key "conv-uid-$arg_uid";
141
#srcache_fetch GET /memc key=$key;
142
#srcache_store PUT /memc key=$key;
144
default_type 'application/json';
146
drizzle_query "select new_uid as uid from conv_uid where old_uid=$arg_uid";
147
drizzle_pass backend;
154
access_by_lua_file 'html/foo.lua';
156
echo "Logged in $uid";
160
local cjson = require('cjson');
161
local old_uid = ngx.var.uid
162
print('about to run sr')
163
local res = ngx.location.capture('/conv-uid-mysql?uid=' .. old_uid)
164
print('just have run sr' .. res.body)
165
if (res.status ~= ngx.HTTP_OK) then
166
-- ngx.exit(res.status)
168
res = cjson.decode(res.body)
169
if (not res or not res[1] or not res[1].uid or
170
not string.match(res[1].uid, '^%d+$')) then
171
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
173
ngx.var.uid = res[1].uid;
183
=== TEST 7: working with ngx_auth_request (simplest form)
186
lua_package_cpath '$::LuaCpath';
188
drizzle_server 127.0.0.1:\$TEST_NGINX_MYSQL_PORT protocol=mysql
189
dbname=ngx_test user=ngx_test password=ngx_test;
190
drizzle_keepalive max=300 mode=single overflow=ignore;
197
set $memc_key $arg_key;
198
set $memc_exptime $arg_exptime;
200
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
203
location /conv-uid-mysql {
206
set $key "conv-uid-$arg_uid";
208
#srcache_fetch GET /memc key=$key;
209
#srcache_store PUT /memc key=$key;
211
default_type 'application/json';
213
drizzle_query "select new_uid as uid from conv_uid where old_uid=$arg_uid";
214
drizzle_pass backend;
221
access_by_lua_file html/foo.lua;
223
echo "Logged in $uid";
227
local cjson = require('cjson');
228
local old_uid = ngx.var.uid
229
-- print('about to run sr')
230
local res = ngx.location.capture('/conv-uid-mysql?uid=' .. old_uid)
231
-- print('just have run sr' .. res.body)
232
if (res.status ~= ngx.HTTP_OK) then
235
res = cjson.decode(res.body)
236
if (not res or not res[1] or not res[1].uid or
237
not string.match(res[1].uid, '^%d+$')) then
238
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
240
ngx.var.uid = res[1].uid;
249
=== TEST 8: working with ngx_auth_request
252
lua_package_cpath '$::LuaCpath';
254
drizzle_server 127.0.0.1:\$TEST_NGINX_MYSQL_PORT protocol=mysql
255
dbname=ngx_test user=ngx_test password=ngx_test;
256
drizzle_keepalive max=300 mode=single overflow=ignore;
260
server 127.0.0.1:\$TEST_NGINX_MEMCACHED_PORT;
264
server 127.0.0.1:\$TEST_NGINX_MEMCACHED_PORT;
267
upstream_list memc_cluster memc_a memc_b;
273
set $memc_key $arg_key;
274
set $memc_exptime $arg_exptime;
276
set_hashed_upstream $backend memc_cluster $arg_key;
280
location /conv-uid-mysql {
283
set $key "conv-uid-$arg_uid";
285
#srcache_fetch GET /memc key=$key;
286
#srcache_store PUT /memc key=$key;
288
default_type 'application/json';
290
drizzle_query "select new_uid as uid from conv_uid where old_uid=$arg_uid";
291
drizzle_pass backend;
298
access_by_lua_file html/foo.lua;
300
echo "Logged in $uid";
304
local cjson = require('cjson');
305
local old_uid = ngx.var.uid
306
-- print('about to run sr')
307
local res = ngx.location.capture('/conv-uid-mysql?uid=' .. old_uid)
308
-- print('just have run sr' .. res.body)
309
if (res.status ~= ngx.HTTP_OK) then
312
res = cjson.decode(res.body)
313
if (not res or not res[1] or not res[1].uid or
314
not string.match(res[1].uid, '^%d+$')) then
315
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
317
ngx.var.uid = res[1].uid;
326
=== TEST 9: working with ngx_auth_request
329
drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql
330
dbname=ngx_test user=ngx_test password=ngx_test;
331
drizzle_keepalive max=300 mode=single overflow=ignore;
335
server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
339
#upstream_list memc_cluster memc_a memc_b;
345
set $memc_key $arg_key;
346
set $memc_exptime $arg_exptime;
348
#set_hashed_upstream $backend memc_cluster $arg_key;
352
location /conv-mysql {
355
set $key "conv-uri-$query_string";
357
#srcache_fetch GET /memc key=$key;
358
#srcache_store PUT /memc key=$key;
360
default_type 'application/json';
362
set_quote_sql_str $seo_uri $query_string;
363
drizzle_query "select url from my_url_map where seo_url=$seo_uri";
364
drizzle_pass backend;
371
access_by_lua_file 'html/foo.lua';
372
content_by_lua 'ngx.exit(ngx.OK)';
377
auth_request /conv-uid;
379
echo_exec /jump $my_uri;
384
rewrite ^ $query_string? redirect;
388
local cjson = require('cjson');
389
local seo_uri = ngx.var.my_uri
390
-- print('about to run sr')
391
local res = ngx.location.capture('/conv-mysql?' .. seo_uri)
392
if (res.status ~= ngx.HTTP_OK) then
395
res = cjson.decode(res.body)
396
if (not res or not res[1] or not res[1].url) then
397
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
399
ngx.var.my_uri = res[1].url;
403
--- response_body_like: 302
406
Location: http://localhost:$ServerPort/foo/bar
414
access_by_lua "ngx.say('Hi'); ngx.eof(); ngx.exit(0);ngx.say('world')";
415
content_by_lua 'ngx.exit(ngx.OK)';
425
=== TEST 11: throw ngx.OK does *not* skip other later phase handlers
428
access_by_lua "ngx.exit(ngx.OK)";
439
=== TEST 12: throw ngx.HTTP_OK *does* skip other later phase handlers (by inlined code)
442
access_by_lua "ngx.exit(ngx.HTTP_OK)";
452
=== TEST 13: throw ngx.HTTP_OK *does* skip other rewrite phase handlers (by inlined code + partial output)
455
rewrite_by_lua "ngx.say('hiya') ngx.exit(ngx.HTTP_OK)";
466
=== TEST 14: throw ngx.HTTP_OK *does* skip other later phase handlers (by file)
469
access_by_lua_file html/foo.lua;
475
ngx.exit(ngx.HTTP_OK)
482
=== TEST 15: throw ngx.HTTP_OK *does* skip other rewrite phase handlers (by file + partial output)
485
rewrite_by_lua_file html/foo.lua;
492
ngx.exit(ngx.HTTP_OK)
500
=== TEST 16: error page with custom body
508
ngx.status = ngx.HTTP_GONE
509
ngx.say("This is our own content")
510
-- to cause quit the whole request rather than the current phase handler
511
ngx.exit(ngx.HTTP_OK)
518
This is our own content
523
=== TEST 17: exit(404) after I/O
525
error_page 400 /400.html;
526
error_page 404 /404.html;
529
ngx.location.capture("/sleep")
530
ngx.exit(ngx.HTTP_NOT_FOUND)