@@ -119,6 +119,25 @@ public async Task Request_AcceptUnknown_NotCompressed()
119
119
AssertLog ( logMessages . Skip ( 2 ) . First ( ) , LogLevel . Debug , "No matching response compression provider found." ) ;
120
120
}
121
121
122
+ [ Fact ]
123
+ public async Task RequestHead_NoAcceptEncoding_Uncompressed ( )
124
+ {
125
+ var ( response , logMessages ) = await InvokeMiddleware ( 100 , requestAcceptEncodings : null , responseType : TextPlain , httpMethod : HttpMethods . Head ) ;
126
+
127
+ CheckResponseNotCompressed ( response , expectedBodyLength : 100 , sendVaryHeader : false ) ;
128
+ AssertLog ( logMessages . Single ( ) , LogLevel . Debug , "No response compression available, the Accept-Encoding header is missing or invalid." ) ;
129
+ }
130
+
131
+ [ Fact ]
132
+ public async Task RequestHead_AcceptGzipDeflate_CompressedGzip ( )
133
+ {
134
+ var ( response , logMessages ) = await InvokeMiddleware ( 100 , requestAcceptEncodings : new [ ] { "gzip" , "deflate" } , responseType : TextPlain , httpMethod : HttpMethods . Head ) ;
135
+
136
+ // Per RFC 7231, section 4.3.2, the Content-Lenght header can be omitted on HEAD requests.
137
+ CheckResponseCompressed ( response , expectedBodyLength : null , expectedEncoding : "gzip" ) ;
138
+ AssertCompressedWithLog ( logMessages , "gzip" ) ;
139
+ }
140
+
122
141
[ Theory ]
123
142
[ InlineData ( "text/plain" ) ]
124
143
[ InlineData ( "text/PLAIN" ) ]
@@ -1154,7 +1173,8 @@ public async Task Dispose_SyncWriteOrFlushNotCalled(string encoding)
1154
1173
string [ ] requestAcceptEncodings ,
1155
1174
string responseType ,
1156
1175
Action < HttpResponse > addResponseAction = null ,
1157
- Action < ResponseCompressionOptions > configure = null )
1176
+ Action < ResponseCompressionOptions > configure = null ,
1177
+ string httpMethod = "GET" )
1158
1178
{
1159
1179
var sink = new TestSink (
1160
1180
TestSink . EnableWithTypeName < ResponseCompressionProvider > ,
@@ -1178,6 +1198,13 @@ public async Task Dispose_SyncWriteOrFlushNotCalled(string encoding)
1178
1198
{
1179
1199
context . Response . Headers [ HeaderNames . ContentMD5 ] = "MD5" ;
1180
1200
context . Response . ContentType = responseType ;
1201
+
1202
+ if ( HttpMethods . IsHead ( context . Request . Method ) )
1203
+ {
1204
+ context . Response . ContentLength = uncompressedBodyLength ;
1205
+ return Task . CompletedTask ;
1206
+ }
1207
+
1181
1208
addResponseAction ? . Invoke ( context . Response ) ;
1182
1209
return context . Response . WriteAsync ( new string ( 'a' , uncompressedBodyLength ) ) ;
1183
1210
} ) ;
@@ -1189,7 +1216,7 @@ public async Task Dispose_SyncWriteOrFlushNotCalled(string encoding)
1189
1216
var server = host . GetTestServer ( ) ;
1190
1217
var client = server . CreateClient ( ) ;
1191
1218
1192
- var request = new HttpRequestMessage ( HttpMethod . Get , "" ) ;
1219
+ var request = new HttpRequestMessage ( new HttpMethod ( httpMethod ) , "" ) ;
1193
1220
for ( var i = 0 ; i < requestAcceptEncodings ? . Length ; i ++ )
1194
1221
{
1195
1222
request . Headers . AcceptEncoding . Add ( System . Net . Http . Headers . StringWithQualityHeaderValue . Parse ( requestAcceptEncodings [ i ] ) ) ;
@@ -1200,7 +1227,7 @@ public async Task Dispose_SyncWriteOrFlushNotCalled(string encoding)
1200
1227
return ( response , sink . Writes . ToList ( ) ) ;
1201
1228
}
1202
1229
1203
- private void CheckResponseCompressed ( HttpResponseMessage response , int expectedBodyLength , string expectedEncoding )
1230
+ private void CheckResponseCompressed ( HttpResponseMessage response , long ? expectedBodyLength , string expectedEncoding )
1204
1231
{
1205
1232
var containsVaryAcceptEncoding = false ;
1206
1233
foreach ( var value in response . Headers . GetValues ( HeaderNames . Vary ) )
@@ -1217,7 +1244,7 @@ private void CheckResponseCompressed(HttpResponseMessage response, int expectedB
1217
1244
Assert . Equal ( expectedBodyLength , response . Content . Headers . ContentLength ) ;
1218
1245
}
1219
1246
1220
- private void CheckResponseNotCompressed ( HttpResponseMessage response , int expectedBodyLength , bool sendVaryHeader )
1247
+ private void CheckResponseNotCompressed ( HttpResponseMessage response , long ? expectedBodyLength , bool sendVaryHeader )
1221
1248
{
1222
1249
if ( sendVaryHeader )
1223
1250
{
0 commit comments