forked from TrueCloudLab/certificates
api/render: implemented BadRequest
This commit is contained in:
parent
4cdb38b2e8
commit
e82b21c1cb
2 changed files with 62 additions and 0 deletions
|
@ -113,3 +113,30 @@ func Error(w http.ResponseWriter, err error) {
|
|||
log.Error(w, err)
|
||||
}
|
||||
}
|
||||
|
||||
// BadRequest renders the JSON representation of err into w and sets its
|
||||
// status code to http.StatusBadRequest.
|
||||
func BadRequest(w http.ResponseWriter, err error) {
|
||||
codedError(w, http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
func codedError(w http.ResponseWriter, code int, err error) {
|
||||
var wrapper = struct {
|
||||
Status int `json:"status"`
|
||||
Message string `json:"message"`
|
||||
}{
|
||||
Status: code,
|
||||
Message: err.Error(),
|
||||
}
|
||||
|
||||
data, err := json.Marshal(wrapper)
|
||||
if err != nil {
|
||||
log.Error(w, err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(data)
|
||||
}
|
||||
|
|
|
@ -3,8 +3,11 @@ package render
|
|||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/smallstep/certificates/logging"
|
||||
)
|
||||
|
||||
|
@ -51,3 +54,35 @@ func TestJSON(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
cases := []struct {
|
||||
fn func(http.ResponseWriter, error) // helper
|
||||
err error // error being render
|
||||
code int // expected status code
|
||||
body string // expected body
|
||||
}{
|
||||
// --- BadRequest
|
||||
0: {
|
||||
fn: BadRequest,
|
||||
err: assert.AnError,
|
||||
code: http.StatusBadRequest,
|
||||
body: `{"status":400,"message":"assert.AnError general error for testing"}`,
|
||||
},
|
||||
}
|
||||
|
||||
for caseIndex := range cases {
|
||||
kase := cases[caseIndex]
|
||||
|
||||
t.Run(strconv.Itoa(caseIndex), func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
kase.fn(rec, kase.err)
|
||||
|
||||
ret := rec.Result()
|
||||
|
||||
assert.Equal(t, "application/json", ret.Header.Get("Content-Type"))
|
||||
assert.Equal(t, kase.code, ret.StatusCode)
|
||||
assert.Equal(t, kase.body, rec.Body.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue