|
|
|
@ -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())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|