Browse code

If `Content-Type` is `application/json;charset=UTF-8`, `RequestBody` is empty. Signed-off-by: odg0318 <odg0318@gmail.com>

odg0318 authored on 2018/04/04 17:04:50
Showing 2 changed files
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"bytes"
6 6
 	"fmt"
7 7
 	"io"
8
+	"mime"
8 9
 	"net/http"
9 10
 	"strings"
10 11
 
... ...
@@ -153,7 +154,12 @@ func sendBody(url string, header http.Header) bool {
153 153
 	}
154 154
 
155 155
 	// body is sent only for text or json messages
156
-	return header.Get("Content-Type") == "application/json"
156
+	contentType, _, err := mime.ParseMediaType(header.Get("Content-Type"))
157
+	if err != nil {
158
+		return false
159
+	}
160
+
161
+	return contentType == "application/json"
157 162
 }
158 163
 
159 164
 // headers returns flatten version of the http headers excluding authorization
... ...
@@ -172,6 +172,66 @@ func TestDrainBody(t *testing.T) {
172 172
 	}
173 173
 }
174 174
 
175
+func TestSendBody(t *testing.T) {
176
+	var (
177
+		url       = "nothing.com"
178
+		testcases = []struct {
179
+			contentType string
180
+			expected    bool
181
+		}{
182
+			{
183
+				contentType: "application/json",
184
+				expected:    true,
185
+			},
186
+			{
187
+				contentType: "Application/json",
188
+				expected:    true,
189
+			},
190
+			{
191
+				contentType: "application/JSON",
192
+				expected:    true,
193
+			},
194
+			{
195
+				contentType: "APPLICATION/JSON",
196
+				expected:    true,
197
+			},
198
+			{
199
+				contentType: "application/json; charset=utf-8",
200
+				expected:    true,
201
+			},
202
+			{
203
+				contentType: "application/json;charset=utf-8",
204
+				expected:    true,
205
+			},
206
+			{
207
+				contentType: "application/json; charset=UTF8",
208
+				expected:    true,
209
+			},
210
+			{
211
+				contentType: "application/json;charset=UTF8",
212
+				expected:    true,
213
+			},
214
+			{
215
+				contentType: "text/html",
216
+				expected:    false,
217
+			},
218
+			{
219
+				contentType: "",
220
+				expected:    false,
221
+			},
222
+		}
223
+	)
224
+
225
+	for _, testcase := range testcases {
226
+		header := http.Header{}
227
+		header.Set("Content-Type", testcase.contentType)
228
+
229
+		if b := sendBody(url, header); b != testcase.expected {
230
+			t.Fatalf("Unexpected Content-Type; Expected: %t, Actual: %t", testcase.expected, b)
231
+		}
232
+	}
233
+}
234
+
175 235
 func TestResponseModifierOverride(t *testing.T) {
176 236
 	r := httptest.NewRecorder()
177 237
 	m := NewResponseModifier(r)