Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions cmd/jsonschema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"encoding/json"

"github.com/invopop/jsonschema"
"github.com/linuxsuren/api-testing/pkg/testing"
"github.com/spf13/cobra"
)

func createJSONSchemaCmd() (c *cobra.Command) {
c = &cobra.Command{
Use: "json",
Short: "Print the JSON schema of the test suites struct",
RunE: func(cmd *cobra.Command, args []string) (err error) {
var data []byte
schema := jsonschema.Reflect(&testing.TestSuite{})
if data, err = json.MarshalIndent(schema, "", " "); err == nil {
cmd.Println(string(data))
}
return
},
}
return
}
22 changes: 22 additions & 0 deletions cmd/jsonschema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd_test

import (
"bytes"
"strings"
"testing"

"github.com/linuxsuren/api-testing/cmd"
"github.com/stretchr/testify/assert"
)

func TestJSONSchemaCmd(t *testing.T) {
c := cmd.NewRootCmd()

buf := new(bytes.Buffer)
c.SetOut(buf)

c.SetArgs([]string{"json"})
err := c.Execute()
assert.Nil(t, err)
assert.True(t, strings.Contains(buf.String(), "schema"))
}
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/linuxsuren/api-testing/pkg/version"
"github.com/spf13/cobra"
)
Expand All @@ -11,9 +13,10 @@ func NewRootCmd() (c *cobra.Command) {
Use: "atest",
Short: "API testing tool",
}
c.SetOut(os.Stdout)
c.Version = version.GetVersion()
c.AddCommand(createInitCommand(),
createRunCommand(), createSampleCmd(),
createServerCmd())
createServerCmd(), createJSONSchemaCmd())
return
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/invopop/jsonschema v0.7.0 // 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
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,17 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
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/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
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.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So=
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down Expand Up @@ -598,6 +602,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
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.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
40 changes: 20 additions & 20 deletions pkg/testing/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package testing

// TestSuite represents a set of test cases
type TestSuite struct {
Name string `yaml:"name"`
API string `yaml:"api"`
Items []TestCase `yaml:"items"`
Name string `yaml:"name" json:"name"`
API string `yaml:"api,omitempty" json:"api,omitempty"`
Items []TestCase `yaml:"items" json:"items"`
}

// TestCase represents a test case
type TestCase struct {
Name string
Name string `yaml:"name" json:"name"`
Group string
Prepare Prepare `yaml:"prepare"`
Request Request `yaml:"request"`
Expect Response `yaml:"expect"`
Clean Clean `yaml:"clean"`
Prepare Prepare `yaml:"prepare" json:"-"`
Request Request `yaml:"request" json:"request"`
Expect Response `yaml:"expect" json:"expect"`
Clean Clean `yaml:"clean" json:"-"`
}

// Prepare does the prepare work
Expand All @@ -24,22 +24,22 @@ type Prepare struct {

// Request represents a HTTP request
type Request struct {
API string `yaml:"api"`
Method string `yaml:"method"`
Query map[string]string `yaml:"query"`
Header map[string]string `yaml:"header"`
Form map[string]string `yaml:"form"`
Body string `yaml:"body"`
BodyFromFile string `yaml:"bodyFromFile"`
API string `yaml:"api" json:"api"`
Method string `yaml:"method,omitempty" json:"method,omitempty" jsonschema:"enum=GET,enum=POST,enum=PUT,enum=DELETE"`
Query map[string]string `yaml:"query" json:"query,omitempty"`
Header map[string]string `yaml:"header" json:"header,omitempty"`
Form map[string]string `yaml:"form" json:"form,omitempty"`
Body string `yaml:"body" json:"body,omitempty"`
BodyFromFile string `yaml:"bodyFromFile" json:"bodyFromFile,omitempty"`
}

// Response is the expected response
type Response struct {
StatusCode int `yaml:"statusCode"`
Body string `yaml:"body"`
Header map[string]string `yaml:"header"`
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect"`
Verify []string `yaml:"verify"`
StatusCode int `yaml:"statusCode" json:"bodyFromFile,omitempty"`
Body string `yaml:"body" json:"body,omitempty"`
Header map[string]string `yaml:"header" json:"header,omitempty"`
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect" json:"bodyFieldsExpect,omitempty"`
Verify []string `yaml:"verify" json:"verify,omitempty"`
}

// Clean represents the clean work after testing
Expand Down