Skip to content

Commit f0d7626

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

File tree

7 files changed

+226
-31
lines changed

7 files changed

+226
-31
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

cmd/run.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package main
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 {
@@ -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 map[string]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: 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: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ 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

15-
func RunTestCase(testcase *testing.TestCase) (err error) {
20+
func RunTestCase(testcase *testing.TestCase, ctx map[string]interface{}) (output map[string]interface{}, err error) {
1621
fmt.Printf("start to run: '%s'\n", testcase.Name)
1722
if err = doPrepare(testcase); err != nil {
1823
err = fmt.Errorf("failed to prepare, error: %v", err)
@@ -31,6 +36,16 @@ 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))
45+
}
46+
47+
if err = testcase.Request.Render(nil); err != nil {
48+
return
3449
}
3550

3651
var request *http.Request
@@ -64,15 +79,33 @@ func RunTestCase(testcase *testing.TestCase) (err error) {
6479
}
6580
}
6681

82+
var responseBodyData []byte
83+
if responseBodyData, err = ioutil.ReadAll(resp.Body); err != nil {
84+
return
85+
}
6786
if testcase.Expect.Body != "" {
68-
var data []byte
69-
if data, err = ioutil.ReadAll(resp.Body); err != nil {
87+
if string(responseBodyData) != strings.TrimSpace(testcase.Expect.Body) {
88+
err = fmt.Errorf("case: %s, got different response body, diff: \n%s", testcase.Name,
89+
diff.LineDiff(testcase.Expect.Body, string(responseBodyData)))
7090
return
7191
}
92+
}
7293

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

pkg/testing/case.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package testing
22

3+
type TestSuite struct {
4+
Name string `yaml:"name"`
5+
Items []TestCase `yaml:"items"`
6+
}
7+
38
type TestCase struct {
49
Name string
510
Group string
@@ -14,17 +19,19 @@ type Prepare struct {
1419
}
1520

1621
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"`
22+
API string `yaml:"api"`
23+
Method string `yaml:"method"`
24+
Query map[string]string `yaml:"query"`
25+
Header map[string]string `yaml:"header"`
26+
Body string `yaml:"body"`
27+
BodyFromFile string `yaml:"bodyFromFile"`
2228
}
2329

2430
type Response struct {
25-
StatusCode int `yaml:"statusCode"`
26-
Body string `yaml:"body"`
27-
Header map[string]string `yaml:"header"`
31+
StatusCode int `yaml:"statusCode"`
32+
Body string `yaml:"body"`
33+
Header map[string]string `yaml:"header"`
34+
BodyFieldsExpect map[string]string `yaml:"bodyFieldsExpect"`
2835
}
2936

3037
type Clean struct {

pkg/testing/parser.go

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,94 @@
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

8-
func Parse(configFile string) (testCase *TestCase, err error) {
13+
func Parse(configFile string) (testSuite *TestSuite, err error) {
914
var data []byte
1015
if data, err = ioutil.ReadFile(configFile); err != nil {
1116
return
1217
}
1318

14-
testCase = &TestCase{}
15-
if err = yaml.Unmarshal(data, testCase); err != nil {
19+
testSuite = &TestSuite{}
20+
if err = yaml.Unmarshal(data, testSuite); err != nil {
21+
return
22+
}
23+
24+
// for i, testCase := range testSuite.Items {
25+
// // template the API
26+
// var tpl *template.Template
27+
// if tpl, err = template.New("base").Funcs(sprig.FuncMap()).Parse(testCase.Request.API); err != nil {
28+
// return
29+
// }
30+
// buf := new(bytes.Buffer)
31+
// if err = tpl.Execute(buf, os.Environ()); err != nil {
32+
// return
33+
// } else {
34+
// testCase.Request.API = buf.String()
35+
// }
36+
37+
// // read body from file
38+
// if testCase.Request.BodyFromFile != "" {
39+
// if data, err = os.ReadFile(testCase.Request.BodyFromFile); err != nil {
40+
// return
41+
// }
42+
// testCase.Request.Body = string(data)
43+
// }
44+
45+
// // template the body
46+
// if tpl, err = template.New("base").Funcs(sprig.FuncMap()).Parse(testCase.Request.Body); err != nil {
47+
// return
48+
// }
49+
// buf = new(bytes.Buffer)
50+
// if err = tpl.Execute(buf, os.Environ()); err != nil {
51+
// return
52+
// } else {
53+
// testCase.Request.Body = buf.String()
54+
// }
55+
56+
// testSuite.Items[i] = testCase
57+
// }
58+
return
59+
}
60+
61+
func (r *Request) Render(ctx map[string]interface{}) (err error) {
62+
// template the API
63+
var tpl *template.Template
64+
if tpl, err = template.New("base").Funcs(sprig.FuncMap()).Parse(r.API); err != nil {
65+
return
66+
}
67+
buf := new(bytes.Buffer)
68+
if err = tpl.Execute(buf, os.Environ()); err != nil {
69+
return
70+
} else {
71+
r.API = buf.String()
72+
}
73+
74+
// read body from file
75+
if r.BodyFromFile != "" {
76+
var data []byte
77+
if data, err = os.ReadFile(r.BodyFromFile); err != nil {
78+
return
79+
}
80+
r.Body = string(data)
81+
}
82+
83+
// template the body
84+
if tpl, err = template.New("base").Funcs(sprig.FuncMap()).Parse(r.Body); err != nil {
85+
return
86+
}
87+
buf = new(bytes.Buffer)
88+
if err = tpl.Execute(buf, ctx); err != nil {
1689
return
90+
} else {
91+
r.Body = buf.String()
1792
}
1893
return
1994
}

0 commit comments

Comments
 (0)