@@ -3,6 +3,7 @@ package middleware
3
3
import (
4
4
"bytes"
5
5
"compress/gzip"
6
+ "io"
6
7
"io/ioutil"
7
8
"net/http"
8
9
"net/http/httptest"
@@ -44,6 +45,49 @@ func TestGzip(t *testing.T) {
44
45
buf .ReadFrom (r )
45
46
assert .Equal ("test" , buf .String ())
46
47
}
48
+
49
+ chunkBuf := make ([]byte , 5 )
50
+
51
+ // Gzip chunked
52
+ req = httptest .NewRequest (http .MethodGet , "/" , nil )
53
+ req .Header .Set (echo .HeaderAcceptEncoding , gzipScheme )
54
+ rec = httptest .NewRecorder ()
55
+
56
+ c = e .NewContext (req , rec )
57
+ Gzip ()(func (c echo.Context ) error {
58
+ c .Response ().Header ().Set ("Content-Type" , "text/event-stream" )
59
+ c .Response ().Header ().Set ("Transfer-Encoding" , "chunked" )
60
+
61
+ // Write and flush the first part of the data
62
+ c .Response ().Write ([]byte ("test\n " ))
63
+ c .Response ().Flush ()
64
+
65
+ // Read the first part of the data
66
+ assert .True (rec .Flushed )
67
+ assert .Equal (gzipScheme , rec .Header ().Get (echo .HeaderContentEncoding ))
68
+ r .Reset (rec .Body )
69
+
70
+ _ , err = io .ReadFull (r , chunkBuf )
71
+ assert .NoError (err )
72
+ assert .Equal ("test\n " , string (chunkBuf ))
73
+
74
+ // Write and flush the second part of the data
75
+ c .Response ().Write ([]byte ("test\n " ))
76
+ c .Response ().Flush ()
77
+
78
+ _ , err = io .ReadFull (r , chunkBuf )
79
+ assert .NoError (err )
80
+ assert .Equal ("test\n " , string (chunkBuf ))
81
+
82
+ // Write the final part of the data and return
83
+ c .Response ().Write ([]byte ("test" ))
84
+ return nil
85
+ })(c )
86
+
87
+ buf := new (bytes.Buffer )
88
+ defer r .Close ()
89
+ buf .ReadFrom (r )
90
+ assert .Equal ("test" , buf .String ())
47
91
}
48
92
49
93
func TestGzipNoContent (t * testing.T ) {
0 commit comments