feat: add normal form request support (#9)

Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
Rick 2023-03-08 20:43:44 +08:00 committed by GitHub
parent e1270c748e
commit 3ae751826d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import (
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"reflect"
"strings"
@ -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())
}
}
@ -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
}

View File

@ -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",
@ -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",
@ -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",
},
},
@ -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",
},
},
@ -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",
},
},
@ -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",
@ -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) {

View File

@ -1,3 +1,4 @@
{
"name": "linuxsuren"
"name": "linuxsuren",
"number": 1
}

View File

@ -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

View File

@ -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)

View File

@ -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) {