|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +use t::APISIX 'no_plan'; |
| 18 | + |
| 19 | +repeat_each(1); |
| 20 | +no_long_string(); |
| 21 | +no_root_location(); |
| 22 | +no_shuffle(); |
| 23 | + |
| 24 | +add_block_preprocessor(sub { |
| 25 | + my ($block) = @_; |
| 26 | + |
| 27 | + $block->set_value("stream_conf_enable", 1); |
| 28 | + |
| 29 | + if (!defined $block->extra_stream_config) { |
| 30 | + my $stream_config = <<_EOC_; |
| 31 | + server { |
| 32 | + listen unix:\$TEST_NGINX_HTML_DIR/nginx.sock; |
| 33 | +
|
| 34 | + content_by_lua_block { |
| 35 | + local ext = require("lib.ext-plugin") |
| 36 | + ext.go({}) |
| 37 | + } |
| 38 | + } |
| 39 | +
|
| 40 | +_EOC_ |
| 41 | + $block->set_value("extra_stream_config", $stream_config); |
| 42 | + } |
| 43 | + |
| 44 | + my $unix_socket_path = $ENV{"TEST_NGINX_HTML_DIR"} . "/nginx.sock"; |
| 45 | + my $cmd = $block->ext_plugin_cmd // "['sleep', '5s']"; |
| 46 | + my $extra_yaml_config = <<_EOC_; |
| 47 | +ext-plugin: |
| 48 | + path_for_test: $unix_socket_path |
| 49 | + cmd: $cmd |
| 50 | +_EOC_ |
| 51 | + |
| 52 | + $block->set_value("extra_yaml_config", $extra_yaml_config); |
| 53 | + |
| 54 | + if (!$block->request) { |
| 55 | + $block->set_value("request", "GET /t"); |
| 56 | + } |
| 57 | + |
| 58 | + if (!$block->error_log) { |
| 59 | + $block->set_value("no_error_log", "[error]\n[alert]"); |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +run_tests; |
| 64 | + |
| 65 | +__DATA__ |
| 66 | +
|
| 67 | +=== TEST 1: add route |
| 68 | +--- config |
| 69 | + location /t { |
| 70 | + content_by_lua_block { |
| 71 | + local json = require("toolkit.json") |
| 72 | + local t = require("lib.test_admin") |
| 73 | +
|
| 74 | + local code, message, res = t.test('/apisix/admin/routes/1', |
| 75 | + ngx.HTTP_PUT, |
| 76 | + [[{ |
| 77 | + "uri": "/hello", |
| 78 | + "plugins": { |
| 79 | + "ext-plugin-pre-req": { |
| 80 | + } |
| 81 | + }, |
| 82 | + "upstream": { |
| 83 | + "nodes": { |
| 84 | + "127.0.0.1:1980": 1 |
| 85 | + }, |
| 86 | + "type": "roundrobin" |
| 87 | + } |
| 88 | + }]] |
| 89 | + ) |
| 90 | +
|
| 91 | + if code >= 300 then |
| 92 | + ngx.status = code |
| 93 | + ngx.say(message) |
| 94 | + return |
| 95 | + end |
| 96 | +
|
| 97 | + ngx.say(message) |
| 98 | + } |
| 99 | + } |
| 100 | +--- response_body |
| 101 | +passed |
| 102 | +
|
| 103 | +
|
| 104 | +
|
| 105 | +=== TEST 2: var |
| 106 | +--- request |
| 107 | +GET /hello?x= |
| 108 | +--- extra_stream_config |
| 109 | + server { |
| 110 | + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; |
| 111 | +
|
| 112 | + content_by_lua_block { |
| 113 | + local ext = require("lib.ext-plugin") |
| 114 | + local actions = { |
| 115 | + {type = "var", name = "server_addr", result = "127.0.0.1"}, |
| 116 | + {type = "var", name = "remote_addr", result = "127.0.0.1"}, |
| 117 | + {type = "var", name = "route_id", result = "1"}, |
| 118 | + {type = "var", name = "arg_x", result = ""}, |
| 119 | + } |
| 120 | + ext.go({extra_info = actions, stop = true}) |
| 121 | + } |
| 122 | + } |
| 123 | +--- error_code: 405 |
| 124 | +--- grep_error_log eval |
| 125 | +qr/send extra info req successfully/ |
| 126 | +--- grep_error_log_out |
| 127 | +send extra info req successfully |
| 128 | +send extra info req successfully |
| 129 | +send extra info req successfully |
| 130 | +send extra info req successfully |
| 131 | +
|
| 132 | +
|
| 133 | +
|
| 134 | +=== TEST 3: ask nonexistent var |
| 135 | +--- request |
| 136 | +GET /hello |
| 137 | +--- more_headers |
| 138 | +X-Change: foo |
| 139 | +X-Delete: foo |
| 140 | +--- extra_stream_config |
| 141 | + server { |
| 142 | + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; |
| 143 | +
|
| 144 | + content_by_lua_block { |
| 145 | + local ext = require("lib.ext-plugin") |
| 146 | + local actions = { |
| 147 | + {type = "var", name = "erver_addr"}, |
| 148 | + } |
| 149 | + ext.go({extra_info = actions, rewrite = true}) |
| 150 | + } |
| 151 | + } |
| 152 | +--- response_body |
| 153 | +uri: /uri |
| 154 | +host: localhost |
| 155 | +x-add: bar |
| 156 | +x-change: bar |
| 157 | +x-real-ip: 127.0.0.1 |
| 158 | +--- grep_error_log eval |
| 159 | +qr/send extra info req successfully/ |
| 160 | +--- grep_error_log_out |
| 161 | +send extra info req successfully |
| 162 | +
|
| 163 | +
|
| 164 | +
|
| 165 | +=== TEST 4: network is down in the middle |
| 166 | +--- request |
| 167 | +GET /hello |
| 168 | +--- extra_stream_config |
| 169 | + server { |
| 170 | + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; |
| 171 | +
|
| 172 | + content_by_lua_block { |
| 173 | + local ext = require("lib.ext-plugin") |
| 174 | + local actions = { |
| 175 | + {type = "var", name = "server_addr", result = "127.0.0.1"}, |
| 176 | + {type = "closed"}, |
| 177 | + } |
| 178 | + ext.go({extra_info = actions, stop = true}) |
| 179 | + } |
| 180 | + } |
| 181 | +--- error_code: 503 |
| 182 | +--- error_log |
| 183 | +failed to receive RPC_HTTP_REQ_CALL: closed |
0 commit comments