|
|
@ -28,12 +28,14 @@ func TestError(t *testing.T) {
|
|
|
|
name string
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
input string
|
|
|
|
errorHandler func(error) (int, interface{})
|
|
|
|
errorHandler func(error) (int, interface{})
|
|
|
|
|
|
|
|
expectHaveBody bool
|
|
|
|
expectBody string
|
|
|
|
expectBody string
|
|
|
|
expectCode int
|
|
|
|
expectCode int
|
|
|
|
}{
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: "default error handler",
|
|
|
|
name: "default error handler",
|
|
|
|
input: body,
|
|
|
|
input: body,
|
|
|
|
|
|
|
|
expectHaveBody: true,
|
|
|
|
expectBody: body,
|
|
|
|
expectBody: body,
|
|
|
|
expectCode: http.StatusBadRequest,
|
|
|
|
expectCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -43,6 +45,7 @@ func TestError(t *testing.T) {
|
|
|
|
errorHandler: func(err error) (int, interface{}) {
|
|
|
|
errorHandler: func(err error) (int, interface{}) {
|
|
|
|
return http.StatusForbidden, err.Error()
|
|
|
|
return http.StatusForbidden, err.Error()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
expectHaveBody: true,
|
|
|
|
expectBody: wrappedBody,
|
|
|
|
expectBody: wrappedBody,
|
|
|
|
expectCode: http.StatusForbidden,
|
|
|
|
expectCode: http.StatusForbidden,
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -52,9 +55,20 @@ func TestError(t *testing.T) {
|
|
|
|
errorHandler: func(err error) (int, interface{}) {
|
|
|
|
errorHandler: func(err error) (int, interface{}) {
|
|
|
|
return http.StatusForbidden, err
|
|
|
|
return http.StatusForbidden, err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
expectHaveBody: true,
|
|
|
|
expectBody: body,
|
|
|
|
expectBody: body,
|
|
|
|
expectCode: http.StatusForbidden,
|
|
|
|
expectCode: http.StatusForbidden,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
name: "customized error handler return nil",
|
|
|
|
|
|
|
|
input: body,
|
|
|
|
|
|
|
|
errorHandler: func(err error) (int, interface{}) {
|
|
|
|
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
expectHaveBody: false,
|
|
|
|
|
|
|
|
expectBody: "",
|
|
|
|
|
|
|
|
expectCode: http.StatusForbidden,
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
for _, test := range tests {
|
|
|
@ -75,6 +89,7 @@ func TestError(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Error(&w, errors.New(test.input))
|
|
|
|
Error(&w, errors.New(test.input))
|
|
|
|
assert.Equal(t, test.expectCode, w.code)
|
|
|
|
assert.Equal(t, test.expectCode, w.code)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expectHaveBody, w.haveBody)
|
|
|
|
assert.Equal(t, test.expectBody, strings.TrimSpace(w.builder.String()))
|
|
|
|
assert.Equal(t, test.expectBody, strings.TrimSpace(w.builder.String()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -122,6 +137,7 @@ func TestWriteJsonLessWritten(t *testing.T) {
|
|
|
|
type tracedResponseWriter struct {
|
|
|
|
type tracedResponseWriter struct {
|
|
|
|
headers map[string][]string
|
|
|
|
headers map[string][]string
|
|
|
|
builder strings.Builder
|
|
|
|
builder strings.Builder
|
|
|
|
|
|
|
|
haveBody bool
|
|
|
|
code int
|
|
|
|
code int
|
|
|
|
lessWritten bool
|
|
|
|
lessWritten bool
|
|
|
|
timeout bool
|
|
|
|
timeout bool
|
|
|
@ -140,6 +156,7 @@ func (w *tracedResponseWriter) Write(bytes []byte) (n int, err error) {
|
|
|
|
if w.lessWritten {
|
|
|
|
if w.lessWritten {
|
|
|
|
n -= 1
|
|
|
|
n -= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.haveBody = true
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|