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
12 changes: 12 additions & 0 deletions pkg/runner/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -62,6 +63,12 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
_ = writer.Close()
requestBody = multiBody
testcase.Request.Header["Content-Type"] = writer.FormDataContentType()
} else if testcase.Request.Header["Content-Type"] == "application/x-www-form-urlencoded" {
data := url.Values{}
for key, val := range testcase.Request.Form {
data.Set(key, val)
}
requestBody = strings.NewReader(data.Encode())
}
}

Expand Down Expand Up @@ -140,6 +147,11 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
err = fmt.Errorf("not found field: %s", key)
return
} else if !reflect.DeepEqual(expectVal, val) {
if reflect.TypeOf(expectVal).Kind() == reflect.Int {
if strings.Compare(fmt.Sprintf("%v", expectVal), fmt.Sprintf("%v", val)) == 0 {
continue
}
}
err = fmt.Errorf("field[%s] expect value: %v, actual: %v", key, expectVal, val)
return
}
Expand Down
36 changes: 29 additions & 7 deletions pkg/runner/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func TestTestCase(t *testing.T) {
},
Expect: atest.Response{
StatusCode: http.StatusOK,
BodyFieldsExpect: map[string]string{
"name": "linuxsuren",
BodyFieldsExpect: map[string]interface{}{
"name": "linuxsuren",
"number": 1,
},
Header: map[string]string{
"type": "generic",
Expand All @@ -52,7 +53,7 @@ func TestTestCase(t *testing.T) {
},
verify: func(t *testing.T, output interface{}, err error) {
assert.Nil(t, err)
assert.Equal(t, map[string]interface{}{"name": "linuxsuren"}, output)
assert.Equal(t, map[string]interface{}{"name": "linuxsuren", "number": float64(1)}, output)
},
}, {
name: "normal, response is slice",
Expand Down Expand Up @@ -181,7 +182,7 @@ func TestTestCase(t *testing.T) {
API: "http://localhost/foo",
},
Expect: atest.Response{
BodyFieldsExpect: map[string]string{
BodyFieldsExpect: map[string]interface{}{
"foo": "bar",
},
},
Expand All @@ -200,7 +201,7 @@ func TestTestCase(t *testing.T) {
API: "http://localhost/foo",
},
Expect: atest.Response{
BodyFieldsExpect: map[string]string{
BodyFieldsExpect: map[string]interface{}{
"name": "bar",
},
},
Expand All @@ -219,7 +220,7 @@ func TestTestCase(t *testing.T) {
API: "http://localhost/foo",
},
Expect: atest.Response{
BodyFieldsExpect: map[string]string{
BodyFieldsExpect: map[string]interface{}{
"items[1]": "bar",
},
},
Expand Down Expand Up @@ -316,7 +317,7 @@ func TestTestCase(t *testing.T) {
assert.Contains(t, err.Error(), "template: api:1:")
},
}, {
name: "form request",
name: "multipart form request",
testCase: &atest.TestCase{
Request: atest.Request{
API: "http://localhost/foo",
Expand All @@ -336,6 +337,27 @@ func TestTestCase(t *testing.T) {
verify: func(t *testing.T, output interface{}, err error) {
assert.Nil(t, err)
},
}, {
name: "normal form request",
testCase: &atest.TestCase{
Request: atest.Request{
API: "http://localhost/foo",
Method: http.MethodPost,
Header: map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
},
Form: map[string]string{
"key": "value",
},
},
},
prepare: func() {
gock.New("http://localhost").
Post("/foo").Reply(http.StatusOK).BodyString(`{"items":[]}`)
},
verify: func(t *testing.T, output interface{}, err error) {
assert.Nil(t, err)
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/runner/testdata/generic_response.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"name": "linuxsuren"
"name": "linuxsuren",
"number": 1
}
10 changes: 5 additions & 5 deletions pkg/testing/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ type Request struct {

// 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]string `yaml:"bodyFieldsExpect"`
Verify []string `yaml:"verify"`
StatusCode int `yaml:"statusCode"`
Body string `yaml:"body"`
Header map[string]string `yaml:"header"`
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect"`
Verify []string `yaml:"verify"`
}

// Clean represents the clean work after testing
Expand Down
10 changes: 10 additions & 0 deletions pkg/testing/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func (r *Request) Render(ctx interface{}) (err error) {
r.Body = strings.TrimSpace(string(data))
}

// template the header
for key, val := range r.Header {
if tpl, err = template.New("header").Funcs(sprig.FuncMap()).Parse(val); err == nil {
buf = new(bytes.Buffer)
if err = tpl.Execute(buf, ctx); err == nil {
r.Header[key] = buf.String()
}
}
}

// template the body
if tpl, err = template.New("body").Funcs(sprig.FuncMap()).Parse(r.Body); err == nil {
buf = new(bytes.Buffer)
Expand Down
12 changes: 12 additions & 0 deletions pkg/testing/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ func TestRender(t *testing.T) {
assert.Equal(t, "linuxsuren", req.Form["key"])
},
hasErr: false,
}, {
name: "header render",
request: &Request{
Header: map[string]string{
"key": "{{.Name}}",
},
},
ctx: TestCase{Name: "linuxsuren"},
verify: func(t *testing.T, req *Request) {
assert.Equal(t, "linuxsuren", req.Header["key"])
},
hasErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down