Skip to content

Commit e1270c7

Browse files
authored
feat: support form request (#8)
Co-authored-by: rick <[email protected]>
1 parent d07c7f2 commit e1270c7

File tree

8 files changed

+73
-11
lines changed

8 files changed

+73
-11
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
build:
22
mkdir -p bin
3-
go build -o bin/atest cmd/*.go
3+
rm -rf bin/atest
4+
go build -o bin/atest main.go
45

56
copy: build
67
cp bin/atest /usr/local/bin/

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ module github.com/linuxsuren/api-testing
33
go 1.17
44

55
require (
6+
github.com/Masterminds/sprig/v3 v3.2.3
67
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
8+
github.com/antonmedv/expr v1.12.1
9+
github.com/h2non/gock v1.2.0
10+
github.com/linuxsuren/unstructured v0.0.1
711
github.com/spf13/cobra v1.4.0
812
github.com/stretchr/testify v1.8.2
913
gopkg.in/yaml.v2 v2.4.0
@@ -12,16 +16,12 @@ require (
1216
require (
1317
github.com/Masterminds/goutils v1.1.1 // indirect
1418
github.com/Masterminds/semver/v3 v3.2.0 // indirect
15-
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
16-
github.com/antonmedv/expr v1.12.1 // indirect
1719
github.com/davecgh/go-spew v1.1.1 // indirect
1820
github.com/google/uuid v1.1.1 // indirect
19-
github.com/h2non/gock v1.2.0 // indirect
2021
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
2122
github.com/huandu/xstrings v1.3.3 // indirect
2223
github.com/imdario/mergo v0.3.11 // indirect
2324
github.com/inconshreveable/mousetrap v1.0.0 // indirect
24-
github.com/linuxsuren/unstructured v0.0.1 // indirect
2525
github.com/mitchellh/copystructure v1.0.0 // indirect
2626
github.com/mitchellh/reflectwalk v1.0.0 // indirect
2727
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMK
3535
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
3636
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
3737
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
38+
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
3839
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
3940
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4041
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -53,9 +54,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
5354
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
5455
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
5556
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
56-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
5757
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
58-
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
5958
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
6059
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6160
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=

pkg/runner/simple.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8+
"mime/multipart"
89
"net/http"
910
"os"
1011
"reflect"
@@ -50,6 +51,20 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
5051
return
5152
}
5253

54+
if len(testcase.Request.Form) > 0 {
55+
if testcase.Request.Header["Content-Type"] == "multipart/form-data" {
56+
multiBody := &bytes.Buffer{}
57+
writer := multipart.NewWriter(multiBody)
58+
for key, val := range testcase.Request.Form {
59+
writer.WriteField(key, val)
60+
}
61+
62+
_ = writer.Close()
63+
requestBody = multiBody
64+
testcase.Request.Header["Content-Type"] = writer.FormDataContentType()
65+
}
66+
}
67+
5368
var request *http.Request
5469
if request, err = http.NewRequest(testcase.Request.Method, testcase.Request.API, requestBody); err != nil {
5570
return
@@ -68,8 +83,14 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
6883
return
6984
}
7085

86+
var responseBodyData []byte
87+
if responseBodyData, err = io.ReadAll(resp.Body); err != nil {
88+
return
89+
}
90+
7191
if testcase.Expect.StatusCode != 0 {
7292
if err = expectInt(testcase.Name, testcase.Expect.StatusCode, resp.StatusCode); err != nil {
93+
err = fmt.Errorf("error is: %v\n%s", err, string(responseBodyData))
7394
return
7495
}
7596
}
@@ -81,10 +102,6 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
81102
}
82103
}
83104

84-
var responseBodyData []byte
85-
if responseBodyData, err = io.ReadAll(resp.Body); err != nil {
86-
return
87-
}
88105
if testcase.Expect.Body != "" {
89106
if string(responseBodyData) != strings.TrimSpace(testcase.Expect.Body) {
90107
err = fmt.Errorf("case: %s, got different response body, diff: \n%s", testcase.Name,

pkg/runner/simple_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
_ "embed"
9+
910
"github.com/h2non/gock"
1011
atest "github.com/linuxsuren/api-testing/pkg/testing"
1112
"github.com/stretchr/testify/assert"
@@ -314,6 +315,27 @@ func TestTestCase(t *testing.T) {
314315
assert.NotNil(t, err)
315316
assert.Contains(t, err.Error(), "template: api:1:")
316317
},
318+
}, {
319+
name: "form request",
320+
testCase: &atest.TestCase{
321+
Request: atest.Request{
322+
API: "http://localhost/foo",
323+
Method: http.MethodPost,
324+
Header: map[string]string{
325+
"Content-Type": "multipart/form-data",
326+
},
327+
Form: map[string]string{
328+
"key": "value",
329+
},
330+
},
331+
},
332+
prepare: func() {
333+
gock.New("http://localhost").
334+
Post("/foo").Reply(http.StatusOK).BodyString(`{"items":[]}`)
335+
},
336+
verify: func(t *testing.T, output interface{}, err error) {
337+
assert.Nil(t, err)
338+
},
317339
}}
318340
for _, tt := range tests {
319341
t.Run(tt.name, func(t *testing.T) {

pkg/testing/case.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Request struct {
2727
Method string `yaml:"method"`
2828
Query map[string]string `yaml:"query"`
2929
Header map[string]string `yaml:"header"`
30+
Form map[string]string `yaml:"form"`
3031
Body string `yaml:"body"`
3132
BodyFromFile string `yaml:"bodyFromFile"`
3233
}

pkg/testing/parser.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,15 @@ func (r *Request) Render(ctx interface{}) (err error) {
4949
r.Body = buf.String()
5050
}
5151
}
52+
53+
// template the form
54+
for key, val := range r.Form {
55+
if tpl, err = template.New("form").Funcs(sprig.FuncMap()).Parse(val); err == nil {
56+
buf = new(bytes.Buffer)
57+
if err = tpl.Execute(buf, ctx); err == nil {
58+
r.Form[key] = buf.String()
59+
}
60+
}
61+
}
5262
return
5363
}

pkg/testing/parser_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ func TestRender(t *testing.T) {
9292
},
9393
ctx: TestCase{},
9494
hasErr: true,
95+
}, {
96+
name: "form render",
97+
request: &Request{
98+
Form: map[string]string{
99+
"key": "{{.Name}}",
100+
},
101+
},
102+
ctx: TestCase{Name: "linuxsuren"},
103+
verify: func(t *testing.T, req *Request) {
104+
assert.Equal(t, "linuxsuren", req.Form["key"])
105+
},
106+
hasErr: false,
95107
}}
96108
for _, tt := range tests {
97109
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)