11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System . Net . Http ;
45using Microsoft . AspNetCore . Http . Timeouts ;
56using Microsoft . Extensions . Logging . Abstractions ;
67using Microsoft . Extensions . Options ;
@@ -20,7 +21,6 @@ public async Task DefaultTimeoutWhenNoEndpoint()
2021 await middlewre . Invoke ( context ) ;
2122
2223 Assert . Equal ( originalToken , context . RequestAborted ) ;
23- CheckTimeoutFeature ( context , shouldExist : true ) ;
2424 }
2525
2626 [ Fact ]
@@ -37,7 +37,6 @@ public async Task DefaultTimeoutWhenNoMetadata()
3737 await middlewre . Invoke ( context ) ;
3838
3939 Assert . Equal ( originalToken , context . RequestAborted ) ;
40- CheckTimeoutFeature ( context , shouldExist : true ) ;
4140 }
4241
4342 [ Fact ]
@@ -54,7 +53,6 @@ public async Task TimeOutFromMetadataPolicy()
5453 await middlewre . Invoke ( context ) ;
5554
5655 Assert . Equal ( originalToken , context . RequestAborted ) ;
57- CheckTimeoutFeature ( context , shouldExist : true ) ;
5856 }
5957
6058 [ Fact ]
@@ -70,7 +68,6 @@ public async Task TimeOutFromMetadataAttributeWithPolicy()
7068 await middlewre . Invoke ( context ) ;
7169
7270 Assert . Equal ( originalToken , context . RequestAborted ) ;
73- CheckTimeoutFeature ( context , shouldExist : true ) ;
7471 }
7572
7673 [ Fact ]
@@ -86,22 +83,23 @@ public async Task TimeOutFromMetadataAttributeWithTimeSpan()
8683 await middlewre . Invoke ( context ) ;
8784
8885 Assert . Equal ( originalToken , context . RequestAborted ) ;
89- CheckTimeoutFeature ( context , shouldExist : true ) ;
9086 }
9187
9288 [ Fact ]
9389 public async Task SkipWhenNoDefaultDefaultTimeout ( )
9490 {
9591 var context = new DefaultHttpContext ( ) ;
9692
97- var middlewre = CreateMiddleware ( originalCancellationToken : context . RequestAborted , linkerCalled : false ) ;
93+ var middlewre = CreateMiddleware (
94+ originalCancellationToken : context . RequestAborted ,
95+ linkerCalled : false ,
96+ timeoutFeatureExists : false ) ;
9897
9998 var originalToken = context . RequestAborted ;
10099
101100 await middlewre . Invoke ( context ) ;
102101
103102 Assert . Equal ( originalToken , context . RequestAborted ) ;
104- CheckTimeoutFeature ( context , shouldExist : false ) ;
105103 }
106104
107105 [ Fact ]
@@ -118,7 +116,6 @@ public async Task TimeOutsAttributeWithPolicyWinsOverDefault()
118116 await middlewre . Invoke ( context ) ;
119117
120118 Assert . Equal ( originalToken , context . RequestAborted ) ;
121- CheckTimeoutFeature ( context , shouldExist : true ) ;
122119 }
123120
124121 [ Fact ]
@@ -135,7 +132,6 @@ public async Task TimeOutsAttributeWithTimeSpanWinsOverDefault()
135132 await middlewre . Invoke ( context ) ;
136133
137134 Assert . Equal ( originalToken , context . RequestAborted ) ;
138- CheckTimeoutFeature ( context , shouldExist : true ) ;
139135 }
140136
141137 [ Fact ]
@@ -152,7 +148,6 @@ public async Task TimeOutsPolicyWinsOverDefault()
152148 await middlewre . Invoke ( context ) ;
153149
154150 Assert . Equal ( originalToken , context . RequestAborted ) ;
155- CheckTimeoutFeature ( context , shouldExist : true ) ;
156151 }
157152
158153 [ Fact ]
@@ -161,7 +156,10 @@ public async Task DisableTimeoutAttributeSkipTheMiddleware()
161156 var context = new DefaultHttpContext ( ) ;
162157 var originalToken = context . RequestAborted ;
163158
164- var middlewre = CreateMiddleware ( defaultTimeout : 10 , originalCancellationToken : originalToken , linkerCalled : false ) ;
159+ var middlewre = CreateMiddleware ( defaultTimeout : 10 ,
160+ originalCancellationToken : originalToken ,
161+ linkerCalled : false ,
162+ timeoutFeatureExists : false ) ;
165163
166164 var endpoint = CreateEndpoint ( new DisableRequestTimeoutAttribute ( ) ,
167165 new RequestTimeoutPolicy { Timeout = TimeSpan . FromSeconds ( 47 ) } ,
@@ -171,7 +169,6 @@ public async Task DisableTimeoutAttributeSkipTheMiddleware()
171169 await middlewre . Invoke ( context ) ;
172170
173171 Assert . Equal ( originalToken , context . RequestAborted ) ;
174- CheckTimeoutFeature ( context , shouldExist : false ) ;
175172 }
176173
177174 [ Fact ]
@@ -257,12 +254,6 @@ public async Task SkipHandleTimeoutException()
257254 Assert . Equal ( originalToken , context . RequestAborted ) ;
258255 }
259256
260- private static void CheckTimeoutFeature ( HttpContext httpContext , bool shouldExist )
261- {
262- var timeoutFeature = httpContext . Features . Get < IHttpRequestTimeoutFeature > ( ) ;
263- Assert . Equal ( shouldExist , timeoutFeature is not null ) ;
264- }
265-
266257 private static RequestTimeoutsMiddleware CreateMiddlewareWithCancel (
267258 double ? expectedTimeSpan = null ,
268259 double ? defaultTimeout = null ,
@@ -288,7 +279,8 @@ private static RequestTimeoutsMiddleware CreateMiddleware(
288279 double ? defaultTimeout = null ,
289280 bool cancelledCts = false ,
290281 CancellationToken originalCancellationToken = default ,
291- bool linkerCalled = true )
282+ bool linkerCalled = true ,
283+ bool timeoutFeatureExists = true )
292284 {
293285 var ctsLinker = new MockCancellationTokenSourceProvider ( expectedTimeSpan . HasValue ? TimeSpan . FromSeconds ( expectedTimeSpan . Value ) : null , cancelledCts ) ;
294286 var options = new RequestTimeoutOptions
@@ -331,6 +323,9 @@ private static RequestTimeoutsMiddleware CreateMiddleware(
331323
332324 Task next ( HttpContext context )
333325 {
326+ var timeoutFeature = context . Features . Get < IHttpRequestTimeoutFeature > ( ) ;
327+ Assert . Equal ( timeoutFeatureExists , timeoutFeature is not null ) ;
328+
334329 Assert . Equal ( linkerCalled , ctsLinker . Called ) ;
335330 if ( ctsLinker . Called )
336331 {
0 commit comments