mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
The ResponseWriters defined in rest.handler add Flush interface. (#318)
This commit is contained in:
parent
71a2b20301
commit
0bd2a0656c
@ -138,3 +138,9 @@ 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()
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,10 @@ func TestAuthHandler(t *testing.T) {
|
||||
w.Header().Set("X-Test", "test")
|
||||
_, err := w.Write([]byte("content"))
|
||||
assert.Nil(t, err)
|
||||
|
||||
flusher, ok := w.(http.Flusher)
|
||||
assert.Equal(t, ok, true)
|
||||
flusher.Flush()
|
||||
}))
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
|
@ -95,6 +95,12 @@ 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
|
||||
|
@ -87,3 +87,20 @@ func TestCryptionHandlerWriteHeader(t *testing.T) {
|
||||
handler.ServeHTTP(recorder, req)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, recorder.Code)
|
||||
}
|
||||
|
||||
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)
|
||||
flusher.Flush()
|
||||
}))
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, req)
|
||||
|
||||
expect, err := codec.EcbEncrypt(aesKey, []byte(respText))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, base64.StdEncoding.EncodeToString(expect), recorder.Body.String())
|
||||
}
|
||||
|
@ -38,6 +38,12 @@ func (w *LoggedResponseWriter) WriteHeader(code int) {
|
||||
w.code = code
|
||||
}
|
||||
|
||||
func (w *LoggedResponseWriter) Flush() {
|
||||
if flusher, ok := w.w.(http.Flusher); ok {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func LogHandler(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
timer := utils.NewElapsedTimer()
|
||||
@ -81,6 +87,12 @@ 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()
|
||||
|
@ -30,6 +30,10 @@ func TestLogHandler(t *testing.T) {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, err := w.Write([]byte("content"))
|
||||
assert.Nil(t, err)
|
||||
|
||||
flusher, ok := w.(http.Flusher)
|
||||
assert.Equal(t, ok, true)
|
||||
flusher.Flush()
|
||||
}))
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
|
@ -19,3 +19,9 @@ 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()
|
||||
}
|
||||
}
|
||||
|
33
rest/internal/security/withcoderesponsewriter_test.go
Normal file
33
rest/internal/security/withcoderesponsewriter_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWithCodeResponseWriter(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
cw := &WithCodeResponseWriter{Writer: w}
|
||||
|
||||
cw.Header().Set("X-Test", "test")
|
||||
cw.WriteHeader(http.StatusServiceUnavailable)
|
||||
assert.Equal(t, cw.Code, http.StatusServiceUnavailable)
|
||||
|
||||
_, err := cw.Write([]byte("content"))
|
||||
assert.Nil(t, err)
|
||||
|
||||
flusher, ok := http.ResponseWriter(cw).(http.Flusher)
|
||||
assert.Equal(t, ok, true)
|
||||
flusher.Flush()
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
||||
assert.Equal(t, "test", resp.Header().Get("X-Test"))
|
||||
assert.Equal(t, "content", resp.Body.String())
|
||||
}
|
Loading…
Reference in New Issue
Block a user