Skip to content

Commit

Permalink
Merge pull request #19 from vearne/fix/lua
Browse files Browse the repository at this point in the history
Fix/lua
  • Loading branch information
vearne authored Jul 11, 2024
2 parents 4927d47 + ea46338 commit 365f83a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
14 changes: 13 additions & 1 deletion internal/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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"])
}
}

Expand Down Expand Up @@ -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"])
}
}

Expand Down
29 changes: 29 additions & 0 deletions internal/rule/http_lua_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
version = "v0.1.1"
version = "v0.1.3"
)

func main() {
Expand Down

0 comments on commit 365f83a

Please sign in to comment.