feat: improve the HTTP request payload and expect (#2)
* feat: improve the HTTP request payload and expect * add unit tests --------- Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
parent
506595a65b
commit
e52246454c
|
@ -0,0 +1,23 @@
|
||||||
|
name: Coverage Report
|
||||||
|
|
||||||
|
on:
|
||||||
|
- push
|
||||||
|
- pull_request
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
TestAndReport:
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
steps:
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: 1.18.x
|
||||||
|
- uses: actions/checkout@v3.0.0
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
go test ./... -coverprofile coverage.out
|
||||||
|
- name: Report
|
||||||
|
env:
|
||||||
|
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
|
||||||
|
run: |
|
||||||
|
bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r coverage.out --force-coverage-parser go
|
|
@ -1 +1,2 @@
|
||||||
bin/
|
bin/
|
||||||
|
.idea/
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -4,3 +4,5 @@ build:
|
||||||
|
|
||||||
copy: build
|
copy: build
|
||||||
cp bin/atest /usr/local/bin/
|
cp bin/atest /usr/local/bin/
|
||||||
|
test:
|
||||||
|
go test ./...
|
||||||
|
|
18
README.md
18
README.md
|
@ -1 +1,19 @@
|
||||||
|
[](https://www.codacy.com/gh/LinuxSuRen/api-testing/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/api-testing&utm_campaign=Badge_Grade)
|
||||||
|
[](https://www.codacy.com/gh/LinuxSuRen/api-testing/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/api-testing&utm_campaign=Badge_Coverage)
|
||||||
|

|
||||||
|
|
||||||
This is a API testing tool.
|
This is a API testing tool.
|
||||||
|
|
||||||
|
## Feature
|
||||||
|
* Response Body fields equation check
|
||||||
|
* Response Body [eval](https://expr.medv.io/)
|
||||||
|
* Output reference between TestCase
|
||||||
|
|
||||||
|
## Template
|
||||||
|
The following fields are templated with [sprig](http://masterminds.github.io/sprig/):
|
||||||
|
|
||||||
|
* API
|
||||||
|
* Request Body
|
||||||
|
|
||||||
|
## Limit
|
||||||
|
* Only support to parse the response body when it's a map
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/linuxsuren/api-testing/pkg/exec"
|
"github.com/linuxsuren/api-testing/pkg/exec"
|
||||||
|
@ -11,7 +11,8 @@ type initOption struct {
|
||||||
waitResource string
|
waitResource string
|
||||||
}
|
}
|
||||||
|
|
||||||
func createInitCommand() (cmd *cobra.Command) {
|
// CreateInitCommand returns the init command
|
||||||
|
func CreateInitCommand() (cmd *cobra.Command) {
|
||||||
opt := &initOption{}
|
opt := &initOption{}
|
||||||
cmd = &cobra.Command{
|
cmd = &cobra.Command{
|
||||||
Use: "init",
|
Use: "init",
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package main
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
atesting "github.com/linuxsuren/api-testing/pkg/testing"
|
||||||
)
|
)
|
||||||
import atesting "github.com/linuxsuren/api-testing/pkg/testing"
|
|
||||||
|
|
||||||
func Test_setRelativeDir(t *testing.T) {
|
func Test_setRelativeDir(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
|
@ -36,3 +38,11 @@ func Test_setRelativeDir(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateRunCommand(t *testing.T) {
|
||||||
|
cmd := CreateRunCommand()
|
||||||
|
assert.Equal(t, "run", cmd.Use)
|
||||||
|
|
||||||
|
init := CreateInitCommand()
|
||||||
|
assert.Equal(t, "init", init.Use)
|
||||||
|
}
|
||||||
|
|
30
cmd/run.go
30
cmd/run.go
|
@ -1,18 +1,20 @@
|
||||||
package main
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/linuxsuren/api-testing/pkg/runner"
|
"github.com/linuxsuren/api-testing/pkg/runner"
|
||||||
"github.com/linuxsuren/api-testing/pkg/testing"
|
"github.com/linuxsuren/api-testing/pkg/testing"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type runOption struct {
|
type runOption struct {
|
||||||
pattern string
|
pattern string
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRunCommand() (cmd *cobra.Command) {
|
// CreateRunCommand returns the run command
|
||||||
|
func CreateRunCommand() (cmd *cobra.Command) {
|
||||||
opt := &runOption{}
|
opt := &runOption{}
|
||||||
cmd = &cobra.Command{
|
cmd = &cobra.Command{
|
||||||
Use: "run",
|
Use: "run",
|
||||||
|
@ -21,26 +23,32 @@ func createRunCommand() (cmd *cobra.Command) {
|
||||||
|
|
||||||
// set flags
|
// set flags
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.StringVarP(&opt.pattern, "pattern", "p", "testcase-*.yaml",
|
flags.StringVarP(&opt.pattern, "pattern", "p", "test-suite-*.yaml",
|
||||||
"The file pattern which try to execute the test cases")
|
"The file pattern which try to execute the test cases")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
|
func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
|
||||||
var files []string
|
var files []string
|
||||||
|
|
||||||
|
ctx := map[string]interface{}{}
|
||||||
|
|
||||||
if files, err = filepath.Glob(o.pattern); err == nil {
|
if files, err = filepath.Glob(o.pattern); err == nil {
|
||||||
for i := range files {
|
for i := range files {
|
||||||
item := files[i]
|
item := files[i]
|
||||||
|
|
||||||
var testcase *testing.TestCase
|
var testSuite *testing.TestSuite
|
||||||
if testcase, err = testing.Parse(item); err != nil {
|
if testSuite, err = testing.Parse(item); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
setRelativeDir(item, testcase)
|
for _, testCase := range testSuite.Items {
|
||||||
|
setRelativeDir(item, &testCase)
|
||||||
if err = runner.RunTestCase(testcase); err != nil {
|
var output interface{}
|
||||||
return
|
if output, err = runner.RunTestCase(&testCase, ctx); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx[testCase.Name] = output
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
18
go.mod
18
go.mod
|
@ -5,14 +5,30 @@ go 1.17
|
||||||
require (
|
require (
|
||||||
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
|
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
|
||||||
github.com/spf13/cobra v1.4.0
|
github.com/spf13/cobra v1.4.0
|
||||||
github.com/stretchr/testify v1.4.0
|
github.com/stretchr/testify v1.8.2
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||||
|
github.com/Masterminds/semver/v3 v3.2.0 // indirect
|
||||||
|
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
|
||||||
|
github.com/antonmedv/expr v1.12.1 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/google/uuid v1.1.1 // indirect
|
||||||
|
github.com/h2non/gock v1.2.0 // indirect
|
||||||
|
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
|
||||||
|
github.com/huandu/xstrings v1.3.3 // indirect
|
||||||
|
github.com/imdario/mergo v0.3.11 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||||
|
github.com/linuxsuren/unstructured v0.0.1 // indirect
|
||||||
|
github.com/mitchellh/copystructure v1.0.0 // indirect
|
||||||
|
github.com/mitchellh/reflectwalk v1.0.0 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/sergi/go-diff v1.2.0 // indirect
|
github.com/sergi/go-diff v1.2.0 // indirect
|
||||||
|
github.com/shopspring/decimal v1.2.0 // indirect
|
||||||
|
github.com/spf13/cast v1.3.1 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
|
golang.org/x/crypto v0.3.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
71
go.sum
71
go.sum
|
@ -1,9 +1,27 @@
|
||||||
|
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
|
||||||
|
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
|
||||||
|
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
|
||||||
|
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
|
||||||
|
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
|
||||||
|
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
|
||||||
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
|
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
|
||||||
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
|
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
|
||||||
|
github.com/antonmedv/expr v1.12.1 h1:GTGrGN1kxxb+le0uQKaFRK8By4cvq1sleUCGE/U6hHg=
|
||||||
|
github.com/antonmedv/expr v1.12.1/go.mod h1:FPC8iWArxls7axbVLsW+kpg1mz29A1b2M6jt+hZfDkU=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||||
|
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
|
||||||
|
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk=
|
||||||
|
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
|
||||||
|
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
|
||||||
|
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
|
||||||
|
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
|
||||||
|
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
|
||||||
|
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||||
|
@ -11,22 +29,75 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU=
|
||||||
|
github.com/linuxsuren/unstructured v0.0.1/go.mod h1:KH6aTj+FegzGBzc1vS6mzZx3/duhTUTEVyW5sO7p4as=
|
||||||
|
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
|
||||||
|
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
|
||||||
|
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
|
||||||
|
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||||
|
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
|
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
|
||||||
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||||
|
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
|
||||||
|
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||||
|
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
|
||||||
|
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||||
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
|
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
|
||||||
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
|
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
|
||||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
||||||
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||||
|
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
|
||||||
|
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
// Package runner responsible for excute the test case
|
||||||
|
package runner
|
|
@ -2,17 +2,25 @@ package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/andreyvit/diff"
|
|
||||||
"github.com/linuxsuren/api-testing/pkg/exec"
|
|
||||||
"github.com/linuxsuren/api-testing/pkg/testing"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/andreyvit/diff"
|
||||||
|
"github.com/antonmedv/expr"
|
||||||
|
"github.com/antonmedv/expr/vm"
|
||||||
|
"github.com/linuxsuren/api-testing/pkg/exec"
|
||||||
|
"github.com/linuxsuren/api-testing/pkg/testing"
|
||||||
|
unstructured "github.com/linuxsuren/unstructured/pkg"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunTestCase(testcase *testing.TestCase) (err error) {
|
// RunTestCase runs the test case
|
||||||
|
func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{}, err error) {
|
||||||
fmt.Printf("start to run: '%s'\n", testcase.Name)
|
fmt.Printf("start to run: '%s'\n", testcase.Name)
|
||||||
if err = doPrepare(testcase); err != nil {
|
if err = doPrepare(testcase); err != nil {
|
||||||
err = fmt.Errorf("failed to prepare, error: %v", err)
|
err = fmt.Errorf("failed to prepare, error: %v", err)
|
||||||
|
@ -31,6 +39,16 @@ func RunTestCase(testcase *testing.TestCase) (err error) {
|
||||||
var requestBody io.Reader
|
var requestBody io.Reader
|
||||||
if testcase.Request.Body != "" {
|
if testcase.Request.Body != "" {
|
||||||
requestBody = bytes.NewBufferString(testcase.Request.Body)
|
requestBody = bytes.NewBufferString(testcase.Request.Body)
|
||||||
|
} else if testcase.Request.BodyFromFile != "" {
|
||||||
|
var data []byte
|
||||||
|
if data, err = os.ReadFile(testcase.Request.BodyFromFile); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
requestBody = bytes.NewBufferString(string(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = testcase.Request.Render(ctx); err != nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var request *http.Request
|
var request *http.Request
|
||||||
|
@ -64,17 +82,68 @@ func RunTestCase(testcase *testing.TestCase) (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var responseBodyData []byte
|
||||||
|
if responseBodyData, err = ioutil.ReadAll(resp.Body); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
if testcase.Expect.Body != "" {
|
if testcase.Expect.Body != "" {
|
||||||
var data []byte
|
if string(responseBodyData) != strings.TrimSpace(testcase.Expect.Body) {
|
||||||
if data, err = ioutil.ReadAll(resp.Body); err != nil {
|
err = fmt.Errorf("case: %s, got different response body, diff: \n%s", testcase.Name,
|
||||||
|
diff.LineDiff(testcase.Expect.Body, string(responseBodyData)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mapOutput := map[string]interface{}{}
|
||||||
|
if err = json.Unmarshal(responseBodyData, &mapOutput); err != nil {
|
||||||
|
switch b := err.(type) {
|
||||||
|
case *json.UnmarshalTypeError:
|
||||||
|
if b.Value != "array" {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
arrayOutput := []interface{}{}
|
||||||
|
if err = json.Unmarshal(responseBodyData, &arrayOutput); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output = arrayOutput
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
output = mapOutput
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, expectVal := range testcase.Expect.BodyFieldsExpect {
|
||||||
|
var val interface{}
|
||||||
|
var ok bool
|
||||||
|
if val, ok, err = unstructured.NestedField(mapOutput, strings.Split(key, "/")...); err != nil {
|
||||||
|
err = fmt.Errorf("failed to get field: %s, %v", key, err)
|
||||||
|
return
|
||||||
|
} else if !ok {
|
||||||
|
err = fmt.Errorf("not found field: %s", key)
|
||||||
|
return
|
||||||
|
} else if !reflect.DeepEqual(expectVal, val) {
|
||||||
|
err = fmt.Errorf("field[%s] expect value: %v, actual: %v", key, expectVal, val)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, verify := range testcase.Expect.Verify {
|
||||||
|
var program *vm.Program
|
||||||
|
if program, err = expr.Compile(verify, expr.Env(output), expr.AsBool()); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(data) != strings.TrimSpace(testcase.Expect.Body) {
|
var result interface{}
|
||||||
err = fmt.Errorf("case: %s, got different response body, diff: \n%s", testcase.Name,
|
if result, err = expr.Run(program, output); err != nil {
|
||||||
diff.LineDiff(testcase.Expect.Body, string(data)))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !result.(bool) {
|
||||||
|
err = fmt.Errorf("faild to verify: %s", verify)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package runner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/h2non/gock"
|
||||||
|
atest "github.com/linuxsuren/api-testing/pkg/testing"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTestCase(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
testCase *atest.TestCase
|
||||||
|
ctx interface{}
|
||||||
|
prepare func()
|
||||||
|
verify func(t *testing.T, output interface{}, err error)
|
||||||
|
}{{
|
||||||
|
name: "normal",
|
||||||
|
testCase: &atest.TestCase{
|
||||||
|
Request: atest.Request{
|
||||||
|
API: "http://localhost/foo",
|
||||||
|
Header: map[string]string{
|
||||||
|
"key": "value",
|
||||||
|
},
|
||||||
|
Body: `{"foo":"bar"}`,
|
||||||
|
},
|
||||||
|
Expect: atest.Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
BodyFieldsExpect: map[string]string{
|
||||||
|
"name": "linuxsuren",
|
||||||
|
},
|
||||||
|
Header: map[string]string{
|
||||||
|
"type": "generic",
|
||||||
|
},
|
||||||
|
Verify: []string{
|
||||||
|
`name == "linuxsuren"`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
prepare: func() {
|
||||||
|
gock.New("http://localhost").
|
||||||
|
Get("/foo").
|
||||||
|
MatchHeader("key", "value").
|
||||||
|
Reply(http.StatusOK).
|
||||||
|
SetHeader("type", "generic").
|
||||||
|
File("testdata/generic_response.json")
|
||||||
|
},
|
||||||
|
verify: func(t *testing.T, output interface{}, err error) {
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, map[string]interface{}{"name": "linuxsuren"}, output)
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
defer gock.Clean()
|
||||||
|
tt.prepare()
|
||||||
|
output, err := RunTestCase(tt.testCase, tt.ctx)
|
||||||
|
tt.verify(t, output, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"name": "linuxsuren"
|
||||||
|
}
|
|
@ -1,5 +1,12 @@
|
||||||
package testing
|
package testing
|
||||||
|
|
||||||
|
// TestSuite represents a set of test cases
|
||||||
|
type TestSuite struct {
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
Items []TestCase `yaml:"items"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCase represents a test case
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
Name string
|
Name string
|
||||||
Group string
|
Group string
|
||||||
|
@ -9,24 +16,31 @@ type TestCase struct {
|
||||||
Clean Clean `yaml:"clean"`
|
Clean Clean `yaml:"clean"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare does the prepare work
|
||||||
type Prepare struct {
|
type Prepare struct {
|
||||||
Kubernetes []string `yaml:"kubernetes"`
|
Kubernetes []string `yaml:"kubernetes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Request represents a HTTP request
|
||||||
type Request struct {
|
type Request struct {
|
||||||
API string `yaml:"api"`
|
API string `yaml:"api"`
|
||||||
Method string `yaml:"method"`
|
Method string `yaml:"method"`
|
||||||
Query map[string]string `yaml:"query"`
|
Query map[string]string `yaml:"query"`
|
||||||
Header map[string]string `yaml:"header"`
|
Header map[string]string `yaml:"header"`
|
||||||
Body string `yaml:"body"`
|
Body string `yaml:"body"`
|
||||||
|
BodyFromFile string `yaml:"bodyFromFile"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Response is the expected response
|
||||||
type Response struct {
|
type Response struct {
|
||||||
StatusCode int `yaml:"statusCode"`
|
StatusCode int `yaml:"statusCode"`
|
||||||
Body string `yaml:"body"`
|
Body string `yaml:"body"`
|
||||||
Header map[string]string `yaml:"header"`
|
Header map[string]string `yaml:"header"`
|
||||||
|
BodyFieldsExpect map[string]string `yaml:"bodyFieldsExpect"`
|
||||||
|
Verify []string `yaml:"verify"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean represents the clean work after testing
|
||||||
type Clean struct {
|
type Clean struct {
|
||||||
CleanPrepare bool `yaml:"cleanPrepare"`
|
CleanPrepare bool `yaml:"cleanPrepare"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
// Package testing provide the test case functions
|
||||||
|
package testing
|
|
@ -1,19 +1,54 @@
|
||||||
package testing
|
package testing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"html/template"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Masterminds/sprig/v3"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Parse(configFile string) (testCase *TestCase, err error) {
|
// Parse parses a file and returns the test suite
|
||||||
|
func Parse(configFile string) (testSuite *TestSuite, err error) {
|
||||||
var data []byte
|
var data []byte
|
||||||
if data, err = ioutil.ReadFile(configFile); err != nil {
|
if data, err = os.ReadFile(configFile); err == nil {
|
||||||
return
|
testSuite = &TestSuite{}
|
||||||
}
|
err = yaml.Unmarshal(data, testSuite)
|
||||||
|
}
|
||||||
testCase = &TestCase{}
|
return
|
||||||
if err = yaml.Unmarshal(data, testCase); err != nil {
|
}
|
||||||
return
|
|
||||||
|
// Render injects the template based context
|
||||||
|
func (r *Request) Render(ctx interface{}) (err error) {
|
||||||
|
// template the API
|
||||||
|
var tpl *template.Template
|
||||||
|
if tpl, err = template.New("api").Funcs(sprig.FuncMap()).Parse(r.API); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
if err = tpl.Execute(buf, ctx); err != nil {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
r.API = buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// read body from file
|
||||||
|
if r.BodyFromFile != "" {
|
||||||
|
var data []byte
|
||||||
|
if data, err = os.ReadFile(r.BodyFromFile); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.Body = strings.TrimSpace(string(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
// template the body
|
||||||
|
if tpl, err = template.New("body").Funcs(sprig.FuncMap()).Parse(r.Body); err == nil {
|
||||||
|
buf = new(bytes.Buffer)
|
||||||
|
if err = tpl.Execute(buf, ctx); err == nil {
|
||||||
|
r.Body = buf.String()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
package testing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParse(t *testing.T) {
|
||||||
|
suite, err := Parse("../../sample/testsuite-gitlab.yaml")
|
||||||
|
if assert.Nil(t, err) && assert.NotNil(t, suite) {
|
||||||
|
assert.Equal(t, "Gitlab", suite.Name)
|
||||||
|
assert.Equal(t, 2, len(suite.Items))
|
||||||
|
assert.Equal(t, TestCase{
|
||||||
|
Name: "projects",
|
||||||
|
Request: Request{
|
||||||
|
API: "https://gitlab.com/api/v4/projects",
|
||||||
|
},
|
||||||
|
Expect: Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
}, suite.Items[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRender(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
request *Request
|
||||||
|
verify func(t *testing.T, req *Request)
|
||||||
|
ctx interface{}
|
||||||
|
hasErr bool
|
||||||
|
}{{
|
||||||
|
name: "slice as context",
|
||||||
|
request: &Request{
|
||||||
|
API: "http://localhost/{{index . 0}}",
|
||||||
|
Body: "{{index . 1}}",
|
||||||
|
},
|
||||||
|
ctx: []string{"foo", "bar"},
|
||||||
|
hasErr: false,
|
||||||
|
verify: func(t *testing.T, req *Request) {
|
||||||
|
assert.Equal(t, "http://localhost/foo", req.API)
|
||||||
|
assert.Equal(t, "bar", req.Body)
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
name: "context is nil",
|
||||||
|
request: &Request{},
|
||||||
|
ctx: nil,
|
||||||
|
hasErr: false,
|
||||||
|
}, {
|
||||||
|
name: "body from file",
|
||||||
|
request: &Request{
|
||||||
|
BodyFromFile: "testdata/generic_body.json",
|
||||||
|
},
|
||||||
|
ctx: TestCase{
|
||||||
|
Name: "linuxsuren",
|
||||||
|
},
|
||||||
|
hasErr: false,
|
||||||
|
verify: func(t *testing.T, req *Request) {
|
||||||
|
assert.Equal(t, `{"name": "linuxsuren"}`, req.Body)
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
name: "body file not found",
|
||||||
|
request: &Request{
|
||||||
|
BodyFromFile: "testdata/fake",
|
||||||
|
},
|
||||||
|
hasErr: true,
|
||||||
|
}, {
|
||||||
|
name: "invalid API as template",
|
||||||
|
request: &Request{
|
||||||
|
API: "{{.name}",
|
||||||
|
},
|
||||||
|
hasErr: true,
|
||||||
|
}, {
|
||||||
|
name: "failed with API render",
|
||||||
|
request: &Request{
|
||||||
|
API: "{{.name}}",
|
||||||
|
},
|
||||||
|
ctx: TestCase{},
|
||||||
|
hasErr: true,
|
||||||
|
}, {
|
||||||
|
name: "invalid body as template",
|
||||||
|
request: &Request{
|
||||||
|
Body: "{{.name}",
|
||||||
|
},
|
||||||
|
hasErr: true,
|
||||||
|
}, {
|
||||||
|
name: "failed with body render",
|
||||||
|
request: &Request{
|
||||||
|
Body: "{{.name}}",
|
||||||
|
},
|
||||||
|
ctx: TestCase{},
|
||||||
|
hasErr: true,
|
||||||
|
}}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
err := tt.request.Render(tt.ctx)
|
||||||
|
if assert.Equal(t, tt.hasErr, err != nil, err) && tt.verify != nil {
|
||||||
|
tt.verify(t, tt.request)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
{"name": "{{.Name}}"}
|
|
@ -1,15 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
c "github.com/linuxsuren/api-testing/cmd"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "atest",
|
Use: "atest",
|
||||||
}
|
}
|
||||||
cmd.AddCommand(createInitCommand(), createRunCommand())
|
cmd.AddCommand(c.CreateInitCommand(), c.CreateRunCommand())
|
||||||
|
|
||||||
// run command
|
// run command
|
||||||
if err := cmd.Execute(); err != nil {
|
if err := cmd.Execute(); err != nil {
|
|
@ -0,0 +1,20 @@
|
||||||
|
# https://docs.gitlab.com/ee/api/api_resources.html
|
||||||
|
name: Gitlab
|
||||||
|
items:
|
||||||
|
- name: projects
|
||||||
|
request:
|
||||||
|
api: https://gitlab.com/api/v4/projects
|
||||||
|
expect:
|
||||||
|
statusCode: 200
|
||||||
|
- name: project
|
||||||
|
request:
|
||||||
|
api: https://gitlab.com/api/v4/projects/{{int64 (index .projects 0).id}}
|
||||||
|
expect:
|
||||||
|
statusCode: 200
|
||||||
|
# bodyFieldsExpect:
|
||||||
|
# http_url_to_repo: https://gitlab.com/senghuy/sr_chea_senghuy_spring_homework001.git
|
||||||
|
verify:
|
||||||
|
- http_url_to_repo startsWith "https"
|
||||||
|
- http_url_to_repo endsWith ".git"
|
||||||
|
- default_branch == 'master' or default_branch == 'main'
|
||||||
|
- len(topics) >= 0
|
Loading…
Reference in New Issue