fix JSON decode bug (#25)

* fix JSON decode bug
This commit is contained in:
Jakub Kabza 2019-09-19 11:25:09 +02:00 committed by Tomasz Smelcerz
parent 22988c0db6
commit 88ceb5da75
2 changed files with 17 additions and 12 deletions

View File

@ -164,7 +164,7 @@ func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
if v != nil { if v != nil && resp.StatusCode < 300 {
err = json.NewDecoder(resp.Body).Decode(v) err = json.NewDecoder(resp.Body).Decode(v)
} }
return resp, err return resp, err

View File

@ -17,16 +17,19 @@ import (
) )
const ( const (
clientsEndpoint = "/clients"
schemeHTTP = "http"
testID = "test-id" testID = "test-id"
schemeHTTP = "http"
testClient = `{"client_id":"test-id","owner":"test-name","scope":"some,scopes","grant_types":["type1"]}` 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"]}` 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"]}` 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"]}` 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"]}` testClientList2 = `{"client_id":"test-id-5","owner":"test-name-5","scope":"scope3 scope4","grant_types":["type5"]}`
emptyBody = `{}`
emptyBodyList = `[]` statusNotFoundBody = `{"error":"Not Found","error_description":"Unable to locate the requested resource","status_code":404,"request_id":"id"}`
clientsEndpoint = "/clients" 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 { type server struct {
@ -67,12 +70,12 @@ func TestCRUD(t *testing.T) {
}, },
"getting unregistered client": { "getting unregistered client": {
http.StatusNotFound, http.StatusNotFound,
emptyBody, statusNotFoundBody,
nil, nil,
}, },
"internal server error when requesting": { "internal server error when requesting": {
http.StatusInternalServerError, http.StatusInternalServerError,
emptyBody, statusInternalServerErrorBody,
errors.New("http request returned unexpected status code"), errors.New("http request returned unexpected status code"),
}, },
} { } {
@ -124,12 +127,12 @@ func TestCRUD(t *testing.T) {
}, },
"with existing client": { "with existing client": {
http.StatusConflict, http.StatusConflict,
emptyBody, statusConflictBody,
errors.New("requested ID already exists"), errors.New("requested ID already exists"),
}, },
"internal server error when requesting": { "internal server error when requesting": {
http.StatusInternalServerError, http.StatusInternalServerError,
emptyBody, statusInternalServerErrorBody,
errors.New("http request returned unexpected status code"), errors.New("http request returned unexpected status code"),
}, },
} { } {
@ -182,7 +185,7 @@ func TestCRUD(t *testing.T) {
}, },
"internal server error when requesting": { "internal server error when requesting": {
http.StatusInternalServerError, http.StatusInternalServerError,
emptyBody, statusInternalServerErrorBody,
errors.New("http request returned unexpected status code"), errors.New("http request returned unexpected status code"),
}, },
} { } {
@ -234,9 +237,11 @@ func TestCRUD(t *testing.T) {
}, },
"with unregistered client": { "with unregistered client": {
statusCode: http.StatusNotFound, statusCode: http.StatusNotFound,
respBody: statusNotFoundBody,
}, },
"internal server error when requesting": { "internal server error when requesting": {
statusCode: http.StatusInternalServerError, statusCode: http.StatusInternalServerError,
respBody: statusInternalServerErrorBody,
err: errors.New("http request returned unexpected status code"), err: errors.New("http request returned unexpected status code"),
}, },
} { } {
@ -269,7 +274,7 @@ func TestCRUD(t *testing.T) {
for d, tc := range map[string]server{ for d, tc := range map[string]server{
"no clients": { "no clients": {
http.StatusOK, http.StatusOK,
emptyBodyList, `[]`,
nil, nil,
}, },
"one client": { "one client": {
@ -284,7 +289,7 @@ func TestCRUD(t *testing.T) {
}, },
"internal server error when requesting": { "internal server error when requesting": {
http.StatusInternalServerError, http.StatusInternalServerError,
emptyBodyList, statusInternalServerErrorBody,
errors.New("http request returned unexpected status code"), errors.New("http request returned unexpected status code"),
}, },
} { } {