Skip to content

Commit 7876445

Browse files
committed
feat(enhancement): add SetContentType helper method
1 parent 1ce39e1 commit 7876445

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

Diff for: multipart_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ func TestMultipartLargeFile(t *testing.T) {
353353
c := dcnl()
354354
resp, err := c.R().
355355
SetFile("file", filepath.Join(getTestDataPath(), "test-img.png")).
356-
SetMultipartBoundary("custom-boundary-"+strconv.FormatInt(time.Now().Unix(), 10)).
357-
SetHeader("Content-Type", "image/png").
356+
SetMultipartBoundary("custom-boundary-" + strconv.FormatInt(time.Now().Unix(), 10)).
357+
SetContentType("image/png").
358358
Post(ts.URL + "/upload")
359359
assertNil(t, err)
360360
assertNotNil(t, resp)
@@ -365,8 +365,8 @@ func TestMultipartLargeFile(t *testing.T) {
365365
c := dcnl()
366366
_, err := c.R().
367367
SetFile("file", filepath.Join(getTestDataPath(), "test-img.png")).
368-
SetMultipartBoundary(`"custom-boundary-"`+strconv.FormatInt(time.Now().Unix(), 10)).
369-
SetHeader("Content-Type", "image/png").
368+
SetMultipartBoundary(`"custom-boundary-"` + strconv.FormatInt(time.Now().Unix(), 10)).
369+
SetContentType("image/png").
370370
Post(ts.URL + "/upload")
371371
assertNotNil(t, err)
372372
assertEqual(t, "mime: invalid boundary character", err.Error())
@@ -616,8 +616,8 @@ func TestMultipartRequest_createMultipart(t *testing.T) {
616616

617617
req1 := c.R().
618618
SetFile("file", filepath.Join(getTestDataPath(), "test-img.png")).
619-
SetMultipartBoundary("custom-boundary-"+strconv.FormatInt(time.Now().Unix(), 10)).
620-
SetHeader("Content-Type", "image/png")
619+
SetMultipartBoundary("custom-boundary-" + strconv.FormatInt(time.Now().Unix(), 10)).
620+
SetContentType("image/png")
621621

622622
mw := multipart.NewWriter(new(bytes.Buffer))
623623
err := createMultipart(mw, req1)
@@ -638,7 +638,7 @@ func TestMultipartRequest_createMultipart(t *testing.T) {
638638

639639
req1 := c.R().
640640
SetFile("file", filepath.Join(getTestDataPath(), "test-img.png")).
641-
SetHeader("Content-Type", "image/png")
641+
SetContentType("image/png")
642642

643643
mw := multipart.NewWriter(new(bytes.Buffer))
644644
err := createMultipart(mw, req1)

Diff for: request.go

+8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ func (r *Request) WithContext(ctx context.Context) *Request {
158158
return rr
159159
}
160160

161+
// SetContentType method is a convenient way to set the header Content-Type in the request
162+
//
163+
// client.R().SetContentType("application/json")
164+
func (r *Request) SetContentType(ct string) *Request {
165+
r.SetHeader(hdrContentTypeKey, ct)
166+
return r
167+
}
168+
161169
// SetHeader method sets a single header field and its value in the current request.
162170
//
163171
// For Example: To set `Content-Type` and `Accept` as `application/json`.

Diff for: request_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ func TestRequestFileUploadAsReader(t *testing.T) {
15941594

15951595
resp, err := dcnldr().
15961596
SetBody(file).
1597-
SetHeader("Content-Type", "image/png").
1597+
SetContentType("image/png").
15981598
Post(ts.URL + "/upload")
15991599

16001600
assertError(t, err)
@@ -1606,7 +1606,7 @@ func TestRequestFileUploadAsReader(t *testing.T) {
16061606

16071607
resp, err = dcnldr().
16081608
SetBody(file).
1609-
SetHeader("Content-Type", "image/png").
1609+
SetContentType("image/png").
16101610
SetContentLength(true).
16111611
Post(ts.URL + "/upload")
16121612

0 commit comments

Comments
 (0)