Skip to content

Commit b3b18d8

Browse files
committed
feat: improve the HTTP request payload and expect
1 parent 506595a commit b3b18d8

File tree

12 files changed

+315
-40
lines changed

12 files changed

+315
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
bin/
2+
.idea/

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
This is a API testing tool.
2+
3+
## Feature
4+
* Response Body fields equation check
5+
* Response Body [eval](https://expr.medv.io/)
6+
* Output reference between TestCase
7+
8+
## Template
9+
The following fields are templated with [sprig](http://masterminds.github.io/sprig/):
10+
11+
* API
12+
* Request Body
13+
14+
## Limit
15+
* Only support to parse the response body when it's a map

cmd/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
import (
44
"github.com/linuxsuren/api-testing/pkg/exec"
@@ -11,7 +11,7 @@ type initOption struct {
1111
waitResource string
1212
}
1313

14-
func createInitCommand() (cmd *cobra.Command) {
14+
func CreateInitCommand() (cmd *cobra.Command) {
1515
opt := &initOption{}
1616
cmd = &cobra.Command{
1717
Use: "init",

cmd/root_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package main
1+
package cmd
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
atesting "github.com/linuxsuren/api-testing/pkg/testing"
69
)
7-
import atesting "github.com/linuxsuren/api-testing/pkg/testing"
810

911
func Test_setRelativeDir(t *testing.T) {
1012
type args struct {

cmd/run.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
package main
1+
package cmd
22

33
import (
4+
"path"
5+
"path/filepath"
6+
47
"github.com/linuxsuren/api-testing/pkg/runner"
58
"github.com/linuxsuren/api-testing/pkg/testing"
69
"github.com/spf13/cobra"
7-
"path"
8-
"path/filepath"
910
)
1011

1112
type runOption struct {
1213
pattern string
1314
}
1415

15-
func createRunCommand() (cmd *cobra.Command) {
16+
func CreateRunCommand() (cmd *cobra.Command) {
1617
opt := &runOption{}
1718
cmd = &cobra.Command{
1819
Use: "run",
@@ -21,26 +22,32 @@ func createRunCommand() (cmd *cobra.Command) {
2122

2223
// set flags
2324
flags := cmd.Flags()
24-
flags.StringVarP(&opt.pattern, "pattern", "p", "testcase-*.yaml",
25+
flags.StringVarP(&opt.pattern, "pattern", "p", "test-suite-*.yaml",
2526
"The file pattern which try to execute the test cases")
2627
return
2728
}
2829

2930
func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
3031
var files []string
32+
33+
ctx := map[string]interface{}{}
34+
3135
if files, err = filepath.Glob(o.pattern); err == nil {
3236
for i := range files {
3337
item := files[i]
3438

35-
var testcase *testing.TestCase
36-
if testcase, err = testing.Parse(item); err != nil {
39+
var testSuite *testing.TestSuite
40+
if testSuite, err = testing.Parse(item); err != nil {
3741
return
3842
}
3943

40-
setRelativeDir(item, testcase)
41-
42-
if err = runner.RunTestCase(testcase); err != nil {
43-
return
44+
for _, testCase := range testSuite.Items {
45+
setRelativeDir(item, &testCase)
46+
var output interface{}
47+
if output, err = runner.RunTestCase(&testCase, ctx); err != nil {
48+
return
49+
}
50+
ctx[testCase.Name] = output
4451
}
4552
}
4653
}

go.mod

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,28 @@ go 1.17
55
require (
66
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
77
github.com/spf13/cobra v1.4.0
8-
github.com/stretchr/testify v1.4.0
8+
github.com/stretchr/testify v1.8.2
99
gopkg.in/yaml.v2 v2.4.0
1010
)
1111

1212
require (
13+
github.com/Masterminds/goutils v1.1.1 // indirect
14+
github.com/Masterminds/semver/v3 v3.2.0 // indirect
15+
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
16+
github.com/antonmedv/expr v1.12.1 // indirect
1317
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/google/uuid v1.1.1 // indirect
19+
github.com/huandu/xstrings v1.3.3 // indirect
20+
github.com/imdario/mergo v0.3.11 // indirect
1421
github.com/inconshreveable/mousetrap v1.0.0 // indirect
22+
github.com/linuxsuren/unstructured v0.0.1 // indirect
23+
github.com/mitchellh/copystructure v1.0.0 // indirect
24+
github.com/mitchellh/reflectwalk v1.0.0 // indirect
1525
github.com/pmezard/go-difflib v1.0.0 // indirect
1626
github.com/sergi/go-diff v1.2.0 // indirect
27+
github.com/shopspring/decimal v1.2.0 // indirect
28+
github.com/spf13/cast v1.3.1 // indirect
1729
github.com/spf13/pflag v1.0.5 // indirect
30+
golang.org/x/crypto v0.3.0 // indirect
31+
gopkg.in/yaml.v3 v3.0.1 // indirect
1832
)

go.sum

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,98 @@
1+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
2+
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
3+
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
4+
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
5+
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
6+
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
17
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
28
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
9+
github.com/antonmedv/expr v1.12.1 h1:GTGrGN1kxxb+le0uQKaFRK8By4cvq1sleUCGE/U6hHg=
10+
github.com/antonmedv/expr v1.12.1/go.mod h1:FPC8iWArxls7axbVLsW+kpg1mz29A1b2M6jt+hZfDkU=
311
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
412
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
513
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
614
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
15+
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
16+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
17+
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
18+
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
19+
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
20+
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
721
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
822
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
923
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1024
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1125
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1226
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1327
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
28+
github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU=
29+
github.com/linuxsuren/unstructured v0.0.1/go.mod h1:KH6aTj+FegzGBzc1vS6mzZx3/duhTUTEVyW5sO7p4as=
30+
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
31+
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
32+
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
33+
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
1434
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1535
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1636
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
1737
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
1838
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
39+
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
40+
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
41+
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
42+
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
1943
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
2044
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
2145
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
2246
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
2347
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
48+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
49+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
50+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
2451
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
2552
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
53+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
54+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
55+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
56+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
57+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
58+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
59+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
60+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
61+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
62+
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
63+
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
64+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
65+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
66+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
67+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
68+
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
69+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
70+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
71+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
72+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
73+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
74+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
75+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
76+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
77+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
78+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
79+
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
80+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
81+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
82+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
83+
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
84+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
85+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
86+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
87+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2688
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2789
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
2890
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2991
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3092
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
93+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3194
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
3295
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
96+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
97+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
98+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/runner/simple.go

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ package runner
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"fmt"
6-
"github.com/andreyvit/diff"
7-
"github.com/linuxsuren/api-testing/pkg/exec"
8-
"github.com/linuxsuren/api-testing/pkg/testing"
97
"io"
108
"io/ioutil"
119
"net/http"
10+
"os"
11+
"reflect"
1212
"strings"
13+
14+
"github.com/andreyvit/diff"
15+
"github.com/antonmedv/expr"
16+
"github.com/antonmedv/expr/vm"
17+
"github.com/linuxsuren/api-testing/pkg/exec"
18+
"github.com/linuxsuren/api-testing/pkg/testing"
19+
unstructured "github.com/linuxsuren/unstructured/pkg"
1320
)
1421

15-
func RunTestCase(testcase *testing.TestCase) (err error) {
22+
func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{}, err error) {
1623
fmt.Printf("start to run: '%s'\n", testcase.Name)
1724
if err = doPrepare(testcase); err != nil {
1825
err = fmt.Errorf("failed to prepare, error: %v", err)
@@ -31,6 +38,16 @@ func RunTestCase(testcase *testing.TestCase) (err error) {
3138
var requestBody io.Reader
3239
if testcase.Request.Body != "" {
3340
requestBody = bytes.NewBufferString(testcase.Request.Body)
41+
} else if testcase.Request.BodyFromFile != "" {
42+
var data []byte
43+
if data, err = os.ReadFile(testcase.Request.BodyFromFile); err != nil {
44+
return
45+
}
46+
requestBody = bytes.NewBufferString(string(data))
47+
}
48+
49+
if err = testcase.Request.Render(ctx); err != nil {
50+
return
3451
}
3552

3653
var request *http.Request
@@ -64,18 +81,69 @@ func RunTestCase(testcase *testing.TestCase) (err error) {
6481
}
6582
}
6683

84+
var responseBodyData []byte
85+
if responseBodyData, err = ioutil.ReadAll(resp.Body); err != nil {
86+
return
87+
}
6788
if testcase.Expect.Body != "" {
68-
var data []byte
69-
if data, err = ioutil.ReadAll(resp.Body); err != nil {
89+
if string(responseBodyData) != strings.TrimSpace(testcase.Expect.Body) {
90+
err = fmt.Errorf("case: %s, got different response body, diff: \n%s", testcase.Name,
91+
diff.LineDiff(testcase.Expect.Body, string(responseBodyData)))
92+
return
93+
}
94+
}
95+
96+
mapOutput := map[string]interface{}{}
97+
if err = json.Unmarshal(responseBodyData, &mapOutput); err != nil {
98+
switch b := err.(type) {
99+
case *json.UnmarshalTypeError:
100+
if b.Value != "array" {
101+
return
102+
} else {
103+
arrayOutput := []interface{}{}
104+
if err = json.Unmarshal(responseBodyData, &arrayOutput); err != nil {
105+
return
106+
}
107+
output = arrayOutput
108+
}
109+
default:
70110
return
71111
}
112+
} else {
113+
output = mapOutput
114+
}
72115

73-
if string(data) != strings.TrimSpace(testcase.Expect.Body) {
74-
err = fmt.Errorf("case: %s, got different response body, diff: \n%s", testcase.Name,
75-
diff.LineDiff(testcase.Expect.Body, string(data)))
116+
for key, expectVal := range testcase.Expect.BodyFieldsExpect {
117+
var val interface{}
118+
var ok bool
119+
if val, ok, err = unstructured.NestedField(mapOutput, strings.Split(key, "/")...); err != nil {
120+
err = fmt.Errorf("failed to get field: %s, %v", key, err)
121+
return
122+
} else if !ok {
123+
err = fmt.Errorf("not found field: %s", key)
124+
return
125+
} else if !reflect.DeepEqual(expectVal, val) {
126+
err = fmt.Errorf("field[%s] expect value: %v, actual: %v", key, expectVal, val)
76127
return
77128
}
78129
}
130+
131+
for _, verify := range testcase.Expect.Verify {
132+
var program *vm.Program
133+
if program, err = expr.Compile(verify, expr.Env(output), expr.AsBool()); err != nil {
134+
return
135+
}
136+
137+
var result interface{}
138+
if result, err = expr.Run(program, output); err != nil {
139+
return
140+
}
141+
142+
if !result.(bool) {
143+
err = fmt.Errorf("faild to verify")
144+
break
145+
}
146+
}
79147
return
80148
}
81149

0 commit comments

Comments
 (0)