Skip to content

Commit 498d46d

Browse files
authored
test: abnormal input for header API (#74)
1 parent 6cb5448 commit 498d46d

File tree

3 files changed

+158
-2
lines changed

3 files changed

+158
-2
lines changed

t/http_req_header.t

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,48 @@ location /t {
389389
qr/get request scheme: \S+/
390390
--- grep_error_log_out
391391
get request scheme: http,
392+
393+
394+
395+
=== TEST 21: get header, abnormal
396+
--- config
397+
location /t {
398+
content_by_lua_block {
399+
local json = require("cjson")
400+
local wasm = require("resty.proxy-wasm")
401+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
402+
local conf = {action = 'req_hdr_get_abnormal'}
403+
for _, e in ipairs({
404+
""
405+
}) do
406+
conf.header = e
407+
local ctx = assert(wasm.on_configure(plugin, json.encode(conf)))
408+
assert(wasm.on_http_request_headers(ctx))
409+
end
410+
}
411+
}
412+
--- more_headers
413+
X-API: foo
414+
--- error_log
415+
error status returned by host: not found
416+
417+
418+
419+
=== TEST 22: set header, abnormal
420+
--- config
421+
location /t {
422+
content_by_lua_block {
423+
local json = require("cjson")
424+
local wasm = require("resty.proxy-wasm")
425+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
426+
local conf = {action = 'req_hdr_set_abnormal'}
427+
for _, e in ipairs({
428+
{"", ""}
429+
}) do
430+
conf.header = e[1]
431+
conf.value = e[2]
432+
local ctx = assert(wasm.on_configure(plugin, json.encode(conf)))
433+
assert(wasm.on_http_request_headers(ctx))
434+
end
435+
}
436+
}

t/http_resp_header.t

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,48 @@ qr/get response header \S+ \S+/
253253
get response header content-type text/plain,
254254
get response header content-length 0,
255255
get response header connection keep-alive,
256+
257+
258+
259+
=== TEST 14: get header, abnormal
260+
--- config
261+
location /t {
262+
return 200;
263+
header_filter_by_lua_block {
264+
local json = require("cjson")
265+
local wasm = require("resty.proxy-wasm")
266+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
267+
local conf = {action = 'resp_hdr_get_abnormal'}
268+
for _, e in ipairs({
269+
""
270+
}) do
271+
conf.header = e
272+
local ctx = assert(wasm.on_configure(plugin, json.encode(conf)))
273+
assert(wasm.on_http_response_headers(ctx))
274+
end
275+
}
276+
}
277+
--- error_log
278+
error status returned by host: not found
279+
280+
281+
282+
=== TEST 15: set header, abnormal
283+
--- config
284+
location /t {
285+
return 200;
286+
header_filter_by_lua_block {
287+
local json = require("cjson")
288+
local wasm = require("resty.proxy-wasm")
289+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
290+
local conf = {action = 'resp_hdr_set_abnormal'}
291+
for _, e in ipairs({
292+
{"", ""}
293+
}) do
294+
conf.header = e[1]
295+
conf.value = e[2]
296+
local ctx = assert(wasm.on_configure(plugin, json.encode(conf)))
297+
assert(wasm.on_http_response_headers(ctx))
298+
end
299+
}
300+
}

t/testdata/http_header/main.go

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
55
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
6+
"github.com/valyala/fastjson"
67
)
78

89
func main() {
@@ -37,7 +38,22 @@ func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t
3738
return types.ActionContinue
3839
}
3940

40-
action := string(data)
41+
var action string
42+
var conf *fastjson.Value
43+
if data[0] == '{' {
44+
var p fastjson.Parser
45+
conf, err = p.ParseBytes(data)
46+
if err != nil {
47+
proxywasm.LogErrorf("erorr decoding plugin configuration: %v", err)
48+
return types.ActionContinue
49+
}
50+
51+
action = string(conf.GetStringBytes("action"))
52+
53+
} else {
54+
action = string(data)
55+
}
56+
4157
switch action {
4258
case "req_hdr_get_all":
4359
hdrs, err := proxywasm.GetHttpRequestHeaders()
@@ -66,6 +82,15 @@ func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t
6682
}
6783
proxywasm.LogWarnf("get request header: %v", res)
6884

85+
case "req_hdr_get_abnormal":
86+
hdr := string(conf.GetStringBytes("header"))
87+
res, err := proxywasm.GetHttpRequestHeader(hdr)
88+
if err != nil {
89+
proxywasm.LogErrorf("error get request header: %v", err)
90+
return types.ActionContinue
91+
}
92+
proxywasm.LogWarnf("get request header: %v", res)
93+
6994
case "req_hdr_set":
7095
proxywasm.ReplaceHttpRequestHeader("foo", "bar")
7196

@@ -75,6 +100,15 @@ func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t
75100
case "req_hdr_del":
76101
proxywasm.RemoveHttpRequestHeader("foo")
77102

103+
case "req_hdr_set_abnormal":
104+
hdr := string(conf.GetStringBytes("header"))
105+
v := string(conf.GetStringBytes("value"))
106+
err := proxywasm.ReplaceHttpRequestHeader(hdr, v)
107+
if err != nil {
108+
proxywasm.LogErrorf("error set request header: %v", err)
109+
return types.ActionContinue
110+
}
111+
78112
case "req_path_get":
79113
res, err := proxywasm.GetHttpRequestHeader(":path")
80114
if err != nil {
@@ -110,7 +144,22 @@ func (ctx *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool)
110144
return types.ActionContinue
111145
}
112146

113-
action := string(data)
147+
var action string
148+
var conf *fastjson.Value
149+
if data[0] == '{' {
150+
var p fastjson.Parser
151+
conf, err = p.ParseBytes(data)
152+
if err != nil {
153+
proxywasm.LogErrorf("erorr decoding plugin configuration: %v", err)
154+
return types.ActionContinue
155+
}
156+
157+
action = string(conf.GetStringBytes("action"))
158+
159+
} else {
160+
action = string(data)
161+
}
162+
114163
if action == "resp_hdr_get_all" {
115164
hdrs, err := proxywasm.GetHttpResponseHeaders()
116165
if err != nil {
@@ -140,6 +189,15 @@ func (ctx *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool)
140189
proxywasm.RemoveHttpResponseHeader("add")
141190
case "resp_hdr_del_miss":
142191
proxywasm.RemoveHttpResponseHeader("aa")
192+
case "resp_hdr_set_abnormal":
193+
hdr := string(conf.GetStringBytes("header"))
194+
v := string(conf.GetStringBytes("value"))
195+
err := proxywasm.ReplaceHttpResponseHeader(hdr, v)
196+
if err != nil {
197+
proxywasm.LogErrorf("error set request header: %v", err)
198+
return types.ActionContinue
199+
}
200+
143201
case "resp_hdr_get":
144202
res, err := proxywasm.GetHttpResponseHeader("add")
145203
if err != nil {
@@ -162,6 +220,14 @@ func (ctx *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool)
162220
return types.ActionContinue
163221
}
164222
proxywasm.LogWarnf("get response header: %v", res)
223+
case "resp_hdr_get_abnormal":
224+
hdr := string(conf.GetStringBytes("header"))
225+
res, err := proxywasm.GetHttpResponseHeader(hdr)
226+
if err != nil {
227+
proxywasm.LogErrorf("error get response header: %v", err)
228+
return types.ActionContinue
229+
}
230+
proxywasm.LogWarnf("get response header: %v", res)
165231
}
166232

167233
return types.ActionContinue

0 commit comments

Comments
 (0)