Skip to content

Commit

Permalink
#258 remove default charset addition from json content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Sep 8, 2019
1 parent d467d57 commit 78f1a6f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
hdrAuthorizationKey = http.CanonicalHeaderKey("Authorization")

plainTextType = "text/plain; charset=utf-8"
jsonContentType = "application/json; charset=utf-8"
jsonContentType = "application/json"
formContentType = "application/x-www-form-urlencoded"

jsonCheck = regexp.MustCompile(`(?i:(application|text)/(json|.*\+json|json\-.*)(;|$))`)
Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ func TestClientOptions(t *testing.T) {
client.SetHostURL("http://httpbin.org")
assertEqual(t, "http://httpbin.org", client.HostURL)

client.SetHeader(hdrContentTypeKey, jsonContentType)
client.SetHeader(hdrContentTypeKey, "application/json; charset=utf-8")
client.SetHeaders(map[string]string{
hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.1",
"X-Request-Id": strconv.FormatInt(time.Now().UnixNano(), 10),
})
assertEqual(t, jsonContentType, client.Header.Get(hdrContentTypeKey))
assertEqual(t, "application/json; charset=utf-8", client.Header.Get(hdrContentTypeKey))

client.SetCookie(&http.Cookie{
Name: "default-cookie",
Expand Down
18 changes: 9 additions & 9 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func TestPostJSONStringSuccess(t *testing.T) {
defer ts.Close()

c := dc()
c.SetHeader(hdrContentTypeKey, jsonContentType).
SetHeaders(map[string]string{hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.1", hdrAcceptKey: jsonContentType})
c.SetHeader(hdrContentTypeKey, "application/json; charset=utf-8").
SetHeaders(map[string]string{hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.1", hdrAcceptKey: "application/json; charset=utf-8"})

resp, err := c.R().
SetBody(`{"username":"testuser", "password":"testpass"}`).
Expand All @@ -148,8 +148,8 @@ func TestPostJSONBytesSuccess(t *testing.T) {
defer ts.Close()

c := dc()
c.SetHeader(hdrContentTypeKey, jsonContentType).
SetHeaders(map[string]string{hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.7", hdrAcceptKey: jsonContentType})
c.SetHeader(hdrContentTypeKey, "application/json; charset=utf-8").
SetHeaders(map[string]string{hdrUserAgentKey: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) go-resty v0.7", hdrAcceptKey: "application/json; charset=utf-8"})

resp, err := c.R().
SetBody([]byte(`{"username":"testuser", "password":"testpass"}`)).
Expand All @@ -166,7 +166,7 @@ func TestPostJSONBytesIoReader(t *testing.T) {
defer ts.Close()

c := dc()
c.SetHeader(hdrContentTypeKey, jsonContentType)
c.SetHeader(hdrContentTypeKey, "application/json; charset=utf-8")

bodyBytes := []byte(`{"username":"testuser", "password":"testpass"}`)

Expand All @@ -188,7 +188,7 @@ func TestPostJSONStructSuccess(t *testing.T) {

c := dc().SetJSONEscapeHTML(false)
resp, err := c.R().
SetHeader(hdrContentTypeKey, jsonContentType).
SetHeader(hdrContentTypeKey, "application/json; charset=utf-8").
SetBody(user).
SetResult(&AuthSuccess{}).
Post(ts.URL + "/login")
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestPostJSONStructInvalidLogin(t *testing.T) {
c.SetDebug(false)

resp, err := c.R().
SetHeader(hdrContentTypeKey, jsonContentType).
SetHeader(hdrContentTypeKey, "application/json; charset=utf-8").
SetBody(User{Username: "testuser", Password: "testpass1"}).
SetError(AuthError{}).
SetJSONEscapeHTML(false).
Expand All @@ -254,7 +254,7 @@ func TestPostJSONErrorRFC7807(t *testing.T) {

c := dc()
resp, err := c.R().
SetHeader(hdrContentTypeKey, jsonContentType).
SetHeader(hdrContentTypeKey, "application/json; charset=utf-8").
SetBody(User{Username: "testuser", Password: "testpass1"}).
SetError(AuthError{}).
Post(ts.URL + "/login?ct=problem")
Expand Down Expand Up @@ -828,7 +828,7 @@ func TestPutJSONString(t *testing.T) {
client.outputLogTo(ioutil.Discard)

resp, err := client.R().
SetHeaders(map[string]string{hdrContentTypeKey: jsonContentType, hdrAcceptKey: jsonContentType}).
SetHeaders(map[string]string{hdrContentTypeKey: "application/json; charset=utf-8", hdrAcceptKey: "application/json; charset=utf-8"}).
SetBody(`{"content":"json content sending to server"}`).
Put(ts.URL + "/json")

Expand Down
14 changes: 7 additions & 7 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func handleLoginEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) {
} else if r.URL.Query().Get("ct") == "rpc" {
w.Header().Set(hdrContentTypeKey, "application/json-rpc")
} else {
w.Header().Set(hdrContentTypeKey, jsonContentType)
w.Header().Set(hdrContentTypeKey, "application/json")
}

if err != nil {
Expand Down Expand Up @@ -192,7 +192,7 @@ func handleUsersEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) {
var users []ExampleUser
jd := json.NewDecoder(r.Body)
err := jd.Decode(&users)
w.Header().Set(hdrContentTypeKey, jsonContentType)
w.Header().Set(hdrContentTypeKey, "application/json")
if err != nil {
t.Logf("Error: %v", err)
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -240,7 +240,7 @@ func createPostServer(t *testing.T) *httptest.Server {
t.Errorf("Error: could not read post body: %s", err.Error())
}
t.Logf("Got query param: status=500 so we're returning the post body as response and a 500 status code. body: %s", string(body))
w.Header().Set(hdrContentTypeKey, jsonContentType)
w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write(body)
return
Expand All @@ -249,7 +249,7 @@ func createPostServer(t *testing.T) *httptest.Server {
var users []map[string]interface{}
jd := json.NewDecoder(r.Body)
err := jd.Decode(&users)
w.Header().Set(hdrContentTypeKey, jsonContentType)
w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8")
if err != nil {
t.Logf("Error: %v", err)
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -391,7 +391,7 @@ func createAuthServer(t *testing.T) *httptest.Server {
auth := r.Header.Get("Authorization")
t.Logf("Bearer Auth: %v", auth)

w.Header().Set(hdrContentTypeKey, jsonContentType)
w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8")

if !strings.HasPrefix(auth, "Bearer ") {
w.Header().Set("Www-Authenticate", "Protected Realm")
Expand All @@ -414,7 +414,7 @@ func createAuthServer(t *testing.T) *httptest.Server {
auth := r.Header.Get("Authorization")
t.Logf("Basic Auth: %v", auth)

w.Header().Set(hdrContentTypeKey, jsonContentType)
w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8")

password, err := base64.StdEncoding.DecodeString(auth[6:])
if err != nil || string(password) != "myuser:basicauth" {
Expand Down Expand Up @@ -472,7 +472,7 @@ func createGenServer(t *testing.T) *httptest.Server {
if r.URL.Path == "/plaintext" {
_, _ = w.Write([]byte("TestPut: plain text response"))
} else if r.URL.Path == "/json" {
w.Header().Set(hdrContentTypeKey, jsonContentType)
w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8")
_, _ = w.Write([]byte(`{"response":"json response"}`))
} else if r.URL.Path == "/xml" {
w.Header().Set(hdrContentTypeKey, "application/xml")
Expand Down

0 comments on commit 78f1a6f

Please sign in to comment.