|
| 1 | +# Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +use t::WASM 'no_plan'; |
| 16 | + |
| 17 | +run_tests(); |
| 18 | + |
| 19 | +__DATA__ |
| 20 | + |
| 21 | +=== TEST 1: sanity |
| 22 | +--- config |
| 23 | +location /t { |
| 24 | + content_by_lua_block { |
| 25 | + local wasm = require("resty.proxy-wasm") |
| 26 | + local plugin = assert(wasm.load("plugin", "t/testdata/http_body/main.go.wasm")) |
| 27 | + ngx.ctx.ctx = assert(wasm.on_configure(plugin, '{}')) |
| 28 | + |
| 29 | + ngx.print("1234") |
| 30 | + ngx.print("12345") |
| 31 | + } |
| 32 | + body_filter_by_lua_block { |
| 33 | + local wasm = require("resty.proxy-wasm") |
| 34 | + ngx.log(ngx.WARN, ngx.arg[1], " ", ngx.arg[2]) |
| 35 | + local ctx = ngx.ctx.ctx |
| 36 | + assert(wasm.on_http_response_body(ctx)) |
| 37 | + } |
| 38 | +} |
| 39 | +--- grep_error_log eval |
| 40 | +qr/body size \d+, end of stream \w+/ |
| 41 | +--- grep_error_log_out |
| 42 | +body size 4, end of stream false |
| 43 | +body size 5, end of stream false |
| 44 | +body size 0, end of stream true |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +=== TEST 2: flush |
| 49 | +--- config |
| 50 | +location /t { |
| 51 | + content_by_lua_block { |
| 52 | + local wasm = require("resty.proxy-wasm") |
| 53 | + local plugin = assert(wasm.load("plugin", "t/testdata/http_body/main.go.wasm")) |
| 54 | + ngx.ctx.ctx = assert(wasm.on_configure(plugin, '{}')) |
| 55 | + |
| 56 | + ngx.print("1234") |
| 57 | + ngx.flush() |
| 58 | + ngx.print("12345") |
| 59 | + } |
| 60 | + body_filter_by_lua_block { |
| 61 | + local wasm = require("resty.proxy-wasm") |
| 62 | + local ctx = ngx.ctx.ctx |
| 63 | + assert(wasm.on_http_response_body(ctx)) |
| 64 | + } |
| 65 | +} |
| 66 | +--- grep_error_log eval |
| 67 | +qr/body size \d+, end of stream \w+/ |
| 68 | +--- grep_error_log_out |
| 69 | +body size 4, end of stream false |
| 70 | +body size 0, end of stream false |
| 71 | +body size 5, end of stream false |
| 72 | +body size 0, end of stream true |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +=== TEST 3: no body |
| 77 | +--- config |
| 78 | +location /t { |
| 79 | + content_by_lua_block { |
| 80 | + local wasm = require("resty.proxy-wasm") |
| 81 | + local plugin = assert(wasm.load("plugin", "t/testdata/http_body/main.go.wasm")) |
| 82 | + ngx.ctx.ctx = assert(wasm.on_configure(plugin, '{"action":"offset"}')) |
| 83 | + } |
| 84 | + body_filter_by_lua_block { |
| 85 | + local wasm = require("resty.proxy-wasm") |
| 86 | + local ctx = ngx.ctx.ctx |
| 87 | + assert(wasm.on_http_response_body(ctx)) |
| 88 | + } |
| 89 | +} |
| 90 | +--- grep_error_log eval |
| 91 | +qr/body size \d+, end of stream \w+/ |
| 92 | +--- grep_error_log_out |
| 93 | +body size 0, end of stream true |
| 94 | +--- error_log |
| 95 | +get body err: error status returned by host: not found |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +=== TEST 4: get body |
| 100 | +--- config |
| 101 | +location /t { |
| 102 | + return 200 "helloworld"; |
| 103 | + body_filter_by_lua_block { |
| 104 | + if not ngx.arg[2] then |
| 105 | + return |
| 106 | + end |
| 107 | + |
| 108 | + local json = require("cjson") |
| 109 | + local wasm = require("resty.proxy-wasm") |
| 110 | + local plugin = assert(wasm.load("plugin", "t/testdata/http_body/main.go.wasm")) |
| 111 | + local ctx = assert(wasm.on_configure(plugin, '{"action":"offset"}')) |
| 112 | + local conf = {action = "offset"} |
| 113 | + for _, e in ipairs({ |
| 114 | + {0, 2}, |
| 115 | + {1, 1}, |
| 116 | + {1, 2}, |
| 117 | + {0, 10}, |
| 118 | + {1, 9}, |
| 119 | + {8, 2}, |
| 120 | + {9, 1}, |
| 121 | + {1, 10}, |
| 122 | + }) do |
| 123 | + conf.start = e[1] |
| 124 | + conf.size = e[2] |
| 125 | + local ctx = assert(wasm.on_configure(plugin, json.encode(conf))) |
| 126 | + ngx.arg[1] = "helloworld" |
| 127 | + assert(wasm.on_http_response_body(ctx)) |
| 128 | + end |
| 129 | + } |
| 130 | +} |
| 131 | +--- grep_error_log eval |
| 132 | +qr/get body \[\w+\]/ |
| 133 | +--- grep_error_log_out |
| 134 | +get body [he] |
| 135 | +get body [e] |
| 136 | +get body [el] |
| 137 | +get body [helloworld] |
| 138 | +get body [elloworld] |
| 139 | +get body [ld] |
| 140 | +get body [d] |
| 141 | +get body [elloworld] |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +=== TEST 5: get body, bad cases |
| 146 | +--- config |
| 147 | +location /t { |
| 148 | + return 200 "helloworld"; |
| 149 | + body_filter_by_lua_block { |
| 150 | + if not ngx.arg[2] then |
| 151 | + return |
| 152 | + end |
| 153 | + |
| 154 | + local json = require("cjson") |
| 155 | + local wasm = require("resty.proxy-wasm") |
| 156 | + local plugin = assert(wasm.load("plugin", "t/testdata/http_body/main.go.wasm")) |
| 157 | + local ctx = assert(wasm.on_configure(plugin, '{"action":"offset"}')) |
| 158 | + local conf = {action = "offset"} |
| 159 | + for _, e in ipairs({ |
| 160 | + {-1, 2}, |
| 161 | + {1, 0}, |
| 162 | + {1, -1}, |
| 163 | + {10, 2}, |
| 164 | + }) do |
| 165 | + conf.start = e[1] |
| 166 | + conf.size = e[2] |
| 167 | + local ctx = assert(wasm.on_configure(plugin, json.encode(conf))) |
| 168 | + ngx.arg[1] = "helloworld" |
| 169 | + assert(wasm.on_http_response_body(ctx)) |
| 170 | + end |
| 171 | + } |
| 172 | +} |
| 173 | +--- error_log |
| 174 | +[error] |
| 175 | +--- grep_error_log eval |
| 176 | +qr/error status returned by host: bad argument/ |
| 177 | +--- grep_error_log_out eval |
| 178 | +"error status returned by host: bad argument\n" x 4 |
0 commit comments