From 86a4c420ea343ceee962041cf8feb21e6af1b904 Mon Sep 17 00:00:00 2001 From: vearne Date: Thu, 11 Jul 2024 15:10:47 +0800 Subject: [PATCH 1/2] add HttpLuaRule --- internal/resource/resource.go | 9 ++++++++- internal/rule/http_lua_rule_test.go | 29 +++++++++++++++++++++++++++++ main.go | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/internal/resource/resource.go b/internal/resource/resource.go index 3da47e2..0634aff 100644 --- a/internal/resource/resource.go +++ b/internal/resource/resource.go @@ -111,7 +111,6 @@ func ParseConfigFile(filePath string) error { return err } c.VerifyRules = append(c.VerifyRules, &item) - case "HttpBodyAtLeastOneRule": var item rule.HttpBodyAtLeastOneRule err = json.Unmarshal(b, &item) @@ -120,6 +119,14 @@ func ParseConfigFile(filePath string) error { return err } c.VerifyRules = append(c.VerifyRules, &item) + case "HttpLuaRule": + var item rule.HttpLuaRule + err = json.Unmarshal(b, &item) + if err != nil { + slog.Error("parse rule[HttpLuaRule], %v", err) + return err + } + c.VerifyRules = append(c.VerifyRules, &item) } } diff --git a/internal/rule/http_lua_rule_test.go b/internal/rule/http_lua_rule_test.go index 322fe59..6ae9820 100644 --- a/internal/rule/http_lua_rule_test.go +++ b/internal/rule/http_lua_rule_test.go @@ -41,3 +41,32 @@ end rule := HttpLuaRule{LuaStr: luaStr} assert.True(t, rule.Verify(&resp)) } + +func TestHttpLuaRule2(t *testing.T) { + luaStr := ` +function verify(r) + local json = require "json"; + local person = json.decode(r:body()); + return person.age == 1 and person.name == "John"; +end +` + var resp resty.Response + resp.SetBody([]byte(`{"age": 10, "name": "John"}`)) + rule := HttpLuaRule{LuaStr: luaStr} + assert.False(t, rule.Verify(&resp)) +} + +func TestHttpLuaRule3(t *testing.T) { + luaStr := ` +function verify(r) + local json = require "json"; + local person = json.decode(r:body()); + print("age:", person.age) + return person.age == 1 and person.name == "John"; +end +` + var resp resty.Response + resp.SetBody([]byte(`{"age": 1, "name": "Lily"}`)) + rule := HttpLuaRule{LuaStr: luaStr} + assert.False(t, rule.Verify(&resp)) +} diff --git a/main.go b/main.go index ce4aa53..59f7e66 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( ) const ( - version = "v0.1.1" + version = "v0.1.2" ) func main() { From ea46338d43273ba7b72423dd812710388c35913c Mon Sep 17 00:00:00 2001 From: vearne Date: Thu, 11 Jul 2024 15:16:04 +0800 Subject: [PATCH 2/2] add HttpLuaRule --- internal/resource/resource.go | 5 +++++ main.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/resource/resource.go b/internal/resource/resource.go index 0634aff..0f8555d 100644 --- a/internal/resource/resource.go +++ b/internal/resource/resource.go @@ -3,6 +3,7 @@ package resource import ( "bufio" "encoding/json" + "fmt" "github.com/go-resty/resty/v2" "github.com/vearne/autotest/internal/config" "github.com/vearne/autotest/internal/model" @@ -127,6 +128,8 @@ func ParseConfigFile(filePath string) error { return err } c.VerifyRules = append(c.VerifyRules, &item) + default: + return fmt.Errorf("unknow http-VerifyRule:%v", r["name"]) } } @@ -202,6 +205,8 @@ func ParseConfigFile(filePath string) error { return err } c.VerifyRules = append(c.VerifyRules, &item) + default: + return fmt.Errorf("unknow Grpc-VerifyRule:%v", r["name"]) } } diff --git a/main.go b/main.go index 59f7e66..8d36b61 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( ) const ( - version = "v0.1.2" + version = "v0.1.3" ) func main() {