Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix JSON decode bug #25

Merged
merged 3 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hydra/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
}

defer resp.Body.Close()
if v != nil {
if v != nil && resp.StatusCode < 300 {
err = json.NewDecoder(resp.Body).Decode(v)
}
return resp, err
Expand Down
27 changes: 16 additions & 11 deletions hydra/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ import (
)

const (
clientsEndpoint = "/clients"
schemeHTTP = "http"

testID = "test-id"
schemeHTTP = "http"
testClient = `{"client_id":"test-id","owner":"test-name","scope":"some,scopes","grant_types":["type1"]}`
testClientCreated = `{"client_id":"test-id-2","client_secret":"TmGkvcY7k526","owner":"test-name-2","scope":"some,other,scopes","grant_types":["type2"]}`
testClientUpdated = `{"client_id":"test-id-3","client_secret":"xFoPPm654por","owner":"test-name-3","scope":"yet,another,scope","grant_types":["type3"]}`
testClientList = `{"client_id":"test-id-4","owner":"test-name-4","scope":"scope1 scope2","grant_types":["type4"]}`
testClientList2 = `{"client_id":"test-id-5","owner":"test-name-5","scope":"scope3 scope4","grant_types":["type5"]}`
emptyBody = `{}`
emptyBodyList = `[]`
clientsEndpoint = "/clients"

statusNotFoundBody = `{"error":"Not Found","error_description":"Unable to locate the requested resource","status_code":404,"request_id":"id"}`
statusConflictBody = `{"error":"Unable to insert or update resource because a resource with that value exists already","error_description":"","status_code":409,"request_id":"id"`
statusInternalServerErrorBody = "the server encountered an internal error or misconfiguration and was unable to complete your request"
)

type server struct {
Expand Down Expand Up @@ -67,12 +70,12 @@ func TestCRUD(t *testing.T) {
},
"getting unregistered client": {
http.StatusNotFound,
emptyBody,
statusNotFoundBody,
nil,
},
"internal server error when requesting": {
http.StatusInternalServerError,
emptyBody,
statusInternalServerErrorBody,
errors.New("http request returned unexpected status code"),
},
} {
Expand Down Expand Up @@ -124,12 +127,12 @@ func TestCRUD(t *testing.T) {
},
"with existing client": {
http.StatusConflict,
emptyBody,
statusConflictBody,
errors.New("requested ID already exists"),
},
"internal server error when requesting": {
http.StatusInternalServerError,
emptyBody,
statusInternalServerErrorBody,
errors.New("http request returned unexpected status code"),
},
} {
Expand Down Expand Up @@ -182,7 +185,7 @@ func TestCRUD(t *testing.T) {
},
"internal server error when requesting": {
http.StatusInternalServerError,
emptyBody,
statusInternalServerErrorBody,
errors.New("http request returned unexpected status code"),
},
} {
Expand Down Expand Up @@ -234,9 +237,11 @@ func TestCRUD(t *testing.T) {
},
"with unregistered client": {
statusCode: http.StatusNotFound,
respBody: statusNotFoundBody,
},
"internal server error when requesting": {
statusCode: http.StatusInternalServerError,
respBody: statusInternalServerErrorBody,
err: errors.New("http request returned unexpected status code"),
},
} {
Expand Down Expand Up @@ -269,7 +274,7 @@ func TestCRUD(t *testing.T) {
for d, tc := range map[string]server{
"no clients": {
http.StatusOK,
emptyBodyList,
`[]`,
nil,
},
"one client": {
Expand All @@ -284,7 +289,7 @@ func TestCRUD(t *testing.T) {
},
"internal server error when requesting": {
http.StatusInternalServerError,
emptyBodyList,
statusInternalServerErrorBody,
errors.New("http request returned unexpected status code"),
},
} {
Expand Down