|
| 1 | +use Test::Nginx::Socket::Lua::Stream; |
| 2 | + |
| 3 | +repeat_each(2); |
| 4 | + |
| 5 | +plan tests => repeat_each() * (blocks() * 3); |
| 6 | + |
| 7 | +add_block_preprocessor(sub { |
| 8 | + my $block = shift; |
| 9 | + |
| 10 | + if (!defined $block->no_error_log) { |
| 11 | + $block->set_value("no_error_log", "[error]"); |
| 12 | + } |
| 13 | +}); |
| 14 | + |
| 15 | +no_long_string(); |
| 16 | +run_tests(); |
| 17 | + |
| 18 | +__DATA__ |
| 19 | +
|
| 20 | +=== TEST 1: lua_load_resty_core is enabled by default |
| 21 | +--- stream_server_config |
| 22 | + content_by_lua_block { |
| 23 | + local loaded_resty_core = package.loaded["resty.core"] |
| 24 | + local resty_core = require "resty.core" |
| 25 | +
|
| 26 | + ngx.say("resty.core loaded: ", loaded_resty_core == resty_core) |
| 27 | + } |
| 28 | +--- stream_response |
| 29 | +resty.core loaded: true |
| 30 | +
|
| 31 | +
|
| 32 | +
|
| 33 | +=== TEST 2: lua_load_resty_core can be disabled |
| 34 | +--- stream_config |
| 35 | + lua_load_resty_core off; |
| 36 | +--- stream_server_config |
| 37 | + content_by_lua_block { |
| 38 | + local loaded_resty_core = package.loaded["resty.core"] |
| 39 | +
|
| 40 | + ngx.say("resty.core loaded: ", loaded_resty_core ~= nil) |
| 41 | + } |
| 42 | +--- stream_response |
| 43 | +resty.core loaded: false |
| 44 | +
|
| 45 | +
|
| 46 | +
|
| 47 | +=== TEST 3: lua_load_resty_core is effective when using lua_shared_dict |
| 48 | +--- stream_config |
| 49 | + lua_shared_dict dogs 128k; |
| 50 | +--- stream_server_config |
| 51 | + content_by_lua_block { |
| 52 | + local loaded_resty_core = package.loaded["resty.core"] |
| 53 | + local resty_core = require "resty.core" |
| 54 | +
|
| 55 | + ngx.say("resty.core loaded: ", loaded_resty_core == resty_core) |
| 56 | + } |
| 57 | +--- stream_response |
| 58 | +resty.core loaded: true |
| 59 | +
|
| 60 | +
|
| 61 | +
|
| 62 | +=== TEST 4: lua_load_resty_core 'on' in stream block and 'off' in http block |
| 63 | +--- http_config |
| 64 | + lua_load_resty_core off; |
| 65 | +--- config |
| 66 | + location = /t2 { |
| 67 | + content_by_lua_block { |
| 68 | + local loaded_resty_core = package.loaded["resty.core"] |
| 69 | +
|
| 70 | + local msg = "resty.core loaded in http: " .. tostring(loaded_resty_core ~= nil) |
| 71 | + ngx.header["Content-Length"] = #msg |
| 72 | + ngx.say(msg) |
| 73 | + } |
| 74 | + } |
| 75 | +--- stream_server_config |
| 76 | + content_by_lua_block { |
| 77 | + local loaded_resty_core = package.loaded["resty.core"] |
| 78 | + local resty_core = require "resty.core" |
| 79 | +
|
| 80 | + ngx.say("resty.core loaded in stream: ", loaded_resty_core == resty_core) |
| 81 | +
|
| 82 | + local sock = ngx.socket.tcp() |
| 83 | + local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_SERVER_PORT) |
| 84 | + if not ok then |
| 85 | + ngx.say("failed to connect: ", err) |
| 86 | + return |
| 87 | + end |
| 88 | +
|
| 89 | + local req = "GET /t2 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" |
| 90 | +
|
| 91 | + local bytes, err = sock:send(req) |
| 92 | + if not bytes then |
| 93 | + ngx.say("failed to send request: ", err) |
| 94 | + return |
| 95 | + end |
| 96 | +
|
| 97 | + local cl |
| 98 | +
|
| 99 | + while true do |
| 100 | + local line, err, part = sock:receive("*l") |
| 101 | + if err then |
| 102 | + ngx.say("failed to receive headers: ", err, " [", part, "]") |
| 103 | + break |
| 104 | + end |
| 105 | +
|
| 106 | + local k, v = line:match("([^:]*):%s*(.*)") |
| 107 | + if k == "Content-Length" then |
| 108 | + cl = v |
| 109 | + end |
| 110 | +
|
| 111 | + if line == "" then |
| 112 | + local body, err = sock:receive(cl) |
| 113 | + if err then |
| 114 | + ngx.say("failed to receive body: ", err) |
| 115 | + break |
| 116 | + end |
| 117 | +
|
| 118 | + ngx.say(body) |
| 119 | + break |
| 120 | + end |
| 121 | + end |
| 122 | +
|
| 123 | + ok, err = sock:close() |
| 124 | + } |
| 125 | +--- stream_response |
| 126 | +resty.core loaded in stream: true |
| 127 | +resty.core loaded in http: false |
| 128 | +
|
| 129 | +
|
| 130 | +
|
| 131 | +=== TEST 5: lua_load_resty_core 'off' in stream block and 'on' in http block |
| 132 | +--- config |
| 133 | + location = /t2 { |
| 134 | + content_by_lua_block { |
| 135 | + local loaded_resty_core = package.loaded["resty.core"] |
| 136 | + local resty_core = require "resty.core" |
| 137 | +
|
| 138 | + local msg = "resty.core loaded in http: " .. tostring(loaded_resty_core == resty_core) |
| 139 | + ngx.header["Content-Length"] = #msg |
| 140 | + ngx.say(msg) |
| 141 | + } |
| 142 | + } |
| 143 | +--- stream_config |
| 144 | + lua_load_resty_core off; |
| 145 | +--- stream_server_config |
| 146 | + content_by_lua_block { |
| 147 | + local loaded_resty_core = package.loaded["resty.core"] |
| 148 | +
|
| 149 | + ngx.say("resty.core loaded in stream: ", loaded_resty_core ~= nil) |
| 150 | +
|
| 151 | + local sock = ngx.socket.tcp() |
| 152 | + local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_SERVER_PORT) |
| 153 | + if not ok then |
| 154 | + ngx.say("failed to connect: ", err) |
| 155 | + return |
| 156 | + end |
| 157 | +
|
| 158 | + local req = "GET /t2 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" |
| 159 | +
|
| 160 | + local bytes, err = sock:send(req) |
| 161 | + if not bytes then |
| 162 | + ngx.say("failed to send request: ", err) |
| 163 | + return |
| 164 | + end |
| 165 | +
|
| 166 | + local cl |
| 167 | +
|
| 168 | + while true do |
| 169 | + local line, err, part = sock:receive("*l") |
| 170 | + if err then |
| 171 | + ngx.say("failed to receive headers: ", err, " [", part, "]") |
| 172 | + break |
| 173 | + end |
| 174 | +
|
| 175 | + local k, v = line:match("([^:]*):%s*(.*)") |
| 176 | + if k == "Content-Length" then |
| 177 | + cl = v |
| 178 | + end |
| 179 | +
|
| 180 | + if line == "" then |
| 181 | + local body, err = sock:receive(cl) |
| 182 | + if err then |
| 183 | + ngx.say("failed to receive body: ", err) |
| 184 | + break |
| 185 | + end |
| 186 | +
|
| 187 | + ngx.say(body) |
| 188 | + break |
| 189 | + end |
| 190 | + end |
| 191 | +
|
| 192 | + ok, err = sock:close() |
| 193 | + } |
| 194 | +--- stream_response |
| 195 | +resty.core loaded in stream: false |
| 196 | +resty.core loaded in http: true |
| 197 | +
|
| 198 | +
|
| 199 | +
|
| 200 | +=== TEST 6: lua_load_resty_core 'off' in stream block and 'off' in http block |
| 201 | +--- http_config |
| 202 | + lua_load_resty_core off; |
| 203 | +--- config |
| 204 | + location = /t2 { |
| 205 | + content_by_lua_block { |
| 206 | + local loaded_resty_core = package.loaded["resty.core"] |
| 207 | +
|
| 208 | + local msg = "resty.core loaded in http: " .. tostring(loaded_resty_core ~= nil) |
| 209 | + ngx.header["Content-Length"] = #msg |
| 210 | + ngx.say(msg) |
| 211 | + } |
| 212 | + } |
| 213 | +--- stream_config |
| 214 | + lua_load_resty_core off; |
| 215 | +--- stream_server_config |
| 216 | + content_by_lua_block { |
| 217 | + local loaded_resty_core = package.loaded["resty.core"] |
| 218 | +
|
| 219 | + ngx.say("resty.core loaded in stream: ", loaded_resty_core ~= nil) |
| 220 | +
|
| 221 | + local sock = ngx.socket.tcp() |
| 222 | + local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_SERVER_PORT) |
| 223 | + if not ok then |
| 224 | + ngx.say("failed to connect: ", err) |
| 225 | + return |
| 226 | + end |
| 227 | +
|
| 228 | + local req = "GET /t2 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" |
| 229 | +
|
| 230 | + local bytes, err = sock:send(req) |
| 231 | + if not bytes then |
| 232 | + ngx.say("failed to send request: ", err) |
| 233 | + return |
| 234 | + end |
| 235 | +
|
| 236 | + local cl |
| 237 | +
|
| 238 | + while true do |
| 239 | + local line, err, part = sock:receive("*l") |
| 240 | + if err then |
| 241 | + ngx.say("failed to receive headers: ", err, " [", part, "]") |
| 242 | + break |
| 243 | + end |
| 244 | +
|
| 245 | + local k, v = line:match("([^:]*):%s*(.*)") |
| 246 | + if k == "Content-Length" then |
| 247 | + cl = v |
| 248 | + end |
| 249 | +
|
| 250 | + if line == "" then |
| 251 | + local body, err = sock:receive(cl) |
| 252 | + if err then |
| 253 | + ngx.say("failed to receive body: ", err) |
| 254 | + break |
| 255 | + end |
| 256 | +
|
| 257 | + ngx.say(body) |
| 258 | + break |
| 259 | + end |
| 260 | + end |
| 261 | +
|
| 262 | + ok, err = sock:close() |
| 263 | + } |
| 264 | +--- stream_response |
| 265 | +resty.core loaded in stream: false |
| 266 | +resty.core loaded in http: false |
0 commit comments