mirror of
https://github.com/pomerium/pomerium.git
synced 2025-06-06 12:52:53 +02:00
httputil: use http error wrapper
Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
parent
d26f935cbb
commit
6e6ab3baa0
11 changed files with 325 additions and 677 deletions
|
@ -1,9 +1,12 @@
|
|||
package httputil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestErrorResponse(t *testing.T) {
|
||||
|
@ -11,10 +14,10 @@ func TestErrorResponse(t *testing.T) {
|
|||
name string
|
||||
rw http.ResponseWriter
|
||||
r *http.Request
|
||||
e *Error
|
||||
e *httpError
|
||||
}{
|
||||
{"good", httptest.NewRecorder(), &http.Request{Method: http.MethodGet}, &Error{Code: http.StatusBadRequest, Message: "missing id token"}},
|
||||
{"good json", httptest.NewRecorder(), &http.Request{Method: http.MethodGet, Header: http.Header{"Accept": []string{"application/json"}}}, &Error{Code: http.StatusBadRequest, Message: "missing id token"}},
|
||||
{"good", httptest.NewRecorder(), &http.Request{Method: http.MethodGet}, &httpError{Code: http.StatusBadRequest, Message: "missing id token"}},
|
||||
{"good json", httptest.NewRecorder(), &http.Request{Method: http.MethodGet, Header: http.Header{"Accept": []string{"application/json"}}}, &httpError{Code: http.StatusBadRequest, Message: "missing id token"}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
@ -29,20 +32,44 @@ func TestError_Error(t *testing.T) {
|
|||
name string
|
||||
Message string
|
||||
Code int
|
||||
CanDebug bool
|
||||
InnerErr error
|
||||
want string
|
||||
}{
|
||||
{"good", "short and stout", http.StatusTeapot, false, "418 I'm a teapot: short and stout"},
|
||||
{"good", "short and stout", http.StatusTeapot, nil, "418 I'm a teapot: short and stout"},
|
||||
{"nested error", "short and stout", http.StatusTeapot, errors.New("another error"), "418 I'm a teapot: short and stout: another error"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := Error{
|
||||
Message: tt.Message,
|
||||
Code: tt.Code,
|
||||
CanDebug: tt.CanDebug,
|
||||
h := httpError{
|
||||
Message: tt.Message,
|
||||
Code: tt.Code,
|
||||
Err: tt.InnerErr,
|
||||
}
|
||||
if got := h.Error(); got != tt.want {
|
||||
t.Errorf("Error.Error() = %v, want %v", got, tt.want)
|
||||
got := h.Error()
|
||||
if diff := cmp.Diff(got, tt.want); diff != "" {
|
||||
t.Errorf("Error.Error() = %s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_httpError_Error(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
message string
|
||||
code int
|
||||
err error
|
||||
want string
|
||||
}{
|
||||
{"good", "foobar", 200, nil, "200 OK: foobar"},
|
||||
{"no code", "foobar", 0, nil, "500 Internal Server Error: foobar"},
|
||||
{"no message or code", "", 0, nil, "500 Internal Server Error: Internal Server Error"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
e := Error(tt.message, tt.code, tt.err)
|
||||
if got := e.Error(); got != tt.want {
|
||||
t.Errorf("httpError.Error() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue