@@ -906,6 +906,111 @@ type noopHandler struct{}
906
906
907
907
func (noopHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {}
908
908
909
+ func TestWriteStringNoCompressionStatic (t * testing.T ) {
910
+ t .Parallel ()
911
+ var h http.Handler = http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
912
+ if w , ok := w .(interface { WriteString (string ) (int , error ) }); ok {
913
+ w .WriteString ("hello string world!" )
914
+ return
915
+ }
916
+ w .Write ([]byte ("hello bytes world!" ))
917
+ })
918
+ a , _ := DefaultAdapter ()
919
+ h = a (h )
920
+ // Do not send accept-encoding to disable compression
921
+ r , _ := http .NewRequest ("GET" , "/" , nil )
922
+ t .Run ("WriteString" , func (t * testing.T ) {
923
+ w := & discardResponseWriterWithWriteString {}
924
+ h .ServeHTTP (w , r )
925
+ if w .s != 19 {
926
+ t .Fatalf ("WriteString not called: %+v" , w )
927
+ }
928
+ })
929
+ t .Run ("Write" , func (t * testing.T ) {
930
+ w := & discardResponseWriter {}
931
+ h .ServeHTTP (w , r )
932
+ if w .b != 18 {
933
+ t .Fatalf ("Write not called: %+v" , w )
934
+ }
935
+ })
936
+ }
937
+
938
+ func TestWriteStringNoCompressionDynamic (t * testing.T ) {
939
+ t .Parallel ()
940
+ var h http.Handler = http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
941
+ w .Header ().Set ("Content-Type" , "text/uncompressible" )
942
+ if w , ok := w .(interface { WriteString (string ) (int , error ) }); ok {
943
+ w .WriteString (testBody ) // first WriteString will fallback to Write
944
+ w .WriteString (testBody )
945
+ return
946
+ }
947
+ w .Write ([]byte (testBody ))
948
+ w .Write ([]byte (testBody ))
949
+ })
950
+ a , _ := DefaultAdapter (ContentTypes ([]string {"text/uncompressible" }, true ))
951
+ h = a (h )
952
+ r , _ := http .NewRequest ("GET" , "/" , nil )
953
+ r .Header .Set ("Accept-Encoding" , "gzip" )
954
+ t .Run ("WriteString" , func (t * testing.T ) {
955
+ w := & discardResponseWriterWithWriteString {}
956
+ h .ServeHTTP (w , r )
957
+ if w .s != len (testBody ) || w .b != len (testBody ) { // first WriteString falls back to Write
958
+ t .Fatalf ("WriteString not called: %+v" , w )
959
+ }
960
+ })
961
+ t .Run ("Write" , func (t * testing.T ) {
962
+ w := & discardResponseWriter {}
963
+ h .ServeHTTP (w , r )
964
+ if w .b != len (testBody )* 2 {
965
+ t .Fatalf ("Write not called: %+v" , w )
966
+ }
967
+ })
968
+ }
969
+
970
+ type discardResponseWriterWithWriteString struct {
971
+ discardResponseWriter
972
+ s int
973
+ }
974
+
975
+ func (w * discardResponseWriterWithWriteString ) WriteString (s string ) (n int , err error ) {
976
+ w .s += len (s )
977
+ return len (s ), nil
978
+ }
979
+
980
+ func TestWriteStringEquivalence (t * testing.T ) {
981
+ t .Parallel ()
982
+
983
+ for _ , ae := range []string {"gzip" , "uncompressed" } {
984
+ for _ , ct := range []string {"text" , "uncompressible" } {
985
+ t .Run (fmt .Sprintf ("%s/%s" , ae , ct ), func (t * testing.T ) {
986
+ r , _ := http .NewRequest ("GET" , "/" , nil )
987
+ r .Header .Set ("Accept-Encoding" , ae )
988
+ a , _ := DefaultAdapter (ContentTypes ([]string {"uncompressible" }, true ))
989
+
990
+ var h http.Handler = http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
991
+ w .Header ().Set ("Content-Type" , ct )
992
+ w .(interface { WriteString (string ) (int , error ) }).WriteString (testBody )
993
+ w .(interface { WriteString (string ) (int , error ) }).WriteString (testBody )
994
+ })
995
+ h = a (h )
996
+ ws := httptest .NewRecorder ()
997
+ h .ServeHTTP (ws , r )
998
+
999
+ h = http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1000
+ w .Header ().Set ("Content-Type" , ct )
1001
+ w .Write ([]byte (testBody ))
1002
+ w .Write ([]byte (testBody ))
1003
+ })
1004
+ h = a (h )
1005
+ w := httptest .NewRecorder ()
1006
+ h .ServeHTTP (w , r )
1007
+
1008
+ assert .Equal (t , ws .Body .Bytes (), w .Body .Bytes (), "response body mismatch" )
1009
+ })
1010
+ }
1011
+ }
1012
+ }
1013
+
909
1014
// --------------------------------------------------------------------
910
1015
911
1016
func BenchmarkAdapter (b * testing.B ) {
0 commit comments