From 60794baedd0d5b3ea029a15128b69689d4481946 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 20 May 2018 22:08:22 -0500 Subject: [PATCH] add httperrors tests --- app/rest/{http_errors.go => httperrors.go} | 6 +-- app/rest/httperrors_test.go | 46 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) rename app/rest/{http_errors.go => httperrors.go} (82%) create mode 100644 app/rest/httperrors_test.go diff --git a/app/rest/http_errors.go b/app/rest/httperrors.go similarity index 82% rename from app/rest/http_errors.go rename to app/rest/httperrors.go index 5e9329b6..87ba221c 100644 --- a/app/rest/http_errors.go +++ b/app/rest/httperrors.go @@ -13,12 +13,12 @@ import ( // SendErrorJSON makes {error: blah, details: blah} json body and responds with error code func SendErrorJSON(w http.ResponseWriter, r *http.Request, code int, err error, details string) { - logDetails(r, code, err, details) + log.Printf("[DEBUG] %s", errDetailsMsg(r, code, err, details)) render.Status(r, code) render.JSON(w, r, map[string]interface{}{"error": err.Error(), "details": details}) } -func logDetails(r *http.Request, code int, err error, details string) { +func errDetailsMsg(r *http.Request, code int, err error, details string) string { uinfoStr := "" if user, e := GetUserInfo(r); e == nil { uinfoStr = user.Name + "/" + user.ID + " - " @@ -35,6 +35,6 @@ func logDetails(r *http.Request, code int, err error, details string) { srcFileInfo = fmt.Sprintf(" [caused by %s:%d]", strings.Join(fnameElems[len(fnameElems)-3:], "/"), line) } - log.Printf("[DEBUG] %s - %v - %d - %s%s - %s%s", + return fmt.Sprintf("%s - %v - %d - %s%s - %s%s", details, err, code, uinfoStr, strings.Split(r.RemoteAddr, ":")[0], q, srcFileInfo) } diff --git a/app/rest/httperrors_test.go b/app/rest/httperrors_test.go new file mode 100644 index 00000000..207e2974 --- /dev/null +++ b/app/rest/httperrors_test.go @@ -0,0 +1,46 @@ +package rest + +import ( + "errors" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSendErrorJSON(t *testing.T) { + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/error" { + t.Log("http err request", r.URL) + SendErrorJSON(w, r, 500, errors.New("error 500"), "error details 123456") + return + } + w.WriteHeader(404) + })) + + defer ts.Close() + + resp, err := http.Get(ts.URL + "/error") + require.Nil(t, err) + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + require.Nil(t, err) + assert.Equal(t, 500, resp.StatusCode) + + assert.Equal(t, `{"details":"error details 123456","error":"error 500"}`+"\n", string(body)) +} + +func TestErrorDetailsMsg(t *testing.T) { + callerFn := func() { + req, err := http.NewRequest("GET", "https://example.com/test?k1=v1&k2=v2", nil) + require.Nil(t, err) + msg := errDetailsMsg(req, 500, errors.New("error 500"), "error details 123456") + assert.Equal(t, "error details 123456 - error 500 - 500 - - https://example.com/test?k1=v1&k2=v2 [caused by app/rest/httperrors_test.go:45]", msg) + } + callerFn() +}