Skip to content

Commit

Permalink
#134 added support for rfc7807 - application/problem+json and applic…
Browse files Browse the repository at this point in the history
…ation/problem+xml (#135)
  • Loading branch information
jeevatkm authored Mar 7, 2018
1 parent 4efc844 commit d7fa836
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Resty first version released on Sep 15, 2015 then it grew gradually as a very ha
* For auto-unmarshal, refer to -
- Success scenario [Request.SetResult()](https://godoc.org/github.com/go-resty/resty#Request.SetResult) and [Response.Result()](https://godoc.org/github.com/go-resty/resty#Response.Result).
- Error scenario [Request.SetError()](https://godoc.org/github.com/go-resty/resty#Request.SetError) and [Response.Error()](https://godoc.org/github.com/go-resty/resty#Response.Error).
- Supports [RFC7807](https://tools.ietf.org/html/rfc7807) - `application/problem+json` & `application/problem+xml`
* Easy to upload one or more file(s) via `multipart/form-data`
* Auto detects file content type
* Request URL [Path Params](https://godoc.org/github.com/go-resty/resty#Request.SetPathParams)
Expand Down
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ var (
jsonContentType = "application/json; charset=utf-8"
formContentType = "application/x-www-form-urlencoded"

jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)")
jsonCheck = regexp.MustCompile(`(?i:(application|text)/(problem\+json|json))`)
xmlCheck = regexp.MustCompile(`(?i:(application|text)/(problem\+xml|xml))`)

hdrUserAgentValue = "go-resty v%s - https://github.com/go-resty/resty"
bufPool = &sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
Expand Down Expand Up @@ -869,8 +869,8 @@ func (f *File) String() string {

// multipartField represent custom data part for multipart request
type multipartField struct {
Param string
FileName string
Param string
FileName string
ContentType string
io.Reader
}
}
30 changes: 29 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,32 @@ func TestPostJSONStructInvalidLogin(t *testing.T) {

assertError(t, err)
assertEqual(t, http.StatusUnauthorized, resp.StatusCode())
assertEqual(t, resp.Header().Get("Www-Authenticate"), "Protected Realm")

authError := resp.Error().(*AuthError)
assertEqual(t, "unauthorized", authError.ID)
assertEqual(t, "Invalid credentials", authError.Message)
t.Logf("Result Error: %q", resp.Error().(*AuthError))

logResponse(t, resp)
}

func TestPostJSONErrorRFC7807(t *testing.T) {
ts := createPostServer(t)
defer ts.Close()

c := dc()
resp, err := c.R().
SetHeader(hdrContentTypeKey, jsonContentType).
SetBody(User{Username: "testuser", Password: "testpass1"}).
SetError(AuthError{}).
Post(ts.URL + "/login?ct=problem")

assertError(t, err)
assertEqual(t, http.StatusUnauthorized, resp.StatusCode())

authError := resp.Error().(*AuthError)
assertEqual(t, "unauthorized", authError.ID)
assertEqual(t, "Invalid credentials", authError.Message)
t.Logf("Result Error: %q", resp.Error().(*AuthError))

logResponse(t, resp)
Expand Down Expand Up @@ -253,6 +277,10 @@ func TestPostJSONMapInvalidResponseJson(t *testing.T) {
assertEqual(t, "invalid character '}' looking for beginning of object key string", err.Error())
assertEqual(t, http.StatusOK, resp.StatusCode())

authSuccess := resp.Result().(*AuthSuccess)
assertEqual(t, "", authSuccess.ID)
assertEqual(t, "", authSuccess.Message)

t.Logf("Result Success: %q", resp.Result().(*AuthSuccess))

logResponse(t, resp)
Expand Down
8 changes: 6 additions & 2 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ func handleLoginEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) {
if IsJSONType(r.Header.Get(hdrContentTypeKey)) {
jd := json.NewDecoder(r.Body)
err := jd.Decode(user)
w.Header().Set(hdrContentTypeKey, jsonContentType)
if r.URL.Query().Get("ct") == "problem" {
w.Header().Set(hdrContentTypeKey, "application/problem+json; charset=utf-8")
} else {
w.Header().Set(hdrContentTypeKey, jsonContentType)
}

if err != nil {
t.Logf("Error: %#v", err)
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -128,7 +133,6 @@ func handleLoginEndpoint(t *testing.T, w http.ResponseWriter, r *http.Request) {
} else if user.Username == "testuser" && user.Password == "invalidjson" {
_, _ = w.Write([]byte(`{ "id": "success", "message": "login successful", }`))
} else {
w.Header().Set("Www-Authenticate", "Protected Realm")
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{ "id": "unauthorized", "message": "Invalid credentials" }`))
}
Expand Down

0 comments on commit d7fa836

Please sign in to comment.