@@ -2035,6 +2035,147 @@ func TestCompressHandler(t *testing.T) {
2035
2035
}
2036
2036
}
2037
2037
2038
+ func TestCompressHandlerVary (t * testing.T ) {
2039
+ t .Parallel ()
2040
+
2041
+ expectedBody := string (createFixedBody (2e4 ))
2042
+
2043
+ h := CompressHandlerBrotliLevel (func (ctx * RequestCtx ) {
2044
+ ctx .WriteString (expectedBody ) //nolint:errcheck
2045
+ }, CompressBrotliBestSpeed , CompressBestSpeed )
2046
+
2047
+ var ctx RequestCtx
2048
+ var resp Response
2049
+
2050
+ // verify uncompressed response
2051
+ h (& ctx )
2052
+ s := ctx .Response .String ()
2053
+ br := bufio .NewReader (bytes .NewBufferString (s ))
2054
+ if err := resp .Read (br ); err != nil {
2055
+ t .Fatalf ("unexpected error: %v" , err )
2056
+ }
2057
+ ce := resp .Header .ContentEncoding ()
2058
+ if string (ce ) != "" {
2059
+ t .Fatalf ("unexpected Content-Encoding: %q. Expecting %q" , ce , "" )
2060
+ }
2061
+ vary := resp .Header .Peek ("Vary" )
2062
+ if string (vary ) != "" {
2063
+ t .Fatalf ("unexpected Vary: %q. Expecting %q" , vary , "" )
2064
+ }
2065
+ body := resp .Body ()
2066
+ if string (body ) != expectedBody {
2067
+ t .Fatalf ("unexpected body %q. Expecting %q" , body , expectedBody )
2068
+ }
2069
+
2070
+ // verify gzip-compressed response
2071
+ ctx .Request .Reset ()
2072
+ ctx .Response .Reset ()
2073
+ ctx .Request .Header .Set ("Accept-Encoding" , "gzip, deflate, sdhc" )
2074
+
2075
+ h (& ctx )
2076
+ s = ctx .Response .String ()
2077
+ br = bufio .NewReader (bytes .NewBufferString (s ))
2078
+ if err := resp .Read (br ); err != nil {
2079
+ t .Fatalf ("unexpected error: %v" , err )
2080
+ }
2081
+ ce = resp .Header .ContentEncoding ()
2082
+ if string (ce ) != "gzip" {
2083
+ t .Fatalf ("unexpected Content-Encoding: %q. Expecting %q" , ce , "gzip" )
2084
+ }
2085
+ vary = resp .Header .Peek ("Vary" )
2086
+ if string (vary ) != "Accept-Encoding" {
2087
+ t .Fatalf ("unexpected Vary: %q. Expecting %q" , vary , "Accept-Encoding" )
2088
+ }
2089
+ body , err := resp .BodyGunzip ()
2090
+ if err != nil {
2091
+ t .Fatalf ("unexpected error: %v" , err )
2092
+ }
2093
+ if string (body ) != expectedBody {
2094
+ t .Fatalf ("unexpected body %q. Expecting %q" , body , expectedBody )
2095
+ }
2096
+
2097
+ // an attempt to compress already compressed response
2098
+ ctx .Request .Reset ()
2099
+ ctx .Response .Reset ()
2100
+ ctx .Request .Header .Set ("Accept-Encoding" , "gzip, deflate, sdhc" )
2101
+ hh := CompressHandler (h )
2102
+ hh (& ctx )
2103
+ s = ctx .Response .String ()
2104
+ br = bufio .NewReader (bytes .NewBufferString (s ))
2105
+ if err := resp .Read (br ); err != nil {
2106
+ t .Fatalf ("unexpected error: %v" , err )
2107
+ }
2108
+ ce = resp .Header .ContentEncoding ()
2109
+ if string (ce ) != "gzip" {
2110
+ t .Fatalf ("unexpected Content-Encoding: %q. Expecting %q" , ce , "gzip" )
2111
+ }
2112
+ vary = resp .Header .Peek ("Vary" )
2113
+ if string (vary ) != "Accept-Encoding" {
2114
+ t .Fatalf ("unexpected Vary: %q. Expecting %q" , vary , "Accept-Encoding" )
2115
+ }
2116
+ body , err = resp .BodyGunzip ()
2117
+ if err != nil {
2118
+ t .Fatalf ("unexpected error: %v" , err )
2119
+ }
2120
+ if string (body ) != expectedBody {
2121
+ t .Fatalf ("unexpected body %q. Expecting %q" , body , expectedBody )
2122
+ }
2123
+
2124
+ // verify deflate-compressed response
2125
+ ctx .Request .Reset ()
2126
+ ctx .Response .Reset ()
2127
+ ctx .Request .Header .Set (HeaderAcceptEncoding , "foobar, deflate, sdhc" )
2128
+
2129
+ h (& ctx )
2130
+ s = ctx .Response .String ()
2131
+ br = bufio .NewReader (bytes .NewBufferString (s ))
2132
+ if err := resp .Read (br ); err != nil {
2133
+ t .Fatalf ("unexpected error: %v" , err )
2134
+ }
2135
+ ce = resp .Header .ContentEncoding ()
2136
+ if string (ce ) != "deflate" {
2137
+ t .Fatalf ("unexpected Content-Encoding: %q. Expecting %q" , ce , "deflate" )
2138
+ }
2139
+ vary = resp .Header .Peek ("Vary" )
2140
+ if string (vary ) != "Accept-Encoding" {
2141
+ t .Fatalf ("unexpected Vary: %q. Expecting %q" , vary , "Accept-Encoding" )
2142
+ }
2143
+ body , err = resp .BodyInflate ()
2144
+ if err != nil {
2145
+ t .Fatalf ("unexpected error: %v" , err )
2146
+ }
2147
+ if string (body ) != expectedBody {
2148
+ t .Fatalf ("unexpected body %q. Expecting %q" , body , expectedBody )
2149
+ }
2150
+
2151
+ // verify br-compressed response
2152
+ ctx .Request .Reset ()
2153
+ ctx .Response .Reset ()
2154
+ ctx .Request .Header .Set (HeaderAcceptEncoding , "gzip, deflate, br" )
2155
+
2156
+ h (& ctx )
2157
+ s = ctx .Response .String ()
2158
+ br = bufio .NewReader (bytes .NewBufferString (s ))
2159
+ if err := resp .Read (br ); err != nil {
2160
+ t .Fatalf ("unexpected error: %v" , err )
2161
+ }
2162
+ ce = resp .Header .ContentEncoding ()
2163
+ if string (ce ) != "br" {
2164
+ t .Fatalf ("unexpected Content-Encoding: %q. Expecting %q" , ce , "br" )
2165
+ }
2166
+ vary = resp .Header .Peek ("Vary" )
2167
+ if string (vary ) != "Accept-Encoding" {
2168
+ t .Fatalf ("unexpected Vary: %q. Expecting %q" , vary , "Accept-Encoding" )
2169
+ }
2170
+ body , err = resp .BodyUnbrotli ()
2171
+ if err != nil {
2172
+ t .Fatalf ("unexpected error: %v" , err )
2173
+ }
2174
+ if string (body ) != expectedBody {
2175
+ t .Fatalf ("unexpected body %q. Expecting %q" , body , expectedBody )
2176
+ }
2177
+ }
2178
+
2038
2179
func TestRequestCtxWriteString (t * testing.T ) {
2039
2180
t .Parallel ()
2040
2181
0 commit comments