From d8a1a6ef2491f69947a5569c8accb88f45a9b411 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 9 Oct 2021 15:35:11 +0800 Subject: [PATCH] feat: dump2JSON --- convert.go | 35 +++++++++++++++++++++++++++++++ convert_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 convert.go create mode 100644 convert_test.go diff --git a/convert.go b/convert.go new file mode 100644 index 00000000..94c06577 --- /dev/null +++ b/convert.go @@ -0,0 +1,35 @@ +package httpboomer + +import ( + "encoding/json" + "io/ioutil" + "log" + "path/filepath" +) + +func (tc *TestCase) toStruct() *TCase { + tcStruct := TCase{ + Config: tc.Config, + } + for _, step := range tc.TestSteps { + tcStruct.TestSteps = append(tcStruct.TestSteps, step.ToStruct()) + } + return &tcStruct +} + +func (tc *TestCase) dump2JSON(path string) error { + path, err := filepath.Abs(path) + if err != nil { + log.Printf("convert absolute path error: %v, path: %v", err, path) + return err + } + log.Printf("dump testcase to json path: %s", path) + tcStruct := tc.toStruct() + file, _ := json.MarshalIndent(tcStruct, "", " ") + err = ioutil.WriteFile(path, file, 0644) + if err != nil { + log.Printf("dump json path error: %v", err) + return err + } + return nil +} diff --git a/convert_test.go b/convert_test.go new file mode 100644 index 00000000..c21a3384 --- /dev/null +++ b/convert_test.go @@ -0,0 +1,56 @@ +package httpboomer + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDump2JSON(t *testing.T) { + testcase := &TestCase{ + Config: TConfig{ + Name: "demo with complex mechanisms", + BaseURL: "https://postman-echo.com", + Variables: map[string]interface{}{ // global level variables + "n": 5, + "a": 12.3, + "b": 3.45, + "varFoo1": "${gen_random_string($n)}", + "varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function + }, + }, + TestSteps: []IStep{ + Step("get with params"). + WithVariables(map[string]interface{}{ // step level variables + "n": 3, // inherit config level variables if not set in step level, a/varFoo1 + "b": 34.5, // override config level variable if existed, n/b/varFoo2 + "varFoo2": "${max($a, $b)}", // 34.5; override variable b and eval again + }). + GET("/get"). + WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}). // request with params + WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). // request with headers + Extract(). + WithJmesPath("body.args.foo1", "varFoo1"). // extract variable with jmespath + Validate(). + AssertEqual("status_code", 200, "check response status code"). // validate response status code + AssertStartsWith("headers.\"Content-Type\"", "application/json", ""). // validate response header + AssertLengthEqual("body.args.foo1", 5, "check args foo1"). // validate response body with jmespath + AssertLengthEqual("$varFoo1", 5, "check args foo1"). // assert with extracted variable from current step + AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string + Step("post json data"). + POST("/post"). + WithJSON(map[string]interface{}{ + "foo1": "$varFoo1", // reference former extracted variable + "foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here + }). + Validate(). + AssertEqual("status_code", 200, "check status code"). + AssertLengthEqual("body.json.foo1", 5, "check args foo1"). + AssertEqual("body.json.foo2", 12.3, "check args foo2"), + }, + } + err := testcase.dump2JSON("test.json") + if !assert.NoError(t, err) { + t.Fail() + } +}