test: add more tests (#1856)

This commit is contained in:
Kevin Wan
2022-05-02 21:24:20 +08:00
committed by GitHub
parent d0a58d1f2d
commit f21970c117
6 changed files with 194 additions and 5 deletions

View File

@@ -129,7 +129,18 @@ func TestWriteJsonTimeout(t *testing.T) {
// only log it and ignore
w := tracedResponseWriter{
headers: make(map[string][]string),
timeout: true,
err: http.ErrHandlerTimeout,
}
msg := message{Name: "anyone"}
WriteJson(&w, http.StatusOK, msg)
assert.Equal(t, http.StatusOK, w.code)
}
func TestWriteJsonError(t *testing.T) {
// only log it and ignore
w := tracedResponseWriter{
headers: make(map[string][]string),
err: errors.New("foo"),
}
msg := message{Name: "anyone"}
WriteJson(&w, http.StatusOK, msg)
@@ -162,8 +173,8 @@ type tracedResponseWriter struct {
hasBody bool
code int
lessWritten bool
timeout bool
wroteHeader bool
err error
}
func (w *tracedResponseWriter) Header() http.Header {
@@ -171,8 +182,8 @@ func (w *tracedResponseWriter) Header() http.Header {
}
func (w *tracedResponseWriter) Write(bytes []byte) (n int, err error) {
if w.timeout {
return 0, http.ErrHandlerTimeout
if w.err != nil {
return 0, w.err
}
n, err = w.builder.Write(bytes)