|
| 1 | +use Test::Nginx::Socket; |
| 2 | +repeat_each(2); |
| 3 | + |
| 4 | +$ENV{TEST_NGINX_NXSOCK} ||= html_dir(); |
| 5 | +plan tests => repeat_each() * blocks() * 3 + 4; |
| 6 | + |
| 7 | +run_tests(); |
| 8 | + |
| 9 | +__DATA__ |
| 10 | + |
| 11 | +=== TEST 1: session cookie is returned |
| 12 | +--- http_config |
| 13 | + init_by_lua_block { |
| 14 | + require("resty.session").init({ |
| 15 | + storage = "cookie", |
| 16 | + }) |
| 17 | + } |
| 18 | +--- config |
| 19 | +location = /test { |
| 20 | + content_by_lua_block { |
| 21 | + local session = require "resty.session".new() |
| 22 | + local ok = session:save() |
| 23 | + if ok then |
| 24 | + ngx.say("yay") |
| 25 | + end |
| 26 | + } |
| 27 | +} |
| 28 | +
|
| 29 | +--- request |
| 30 | +GET /test |
| 31 | +--- response_body |
| 32 | +yay |
| 33 | +--- response_headers_like |
| 34 | +Set-Cookie: .*session=.+;.* |
| 35 | +--- error_code: 200 |
| 36 | +--- no_error_log |
| 37 | +[error] |
| 38 | + |
| 39 | + |
| 40 | +=== TEST 2: remember cookie is returned when remember=true |
| 41 | +--- http_config |
| 42 | + init_by_lua_block { |
| 43 | + require("resty.session").init({ |
| 44 | + remember = true, |
| 45 | + storage = "cookie", |
| 46 | + }) |
| 47 | + } |
| 48 | +--- config |
| 49 | +location = /test { |
| 50 | + content_by_lua_block { |
| 51 | + local session = require "resty.session".new() |
| 52 | + local ok = session:save() |
| 53 | + |
| 54 | + if ok then |
| 55 | + ngx.say("yay") |
| 56 | + end |
| 57 | + } |
| 58 | +} |
| 59 | +
|
| 60 | +--- request |
| 61 | +GET /test |
| 62 | +--- response_body |
| 63 | +yay |
| 64 | +--- response_headers_like |
| 65 | +Set-Cookie: .*remember=.+;.* |
| 66 | +--- error_code: 200 |
| 67 | +--- no_error_log |
| 68 | +[error] |
| 69 | + |
| 70 | + |
| 71 | +=== TEST 3: session.open() opens a session from a valid cookie and data is |
| 72 | +extracted correctly |
| 73 | +--- http_config |
| 74 | + init_by_lua_block { |
| 75 | + require("resty.session").init({ |
| 76 | + secret = "RaJKp8UQW1", |
| 77 | + storage = "cookie", |
| 78 | + audience = "my_application", |
| 79 | + idling_timeout = 0, |
| 80 | + rolling_timeout = 0, |
| 81 | + absolute_timeout = 0, |
| 82 | + }) |
| 83 | + } |
| 84 | +--- config |
| 85 | +location = /test { |
| 86 | + content_by_lua_block { |
| 87 | + local session = require "resty.session".open() |
| 88 | + local sub = session:get_subject() |
| 89 | + local aud = session:get_audience() |
| 90 | + local quote = session:get("quote") |
| 91 | + |
| 92 | + ngx.say(sub .. "|" .. aud .. "|" .. quote) |
| 93 | + } |
| 94 | +} |
| 95 | +
|
| 96 | +--- request |
| 97 | +GET /test |
| 98 | +--- more_headers |
| 99 | +Cookie: session=AQAAS3ZGU0k8tUKsWSci9Fb6PM5xbm469FlR5g_B5HWZ6KYGSOZjAAAAAABcAABTCuHjqpE7B6Ux7m4GCylZAAAAzcWnTvzG51whooR_4QQwDgGdMOOa5W7tG4JWiDFU3zuYLFzakWEi-y-ogrwTpnt24zQXP_uJK7r5lMPNzRSMJM9H1a_MIegzEMm-QSgVRaoZVJq3Oo; Path=/; SameSite=Lax; HttpOnly |
| 100 | +--- response_body |
| 101 | +Lua Fan|my_application|Lorem ipsum dolor sit amet |
| 102 | +--- error_code: 200 |
| 103 | +--- no_error_log |
| 104 | +[error] |
| 105 | +
|
| 106 | +
|
| 107 | +=== TEST 4: clear_request_cookie() clears session Cookie from request to |
| 108 | +upstream |
| 109 | +--- http_config |
| 110 | + init_by_lua_block { |
| 111 | + require("resty.session").init({ |
| 112 | + secret = "RaJKp8UQW1", |
| 113 | + storage = "cookie", |
| 114 | + audience = "my_application", |
| 115 | + idling_timeout = 0, |
| 116 | + rolling_timeout = 0, |
| 117 | + absolute_timeout = 0, |
| 118 | + }) |
| 119 | + } |
| 120 | +
|
| 121 | + server { |
| 122 | + listen unix:/$TEST_NGINX_NXSOCK/nginx.sock; |
| 123 | +
|
| 124 | + location /t { |
| 125 | + content_by_lua_block { |
| 126 | + local headers = ngx.req.get_headers() |
| 127 | + ngx.say("session_cookie: [", tostring(headers["Cookie"]), "]") |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +
|
| 132 | +--- config |
| 133 | +location = /test { |
| 134 | + access_by_lua_block { |
| 135 | + local session = require "resty.session".open() |
| 136 | + session:clear_request_cookie() |
| 137 | + } |
| 138 | + proxy_pass http://unix:/$TEST_NGINX_NXSOCK/nginx.sock; |
| 139 | +} |
| 140 | + |
| 141 | +--- request |
| 142 | +GET /test |
| 143 | +--- more_headers |
| 144 | +Cookie: session=AQAAS3ZGU0k8tUKsWSci9Fb6PM5xbm469FlR5g_B5HWZ6KYGSOZjAAAAAABcAABTCuHjqpE7B6Ux7m4GCylZAAAAzcWnTvzG51whooR_4QQwDgGdMOOa5W7tG4JWiDFU3zuYLFzakWEi-y-ogrwTpnt24zQXP_uJK7r5lMPNzRSMJM9H1a_MIegzEMm-QSgVRaoZVJq3Oo; Path=/; SameSite=Lax; HttpOnly |
| 145 | +--- response_body |
| 146 | +session_cookie: [Path=/; SameSite=Lax; HttpOnly] |
| 147 | +--- error_code: 200 |
| 148 | +--- no_error_log |
| 149 | +[error] |
0 commit comments