@@ -141,25 +141,59 @@ public Task BadRequestIfHostHeaderDoesNotMatchRequestTarget(string requestTarget
141
141
[ Fact ]
142
142
public async Task CanOptOutOfBadRequestIfHostHeaderDoesNotMatchRequestTarget ( )
143
143
{
144
+ BadHttpRequestException loggedException = null ;
145
+
146
+ var mockKestrelTrace = new Mock < IKestrelTrace > ( ) ;
147
+ mockKestrelTrace
148
+ . Setup ( trace => trace . IsEnabled ( LogLevel . Information ) )
149
+ . Returns ( true ) ;
150
+ mockKestrelTrace
151
+ . Setup ( trace => trace . ConnectionBadRequest ( It . IsAny < string > ( ) , It . IsAny < BadHttpRequestException > ( ) ) )
152
+ . Callback < string , BadHttpRequestException > ( ( connectionId , exception ) => loggedException = exception ) ;
153
+
154
+ // Set up a listener to catch the BadRequest event
155
+ var diagListener = new DiagnosticListener ( "OptOutBadRequestTestsDiagListener" ) ;
156
+ string eventProviderName = "" ;
157
+ string exceptionString = "" ;
158
+ var badRequestEventListener = new BadRequestEventListener ( diagListener , ( pair ) => {
159
+ eventProviderName = pair . Key ;
160
+ var featureCollection = pair . Value as IFeatureCollection ;
161
+ if ( featureCollection is not null )
162
+ {
163
+ var badRequestFeature = featureCollection . Get < IBadRequestExceptionFeature > ( ) ;
164
+ exceptionString = badRequestFeature . Error . ToString ( ) ;
165
+ }
166
+ } ) ;
167
+
144
168
var receivedHost = StringValues . Empty ;
145
169
await using var server = new TestServer ( context =>
146
170
{
147
171
receivedHost = context . Request . Headers . Host ;
148
172
return Task . CompletedTask ;
149
- } , new TestServiceContext ( LoggerFactory )
173
+ } , new TestServiceContext ( LoggerFactory , mockKestrelTrace . Object )
150
174
{
175
+ DiagnosticSource = diagListener ,
151
176
ServerOptions = new KestrelServerOptions ( )
152
177
{
153
178
EnableInsecureAbsoluteFormHostOverride = true ,
154
179
}
155
180
} ) ;
181
+
156
182
using var client = server . CreateConnection ( ) ;
157
183
158
184
await client . SendAll ( $ "GET http://www.foo.com/api/data HTTP/1.1\r \n Host: www.foo.comConnection: keep-alive\r \n \r \n ") ;
159
185
160
186
await client . Receive ( "HTTP/1.1 200 OK" ) ;
161
187
162
188
Assert . Equal ( "www.foo.com:80" , receivedHost ) ;
189
+
190
+ mockKestrelTrace . Verify ( trace => trace . ConnectionBadRequest ( It . IsAny < string > ( ) , It . IsAny < BadHttpRequestException > ( ) ) ) ;
191
+ Assert . Equal ( CoreStrings . FormatBadRequest_InvalidHostHeader_Detail ( "www.foo.comConnection: keep-alive" ) , loggedException . Message ) ;
192
+
193
+ // Verify DiagnosticSource event for bad request
194
+ Assert . True ( badRequestEventListener . EventFired ) ;
195
+ Assert . Equal ( "Microsoft.AspNetCore.Server.Kestrel.BadRequest" , eventProviderName ) ;
196
+ Assert . Contains ( CoreStrings . FormatBadRequest_InvalidHostHeader_Detail ( "www.foo.comConnection: keep-alive" ) , exceptionString ) ;
163
197
}
164
198
165
199
[ Fact ]
0 commit comments