diff --git a/pkg/runner/simple.go b/pkg/runner/simple.go index 6fbf5c91..cfb7b4b6 100644 --- a/pkg/runner/simple.go +++ b/pkg/runner/simple.go @@ -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 } diff --git a/pkg/runner/simple_test.go b/pkg/runner/simple_test.go index c7bbdb3e..fa5f8413 100644 --- a/pkg/runner/simple_test.go +++ b/pkg/runner/simple_test.go @@ -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) { diff --git a/pkg/runner/testdata/generic_response.json b/pkg/runner/testdata/generic_response.json index 0c73bba4..49837ec0 100644 --- a/pkg/runner/testdata/generic_response.json +++ b/pkg/runner/testdata/generic_response.json @@ -1,3 +1,4 @@ { - "name": "linuxsuren" + "name": "linuxsuren", + "number": 1 } diff --git a/pkg/testing/case.go b/pkg/testing/case.go index 4ff287f7..869d48da 100644 --- a/pkg/testing/case.go +++ b/pkg/testing/case.go @@ -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 diff --git a/pkg/testing/parser.go b/pkg/testing/parser.go index 8b49f68a..055f8b4d 100644 --- a/pkg/testing/parser.go +++ b/pkg/testing/parser.go @@ -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) diff --git a/pkg/testing/parser_test.go b/pkg/testing/parser_test.go index 69ca9969..51ceb6fe 100644 --- a/pkg/testing/parser_test.go +++ b/pkg/testing/parser_test.go @@ -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) {