diff --git a/internal/resource/resource.go b/internal/resource/resource.go index 3da47e2..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" @@ -111,7 +112,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 +120,16 @@ 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) + default: + return fmt.Errorf("unknow http-VerifyRule:%v", r["name"]) } } @@ -195,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/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..8d36b61 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( ) const ( - version = "v0.1.1" + version = "v0.1.3" ) func main() {