Skip to content

Commit

Permalink
Merge pull request parnurzeal#165 from harsimranmaan/infer_header
Browse files Browse the repository at this point in the history
Don't infer Content-Type when set explicitly
  • Loading branch information
parnurzeal authored Oct 10, 2017
2 parents a4fdb89 + faf2e78 commit 219b5e0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 87 deletions.
4 changes: 3 additions & 1 deletion gorequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,9 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) {
}
}

if len(contentType) != 0 {
// https://github.com/parnurzeal/gorequest/issues/164
// Don't infer the content type header if an overrride is already provided.
if len(contentType) != 0 && req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", contentType)
}

Expand Down
135 changes: 49 additions & 86 deletions gorequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1804,92 +1804,55 @@ func TestPlainText(t *testing.T) {
End()
}

// Test for force type to plain text even the request has specific Content-Type header.
func TestForceTypeToPlainText(t *testing.T) {
text := `hello world \r\n I am GoRequest`

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check method is PATCH before going to check other features
if r.Method != POST {
t.Errorf("Expected method %q; got %q", POST, r.Method)
}
if r.Header == nil {
t.Error("Expected non-nil request Header")
}
if r.Header.Get("Content-Type") != "text/plain" {
t.Error("Expected Header Content-Type -> text/plain", "| but got", r.Header.Get("Content-Type"))
}

defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
if string(body) != text {
t.Error("Expected text ", text, "| but got", string(body))
}
}))

defer ts.Close()

New().Post(ts.URL).
Set("Content-Type", "text/plain").
Type("text").
Send(text).
End()

New().Post(ts.URL).
Set("Content-Type", "application/json").
Type("text").
Send(text).
End()

New().Post(ts.URL).
Type("text").
Set("Content-Type", "application/json").
Send(text).
End()
}

// Test for force type to JSON even the request has specific Content-Type header.
func TestForceTypeToJSON(t *testing.T) {
jsonData := `{"data":"hello world \r\n I am GoRequest"}`

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check method is PATCH before going to check other features
if r.Method != POST {
t.Errorf("Expected method %q; got %q", POST, r.Method)
}
if r.Header == nil {
t.Error("Expected non-nil request Header")
}
if r.Header.Get("Content-Type") != "application/json" {
t.Error("Expected Header Content-Type -> application/json", "| but got", r.Header.Get("Content-Type"))
}

defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
if string(body) != jsonData {
t.Error("Expected JSON ", jsonData, "| but got", string(body))
}
}))

defer ts.Close()

New().Post(ts.URL).
Set("Content-Type", "application/json").
Type("json").
Send(jsonData).
End()

New().Post(ts.URL).
Set("Content-Type", "text/plain").
Type("json").
Send(jsonData).
End()

New().Post(ts.URL).
Type("json").
Set("Content-Type", "text/plain").
Send(jsonData).
End()
// TestContentTypeInference tests that the ContentType header is set
// properly when a custom override is provided using AppendHeader
// or Set methods.
// https://github.com/parnurzeal/gorequest/issues/164
func TestContentTypeInference(t *testing.T) {
var tests = []struct {
customContentType string
//type is reserved keyword
Type string
expectedHeader string
body string
}{
{"application/json", "json", "application/json", "{}"},
{"", "json", "", ""},
{"", "json", "", "{}"},
{"text/json", "json", "text/json", "{}"},
{"text/xml", "json", "text/xml", "<a />"},
}
for _, test := range tests {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check method is PATCH before going to check other features
if r.Method != POST {
t.Errorf("Expected method %q; got %q", POST, r.Method)
}
if r.Header == nil {
t.Error("Expected non-nil request Header")
}
if r.Header.Get("Content-Type") != test.expectedHeader {
t.Errorf("Expected Header Content-Type -> %q | but got %q", test.expectedHeader, r.Header.Get("Content-Type"))
}
}))

New().Post(ts.URL).
Set("Content-Type", test.customContentType).
Type(test.Type).
Send(test.body).
End()
New().Post(ts.URL).
Set("cOnTent-tYpE", test.customContentType).
Type(test.Type).
Send(test.body).
End()
New().Post(ts.URL).
AppendHeader("Content-Type", test.customContentType).
Type(test.Type).
Send(test.body).
End()
ts.Close()
}
}

// Test for request can accept multiple types.
Expand Down

0 comments on commit 219b5e0

Please sign in to comment.