Skip to content

Commit 75340ed

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

File tree

6 files changed

+158
-18
lines changed

6 files changed

+158
-18
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
This is a API testing tool.
2+
3+
## Template
4+
The following fields are templated with [sprig](http://masterminds.github.io/sprig/):
5+
6+
* API
7+
* Request Body

go.mod

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@ 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.5.1
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
1316
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/google/uuid v1.1.1 // indirect
18+
github.com/huandu/xstrings v1.3.3 // indirect
19+
github.com/imdario/mergo v0.3.11 // indirect
1420
github.com/inconshreveable/mousetrap v1.0.0 // indirect
21+
github.com/linuxsuren/unstructured v0.0.1 // indirect
22+
github.com/mitchellh/copystructure v1.0.0 // indirect
23+
github.com/mitchellh/reflectwalk v1.0.0 // indirect
1524
github.com/pmezard/go-difflib v1.0.0 // indirect
1625
github.com/sergi/go-diff v1.2.0 // indirect
26+
github.com/shopspring/decimal v1.2.0 // indirect
27+
github.com/spf13/cast v1.3.1 // indirect
1728
github.com/spf13/pflag v1.0.5 // indirect
29+
golang.org/x/crypto v0.3.0 // indirect
1830
)

go.sum

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,87 @@
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=
39
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
410
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
511
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
612
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13+
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
14+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
15+
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
16+
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
17+
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
18+
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
719
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
820
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
921
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1022
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1123
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1224
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1325
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
26+
github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU=
27+
github.com/linuxsuren/unstructured v0.0.1/go.mod h1:KH6aTj+FegzGBzc1vS6mzZx3/duhTUTEVyW5sO7p4as=
28+
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
29+
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
30+
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
31+
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
1432
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1533
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1634
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
1735
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
1836
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
37+
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
38+
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
39+
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
40+
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
1941
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
2042
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
2143
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
2244
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
2345
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
46+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
2447
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
2548
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
49+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
50+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
51+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
52+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
53+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
54+
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
55+
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
56+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
57+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
58+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
59+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
60+
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
61+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
62+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
63+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
64+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
65+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
66+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
67+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
68+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
69+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
70+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
71+
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
72+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
73+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
74+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
75+
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
76+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
77+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
78+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
79+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2680
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2781
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
2882
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2983
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3084
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
85+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3186
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
3287
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

pkg/runner/simple.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ 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/linuxsuren/api-testing/pkg/exec"
16+
"github.com/linuxsuren/api-testing/pkg/testing"
17+
unstructured "github.com/linuxsuren/unstructured/pkg"
1318
)
1419

1520
func RunTestCase(testcase *testing.TestCase) (err error) {
@@ -31,6 +36,12 @@ func RunTestCase(testcase *testing.TestCase) (err error) {
3136
var requestBody io.Reader
3237
if testcase.Request.Body != "" {
3338
requestBody = bytes.NewBufferString(testcase.Request.Body)
39+
} else if testcase.Request.BodyFromFile != "" {
40+
var data []byte
41+
if data, err = os.ReadFile(testcase.Request.BodyFromFile); err != nil {
42+
return
43+
}
44+
requestBody = bytes.NewBufferString(string(data))
3445
}
3546

3647
var request *http.Request
@@ -64,15 +75,33 @@ func RunTestCase(testcase *testing.TestCase) (err error) {
6475
}
6576
}
6677

78+
var responseBodyData []byte
79+
if responseBodyData, err = ioutil.ReadAll(resp.Body); err != nil {
80+
return
81+
}
6782
if testcase.Expect.Body != "" {
68-
var data []byte
69-
if data, err = ioutil.ReadAll(resp.Body); err != nil {
83+
if string(responseBodyData) != strings.TrimSpace(testcase.Expect.Body) {
84+
err = fmt.Errorf("case: %s, got different response body, diff: \n%s", testcase.Name,
85+
diff.LineDiff(testcase.Expect.Body, string(responseBodyData)))
7086
return
7187
}
88+
}
7289

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)))
90+
obj := map[string]interface{}{}
91+
if err = json.Unmarshal(responseBodyData, &obj); err != nil {
92+
return
93+
}
94+
for key, expectVal := range testcase.Expect.BodyFieldsExpect {
95+
var val interface{}
96+
var ok bool
97+
if val, ok, err = unstructured.NestedField(obj, strings.Split(key, "/")...); err != nil {
98+
err = fmt.Errorf("failed to get field: %s, %v", key, err)
99+
return
100+
} else if !ok {
101+
err = fmt.Errorf("not found field: %s", key)
102+
return
103+
} else if !reflect.DeepEqual(expectVal, val) {
104+
err = fmt.Errorf("field[%s] expect value: %v, actual: %v", key, expectVal, val)
76105
return
77106
}
78107
}

pkg/testing/case.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ type Prepare struct {
1414
}
1515

1616
type Request struct {
17-
API string `yaml:"api"`
18-
Method string `yaml:"method"`
19-
Query map[string]string `yaml:"query"`
20-
Header map[string]string `yaml:"header"`
21-
Body string `yaml:"body"`
17+
API string `yaml:"api"`
18+
Method string `yaml:"method"`
19+
Query map[string]string `yaml:"query"`
20+
Header map[string]string `yaml:"header"`
21+
Body string `yaml:"body"`
22+
BodyFromFile string `yaml:"bodyFromFile"`
2223
}
2324

2425
type Response struct {
25-
StatusCode int `yaml:"statusCode"`
26-
Body string `yaml:"body"`
27-
Header map[string]string `yaml:"header"`
26+
StatusCode int `yaml:"statusCode"`
27+
Body string `yaml:"body"`
28+
Header map[string]string `yaml:"header"`
29+
BodyFieldsExpect map[string]string `yaml:"bodyFieldsExpect"`
2830
}
2931

3032
type Clean struct {

pkg/testing/parser.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package testing
22

33
import (
4-
"gopkg.in/yaml.v2"
4+
"bytes"
5+
"html/template"
56
"io/ioutil"
7+
"os"
8+
9+
"github.com/Masterminds/sprig/v3"
10+
"gopkg.in/yaml.v2"
611
)
712

813
func Parse(configFile string) (testCase *TestCase, err error) {
@@ -15,5 +20,36 @@ func Parse(configFile string) (testCase *TestCase, err error) {
1520
if err = yaml.Unmarshal(data, testCase); err != nil {
1621
return
1722
}
23+
24+
// template the API
25+
var tpl *template.Template
26+
if tpl, err = template.New("base").Funcs(sprig.FuncMap()).Parse(testCase.Request.API); err != nil {
27+
return
28+
}
29+
buf := new(bytes.Buffer)
30+
if err = tpl.Execute(buf, os.Environ()); err != nil {
31+
return
32+
} else {
33+
testCase.Request.API = buf.String()
34+
}
35+
36+
// read body from file
37+
if testCase.Request.BodyFromFile != "" {
38+
if data, err = os.ReadFile(testCase.Request.BodyFromFile); err != nil {
39+
return
40+
}
41+
testCase.Request.Body = string(data)
42+
}
43+
44+
// template the body
45+
if tpl, err = template.New("base").Funcs(sprig.FuncMap()).Parse(testCase.Request.Body); err != nil {
46+
return
47+
}
48+
buf = new(bytes.Buffer)
49+
if err = tpl.Execute(buf, os.Environ()); err != nil {
50+
return
51+
} else {
52+
testCase.Request.Body = buf.String()
53+
}
1854
return
1955
}

0 commit comments

Comments
 (0)