Skip to content

Commit 3ae7518

Browse files
authored
feat: add normal form request support (#9)
Co-authored-by: rick <[email protected]>
1 parent e1270c7 commit 3ae7518

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
lines changed

pkg/runner/simple.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"mime/multipart"
99
"net/http"
10+
"net/url"
1011
"os"
1112
"reflect"
1213
"strings"
@@ -62,6 +63,12 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
6263
_ = writer.Close()
6364
requestBody = multiBody
6465
testcase.Request.Header["Content-Type"] = writer.FormDataContentType()
66+
} else if testcase.Request.Header["Content-Type"] == "application/x-www-form-urlencoded" {
67+
data := url.Values{}
68+
for key, val := range testcase.Request.Form {
69+
data.Set(key, val)
70+
}
71+
requestBody = strings.NewReader(data.Encode())
6572
}
6673
}
6774

@@ -140,6 +147,11 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
140147
err = fmt.Errorf("not found field: %s", key)
141148
return
142149
} else if !reflect.DeepEqual(expectVal, val) {
150+
if reflect.TypeOf(expectVal).Kind() == reflect.Int {
151+
if strings.Compare(fmt.Sprintf("%v", expectVal), fmt.Sprintf("%v", val)) == 0 {
152+
continue
153+
}
154+
}
143155
err = fmt.Errorf("field[%s] expect value: %v, actual: %v", key, expectVal, val)
144156
return
145157
}

pkg/runner/simple_test.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func TestTestCase(t *testing.T) {
3131
},
3232
Expect: atest.Response{
3333
StatusCode: http.StatusOK,
34-
BodyFieldsExpect: map[string]string{
35-
"name": "linuxsuren",
34+
BodyFieldsExpect: map[string]interface{}{
35+
"name": "linuxsuren",
36+
"number": 1,
3637
},
3738
Header: map[string]string{
3839
"type": "generic",
@@ -52,7 +53,7 @@ func TestTestCase(t *testing.T) {
5253
},
5354
verify: func(t *testing.T, output interface{}, err error) {
5455
assert.Nil(t, err)
55-
assert.Equal(t, map[string]interface{}{"name": "linuxsuren"}, output)
56+
assert.Equal(t, map[string]interface{}{"name": "linuxsuren", "number": float64(1)}, output)
5657
},
5758
}, {
5859
name: "normal, response is slice",
@@ -181,7 +182,7 @@ func TestTestCase(t *testing.T) {
181182
API: "http://localhost/foo",
182183
},
183184
Expect: atest.Response{
184-
BodyFieldsExpect: map[string]string{
185+
BodyFieldsExpect: map[string]interface{}{
185186
"foo": "bar",
186187
},
187188
},
@@ -200,7 +201,7 @@ func TestTestCase(t *testing.T) {
200201
API: "http://localhost/foo",
201202
},
202203
Expect: atest.Response{
203-
BodyFieldsExpect: map[string]string{
204+
BodyFieldsExpect: map[string]interface{}{
204205
"name": "bar",
205206
},
206207
},
@@ -219,7 +220,7 @@ func TestTestCase(t *testing.T) {
219220
API: "http://localhost/foo",
220221
},
221222
Expect: atest.Response{
222-
BodyFieldsExpect: map[string]string{
223+
BodyFieldsExpect: map[string]interface{}{
223224
"items[1]": "bar",
224225
},
225226
},
@@ -316,7 +317,7 @@ func TestTestCase(t *testing.T) {
316317
assert.Contains(t, err.Error(), "template: api:1:")
317318
},
318319
}, {
319-
name: "form request",
320+
name: "multipart form request",
320321
testCase: &atest.TestCase{
321322
Request: atest.Request{
322323
API: "http://localhost/foo",
@@ -336,6 +337,27 @@ func TestTestCase(t *testing.T) {
336337
verify: func(t *testing.T, output interface{}, err error) {
337338
assert.Nil(t, err)
338339
},
340+
}, {
341+
name: "normal form request",
342+
testCase: &atest.TestCase{
343+
Request: atest.Request{
344+
API: "http://localhost/foo",
345+
Method: http.MethodPost,
346+
Header: map[string]string{
347+
"Content-Type": "application/x-www-form-urlencoded",
348+
},
349+
Form: map[string]string{
350+
"key": "value",
351+
},
352+
},
353+
},
354+
prepare: func() {
355+
gock.New("http://localhost").
356+
Post("/foo").Reply(http.StatusOK).BodyString(`{"items":[]}`)
357+
},
358+
verify: func(t *testing.T, output interface{}, err error) {
359+
assert.Nil(t, err)
360+
},
339361
}}
340362
for _, tt := range tests {
341363
t.Run(tt.name, func(t *testing.T) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"name": "linuxsuren"
2+
"name": "linuxsuren",
3+
"number": 1
34
}

pkg/testing/case.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ type Request struct {
3434

3535
// Response is the expected response
3636
type Response struct {
37-
StatusCode int `yaml:"statusCode"`
38-
Body string `yaml:"body"`
39-
Header map[string]string `yaml:"header"`
40-
BodyFieldsExpect map[string]string `yaml:"bodyFieldsExpect"`
41-
Verify []string `yaml:"verify"`
37+
StatusCode int `yaml:"statusCode"`
38+
Body string `yaml:"body"`
39+
Header map[string]string `yaml:"header"`
40+
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect"`
41+
Verify []string `yaml:"verify"`
4242
}
4343

4444
// Clean represents the clean work after testing

pkg/testing/parser.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ func (r *Request) Render(ctx interface{}) (err error) {
4242
r.Body = strings.TrimSpace(string(data))
4343
}
4444

45+
// template the header
46+
for key, val := range r.Header {
47+
if tpl, err = template.New("header").Funcs(sprig.FuncMap()).Parse(val); err == nil {
48+
buf = new(bytes.Buffer)
49+
if err = tpl.Execute(buf, ctx); err == nil {
50+
r.Header[key] = buf.String()
51+
}
52+
}
53+
}
54+
4555
// template the body
4656
if tpl, err = template.New("body").Funcs(sprig.FuncMap()).Parse(r.Body); err == nil {
4757
buf = new(bytes.Buffer)

pkg/testing/parser_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ func TestRender(t *testing.T) {
104104
assert.Equal(t, "linuxsuren", req.Form["key"])
105105
},
106106
hasErr: false,
107+
}, {
108+
name: "header render",
109+
request: &Request{
110+
Header: map[string]string{
111+
"key": "{{.Name}}",
112+
},
113+
},
114+
ctx: TestCase{Name: "linuxsuren"},
115+
verify: func(t *testing.T, req *Request) {
116+
assert.Equal(t, "linuxsuren", req.Header["key"])
117+
},
118+
hasErr: false,
107119
}}
108120
for _, tt := range tests {
109121
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)