Skip to content

Commit a021dc5

Browse files
committed
Add tests for multipart/form-data requests
1 parent ca8a163 commit a021dc5

File tree

1 file changed

+72
-18
lines changed

1 file changed

+72
-18
lines changed

pkg/protocol/http_test.go

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package protocol
22

33
import (
44
"bytes"
5+
"io"
6+
"mime/multipart"
57
"net/http"
68
"net/http/httptest"
9+
"regexp"
10+
"strings"
711
"testing"
812
)
913

@@ -36,6 +40,7 @@ func TestLogRequestOneRenderer(t *testing.T) {
3640
method string
3741
path string
3842
body string
43+
params map[string]string
3944
expectedParams string
4045
headerKey string
4146
headerValue string
@@ -44,6 +49,7 @@ func TestLogRequestOneRenderer(t *testing.T) {
4449
http.MethodGet,
4550
"/foo",
4651
"",
52+
nil,
4753
"",
4854
"Foo",
4955
"Bar",
@@ -52,14 +58,25 @@ func TestLogRequestOneRenderer(t *testing.T) {
5258
http.MethodPost,
5359
"/foo/bar",
5460
"{\"foo\": \"bar\"}",
61+
nil,
5562
"{\"foo\" => \"bar\"}",
5663
"Content-Type",
5764
"application/json",
5865
},
66+
{
67+
http.MethodPost,
68+
"/form/data",
69+
"",
70+
map[string]string{"aloha": "friday"},
71+
"{\"aloha\" => \"friday\"}",
72+
"Content-Type",
73+
"multipart/form-data",
74+
},
5975
{
6076
http.MethodDelete,
6177
"/foo/1",
6278
"",
79+
nil,
6380
"",
6481
"Bearer",
6582
"hello!",
@@ -68,6 +85,7 @@ func TestLogRequestOneRenderer(t *testing.T) {
6885
http.MethodGet,
6986
"/foo/bar?hello=world",
7087
"",
88+
nil,
7189
"{\"hello\" => \"world\"}",
7290
"Content-Type",
7391
"application/json!",
@@ -80,23 +98,57 @@ func TestLogRequestOneRenderer(t *testing.T) {
8098
defer srv.Close()
8199

82100
for _, test := range testTable {
83-
b := bytes.NewBuffer([]byte(test.body))
84-
req, err := http.NewRequest(test.method, srv.URL+test.path, b)
85-
86-
if test.headerKey != "" {
87-
req.Header.Set(test.headerKey, test.headerValue)
101+
r, _ := regexp.Compile(`multipart\/form-data`)
102+
matched := r.MatchString(test.headerValue)
103+
if test.headerKey == "Content-Type" && matched {
104+
b := &bytes.Buffer{}
105+
writer := multipart.NewWriter(b)
106+
for k := range test.params {
107+
fw, err := writer.CreateFormField(k)
108+
if err != nil {
109+
t.Errorf("Error POST to httptest server")
110+
}
111+
112+
_, err = io.Copy(fw, strings.NewReader(test.params[k]))
113+
if err != nil {
114+
t.Errorf("Error POST to httptest server")
115+
}
116+
}
117+
writer.Close()
118+
req, err := http.NewRequest(test.method, srv.URL+test.path, bytes.NewReader(b.Bytes()))
119+
120+
if err != nil {
121+
t.Error(err)
122+
}
123+
124+
req.Header.Set("Content-Type", writer.FormDataContentType())
125+
resp, err := http.DefaultClient.Do(req)
126+
if err != nil {
127+
t.Error(err)
128+
}
129+
if resp.StatusCode != http.StatusOK {
130+
t.Errorf("Request failed with response code: %d", resp.StatusCode)
131+
}
132+
resp.Body.Close()
133+
} else {
134+
b := bytes.NewBuffer([]byte(test.body))
135+
req, err := http.NewRequest(test.method, srv.URL+test.path, b)
136+
137+
if test.headerKey != "" {
138+
req.Header.Set(test.headerKey, test.headerValue)
139+
}
140+
141+
if err != nil {
142+
t.Error(err)
143+
}
144+
145+
resp, err := http.DefaultClient.Do(req)
146+
if err != nil {
147+
t.Error(err)
148+
}
149+
resp.Body.Close()
88150
}
89151

90-
if err != nil {
91-
t.Error(err)
92-
}
93-
94-
resp, err := http.DefaultClient.Do(req)
95-
if err != nil {
96-
t.Error(err)
97-
}
98-
resp.Body.Close()
99-
100152
rp := <-rpChannel
101153

102154
if rp.Fields.Method != test.method {
@@ -111,9 +163,11 @@ func TestLogRequestOneRenderer(t *testing.T) {
111163
t.Errorf("Expected %s, got %s", test.expectedParams, rp.Message)
112164
}
113165

114-
expectedHeaderValue := rp.Headers[test.headerKey][0]
115-
if expectedHeaderValue != test.headerValue {
116-
t.Errorf("Expected %s, got %s", expectedHeaderValue, test.headerValue)
166+
headerValue := rp.Headers[test.headerKey][0]
167+
r, _ = regexp.Compile(test.headerValue)
168+
matched = r.MatchString(headerValue)
169+
if !matched {
170+
t.Errorf("Expected %s, got %s", test.headerValue, headerValue)
117171
}
118172
}
119173
}

0 commit comments

Comments
 (0)