Rename GetReasonFromBody() to simply Reason()
See ¹ for the customary idiomatic way of naming getters in Go. ¹: https://golang.org/doc/effective_go.html#Getters
This commit is contained in:
@@ -25,9 +25,9 @@ import (
|
|||||||
"github.com/BBVA/kapow/internal/server/srverrors"
|
"github.com/BBVA/kapow/internal/server/srverrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetReasonFromBody returns the reason phrase embedded within the JSON error
|
// Reason returns the reason phrase embedded within the JSON error
|
||||||
// body, or an error if no reason can be extracted
|
// body, or an error if no reason can be extracted
|
||||||
func GetReasonFromBody(r *http.Response) (string, error) {
|
func Reason(r *http.Response) (string, error) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("error reading response's body")
|
return "", errors.New("error reading response's body")
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetReasonFromBodyExtractsReasonFromJSON(t *testing.T) {
|
func TestReasonExtractsReasonFromJSON(t *testing.T) {
|
||||||
r := &nethttp.Response{
|
r := &nethttp.Response{
|
||||||
Status: "200 OK",
|
Status: "200 OK",
|
||||||
Body: ioutil.NopCloser(
|
Body: ioutil.NopCloser(
|
||||||
@@ -33,14 +33,14 @@ func TestGetReasonFromBodyExtractsReasonFromJSON(t *testing.T) {
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
reason, _ := GetReasonFromBody(r)
|
reason, _ := Reason(r)
|
||||||
|
|
||||||
if reason != "Because reasons" {
|
if reason != "Because reasons" {
|
||||||
t.Errorf(`reason mismatch, want "Because reasons", got %q`, reason)
|
t.Errorf(`reason mismatch, want "Because reasons", got %q`, reason)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReasonFromBodyErrorsOnJSONWithNoReason(t *testing.T) {
|
func TestReasonErrorsOnJSONWithNoReason(t *testing.T) {
|
||||||
r := &nethttp.Response{
|
r := &nethttp.Response{
|
||||||
Status: "200 OK",
|
Status: "200 OK",
|
||||||
Body: ioutil.NopCloser(
|
Body: ioutil.NopCloser(
|
||||||
@@ -50,14 +50,14 @@ func TestGetReasonFromBodyErrorsOnJSONWithNoReason(t *testing.T) {
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := GetReasonFromBody(r)
|
_, err := Reason(r)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("error not reported")
|
t.Error("error not reported")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReasonFromBodyErrorsOnJSONWithEmptyReason(t *testing.T) {
|
func TestReasonErrorsOnJSONWithEmptyReason(t *testing.T) {
|
||||||
r := &nethttp.Response{
|
r := &nethttp.Response{
|
||||||
Body: ioutil.NopCloser(
|
Body: ioutil.NopCloser(
|
||||||
strings.NewReader(
|
strings.NewReader(
|
||||||
@@ -66,28 +66,28 @@ func TestGetReasonFromBodyErrorsOnJSONWithEmptyReason(t *testing.T) {
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := GetReasonFromBody(r)
|
_, err := Reason(r)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("error not reported")
|
t.Error("error not reported")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReasonFromBodyErrorsOnNoJSON(t *testing.T) {
|
func TestReasonErrorsOnNoJSON(t *testing.T) {
|
||||||
r := &nethttp.Response{
|
r := &nethttp.Response{
|
||||||
Body: ioutil.NopCloser(
|
Body: ioutil.NopCloser(
|
||||||
strings.NewReader(""),
|
strings.NewReader(""),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := GetReasonFromBody(r)
|
_, err := Reason(r)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("error not reported")
|
t.Error("error not reported")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReasonFromBodyErrorsOnInvalidJSON(t *testing.T) {
|
func TestReasonErrorsOnInvalidJSON(t *testing.T) {
|
||||||
r := &nethttp.Response{
|
r := &nethttp.Response{
|
||||||
Body: ioutil.NopCloser(
|
Body: ioutil.NopCloser(
|
||||||
strings.NewReader(
|
strings.NewReader(
|
||||||
@@ -96,7 +96,7 @@ func TestGetReasonFromBodyErrorsOnInvalidJSON(t *testing.T) {
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := GetReasonFromBody(r)
|
_, err := Reason(r)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("error not reported")
|
t.Error("error not reported")
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ func Request(method string, url string, contentType string, r io.Reader, w io.Wr
|
|||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||||
reason, err := GetReasonFromBody(res)
|
reason, err := Reason(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user