From e97e1f10db1adb9c348010ca1933d9ac89c1f62a Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 29 Dec 2020 10:25:55 +0800 Subject: [PATCH] simplify code with http.Flusher type conversion (#325) * simplify code with http.Flusher type conversion * simplify code with http.Flusher type conversion, better version --- rest/handler/authhandler.go | 18 +++++++++--------- rest/handler/authhandler_test.go | 2 +- rest/handler/cryptionhandler.go | 12 ++++++------ rest/handler/cryptionhandler_test.go | 3 +-- rest/handler/loghandler.go | 10 ++++------ rest/handler/loghandler_test.go | 2 +- .../security/withcoderesponsewriter.go | 12 ++++++------ .../security/withcoderesponsewriter_test.go | 2 +- 8 files changed, 29 insertions(+), 32 deletions(-) diff --git a/rest/handler/authhandler.go b/rest/handler/authhandler.go index ab65bd9b..5d24c4ab 100644 --- a/rest/handler/authhandler.go +++ b/rest/handler/authhandler.go @@ -46,18 +46,18 @@ func Authorize(secret string, opts ...AuthorizeOption) func(http.Handler) http.H parser := token.NewTokenParser() return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - token, err := parser.ParseToken(r, secret, authOpts.PrevSecret) + tok, err := parser.ParseToken(r, secret, authOpts.PrevSecret) if err != nil { unauthorized(w, r, err, authOpts.Callback) return } - if !token.Valid { + if !tok.Valid { unauthorized(w, r, errInvalidToken, authOpts.Callback) return } - claims, ok := token.Claims.(jwt.MapClaims) + claims, ok := tok.Claims.(jwt.MapClaims) if !ok { unauthorized(w, r, errNoClaims, authOpts.Callback) return @@ -122,6 +122,12 @@ func newGuardedResponseWriter(w http.ResponseWriter) *guardedResponseWriter { } } +func (grw *guardedResponseWriter) Flush() { + if flusher, ok := grw.writer.(http.Flusher); ok { + flusher.Flush() + } +} + func (grw *guardedResponseWriter) Header() http.Header { return grw.writer.Header() } @@ -138,9 +144,3 @@ func (grw *guardedResponseWriter) WriteHeader(statusCode int) { grw.wroteHeader = true grw.writer.WriteHeader(statusCode) } - -func (grw *guardedResponseWriter) Flush() { - if flusher, ok := grw.writer.(http.Flusher); ok { - flusher.Flush() - } -} diff --git a/rest/handler/authhandler_test.go b/rest/handler/authhandler_test.go index 1bd22649..22ae3384 100644 --- a/rest/handler/authhandler_test.go +++ b/rest/handler/authhandler_test.go @@ -43,7 +43,7 @@ func TestAuthHandler(t *testing.T) { assert.Nil(t, err) flusher, ok := w.(http.Flusher) - assert.Equal(t, ok, true) + assert.True(t, ok) flusher.Flush() })) diff --git a/rest/handler/cryptionhandler.go b/rest/handler/cryptionhandler.go index 58ee396c..f78dc8b0 100644 --- a/rest/handler/cryptionhandler.go +++ b/rest/handler/cryptionhandler.go @@ -83,6 +83,12 @@ func newCryptionResponseWriter(w http.ResponseWriter) *cryptionResponseWriter { } } +func (w *cryptionResponseWriter) Flush() { + if flusher, ok := w.ResponseWriter.(http.Flusher); ok { + flusher.Flush() + } +} + func (w *cryptionResponseWriter) Header() http.Header { return w.ResponseWriter.Header() } @@ -95,12 +101,6 @@ func (w *cryptionResponseWriter) WriteHeader(statusCode int) { w.ResponseWriter.WriteHeader(statusCode) } -func (w *cryptionResponseWriter) Flush() { - if flusher, ok := w.ResponseWriter.(http.Flusher); ok { - flusher.Flush() - } -} - func (w *cryptionResponseWriter) flush(key []byte) { if w.buf.Len() == 0 { return diff --git a/rest/handler/cryptionhandler_test.go b/rest/handler/cryptionhandler_test.go index f7aab395..9819f4dc 100644 --- a/rest/handler/cryptionhandler_test.go +++ b/rest/handler/cryptionhandler_test.go @@ -92,9 +92,8 @@ func TestCryptionHandlerFlush(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/any", nil) handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(respText)) - flusher, ok := w.(http.Flusher) - assert.Equal(t, ok, true) + assert.True(t, ok) flusher.Flush() })) recorder := httptest.NewRecorder() diff --git a/rest/handler/loghandler.go b/rest/handler/loghandler.go index 1f7da6ee..3625481d 100644 --- a/rest/handler/loghandler.go +++ b/rest/handler/loghandler.go @@ -74,6 +74,10 @@ func newDetailLoggedResponseWriter(writer *LoggedResponseWriter, buf *bytes.Buff } } +func (w *DetailLoggedResponseWriter) Flush() { + w.writer.Flush() +} + func (w *DetailLoggedResponseWriter) Header() http.Header { return w.writer.Header() } @@ -87,12 +91,6 @@ func (w *DetailLoggedResponseWriter) WriteHeader(code int) { w.writer.WriteHeader(code) } -func (w *DetailLoggedResponseWriter) Flush() { - if flusher, ok := http.ResponseWriter(w.writer).(http.Flusher); ok { - flusher.Flush() - } -} - func DetailedLogHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { timer := utils.NewElapsedTimer() diff --git a/rest/handler/loghandler_test.go b/rest/handler/loghandler_test.go index 24d1643b..ed97ca58 100644 --- a/rest/handler/loghandler_test.go +++ b/rest/handler/loghandler_test.go @@ -32,7 +32,7 @@ func TestLogHandler(t *testing.T) { assert.Nil(t, err) flusher, ok := w.(http.Flusher) - assert.Equal(t, ok, true) + assert.True(t, ok) flusher.Flush() })) diff --git a/rest/internal/security/withcoderesponsewriter.go b/rest/internal/security/withcoderesponsewriter.go index 795e1b2d..0ee496a9 100644 --- a/rest/internal/security/withcoderesponsewriter.go +++ b/rest/internal/security/withcoderesponsewriter.go @@ -7,6 +7,12 @@ type WithCodeResponseWriter struct { Code int } +func (w *WithCodeResponseWriter) Flush() { + if flusher, ok := w.Writer.(http.Flusher); ok { + flusher.Flush() + } +} + func (w *WithCodeResponseWriter) Header() http.Header { return w.Writer.Header() } @@ -19,9 +25,3 @@ func (w *WithCodeResponseWriter) WriteHeader(code int) { w.Writer.WriteHeader(code) w.Code = code } - -func (w *WithCodeResponseWriter) Flush() { - if flusher, ok := w.Writer.(http.Flusher); ok { - flusher.Flush() - } -} diff --git a/rest/internal/security/withcoderesponsewriter_test.go b/rest/internal/security/withcoderesponsewriter_test.go index 229878cd..3a627798 100644 --- a/rest/internal/security/withcoderesponsewriter_test.go +++ b/rest/internal/security/withcoderesponsewriter_test.go @@ -21,7 +21,7 @@ func TestWithCodeResponseWriter(t *testing.T) { assert.Nil(t, err) flusher, ok := http.ResponseWriter(cw).(http.Flusher) - assert.Equal(t, ok, true) + assert.True(t, ok) flusher.Flush() })