|
| 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 | +worker_connections(1024); |
| 20 | +repeat_each(1); |
| 21 | +no_long_string(); |
| 22 | +no_root_location(); |
| 23 | + |
| 24 | +add_block_preprocessor(sub { |
| 25 | + my ($block) = @_; |
| 26 | + |
| 27 | + if (!$block->request) { |
| 28 | + $block->set_value("request", "GET /t"); |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +run_tests; |
| 33 | + |
| 34 | +__DATA__ |
| 35 | +
|
| 36 | +=== TEST 1: check config with algorithm ksuid |
| 37 | +--- config |
| 38 | + location /t { |
| 39 | + content_by_lua_block { |
| 40 | + local t = require("lib.test_admin").test |
| 41 | + local code, body = t('/apisix/admin/routes/1', |
| 42 | + ngx.HTTP_PUT, |
| 43 | + [[{ |
| 44 | + "plugins": { |
| 45 | + "request-id": { |
| 46 | + "algorithm": "ksuid" |
| 47 | + } |
| 48 | + }, |
| 49 | + "upstream": { |
| 50 | + "nodes": { |
| 51 | + "127.0.0.1:1982": 1 |
| 52 | + }, |
| 53 | + "type": "roundrobin" |
| 54 | + }, |
| 55 | + "uri": "/opentracing" |
| 56 | + }]] |
| 57 | + ) |
| 58 | + if code >= 300 then |
| 59 | + ngx.status = code |
| 60 | + end |
| 61 | + ngx.say(body) |
| 62 | + } |
| 63 | + } |
| 64 | +--- response_body |
| 65 | +passed |
| 66 | +
|
| 67 | +
|
| 68 | +
|
| 69 | +=== TEST 2: hit |
| 70 | +--- request |
| 71 | +GET /opentracing |
| 72 | +--- error_log |
| 73 | +X-Request-Id |
| 74 | +--- no_error_log |
| 75 | +[error] |
| 76 | +
|
| 77 | +
|
| 78 | +
|
| 79 | +=== TEST 3: add plugin with algorithm ksuid |
| 80 | +--- config |
| 81 | + location /t { |
| 82 | + content_by_lua_block { |
| 83 | + local t = require("lib.test_admin").test |
| 84 | + local http = require "resty.http" |
| 85 | + local v = {} |
| 86 | + local ids = {} |
| 87 | + local code, body = t('/apisix/admin/routes/1', |
| 88 | + ngx.HTTP_PUT, |
| 89 | + [[{ |
| 90 | + "plugins": { |
| 91 | + "request-id": { |
| 92 | + "algorithm": "ksuid" |
| 93 | + } |
| 94 | + }, |
| 95 | + "upstream": { |
| 96 | + "nodes": { |
| 97 | + "127.0.0.1:1982": 1 |
| 98 | + }, |
| 99 | + "type": "roundrobin" |
| 100 | + }, |
| 101 | + "uri": "/opentracing" |
| 102 | + }]] |
| 103 | + ) |
| 104 | + if code >= 300 then |
| 105 | + ngx.say("algorithm ksuid is error") |
| 106 | + end |
| 107 | + for i = 1, 180 do |
| 108 | + local th = assert(ngx.thread.spawn(function() |
| 109 | + local httpc = http.new() |
| 110 | + local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/opentracing" |
| 111 | + local res, err = httpc:request_uri(uri, |
| 112 | + { |
| 113 | + method = "GET", |
| 114 | + headers = { |
| 115 | + ["Content-Type"] = "application/json", |
| 116 | + } |
| 117 | + } |
| 118 | + ) |
| 119 | + if not res then |
| 120 | + ngx.log(ngx.ERR, err) |
| 121 | + return |
| 122 | + end |
| 123 | + local id = res.headers["X-Request-Id"] |
| 124 | + if not id then |
| 125 | + return -- ignore if the data is not synced yet. |
| 126 | + end |
| 127 | + if #id ~= 27 then |
| 128 | + ngx.say(id) |
| 129 | + ngx.say("incorrect length for id") |
| 130 | + return |
| 131 | + end |
| 132 | + local start, en = string.find(id, '[a-zA-Z0-9]*') |
| 133 | + if start ~= 1 or en ~= 27 then |
| 134 | + ngx.say("incorrect char set for id") |
| 135 | + ngx.say(id) |
| 136 | + return |
| 137 | + end |
| 138 | + if ids[id] == true then |
| 139 | + ngx.say("ids not unique") |
| 140 | + return |
| 141 | + end |
| 142 | + ids[id] = true |
| 143 | + end, i)) |
| 144 | + table.insert(v, th) |
| 145 | + end |
| 146 | + for i, th in ipairs(v) do |
| 147 | + ngx.thread.wait(th) |
| 148 | + end |
| 149 | + ngx.say("true") |
| 150 | + } |
| 151 | + } |
| 152 | +--- wait: 5 |
| 153 | +--- response_body |
| 154 | +true |
0 commit comments