1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Net . Http ;
4
5
using Microsoft . AspNetCore . Http . Timeouts ;
5
6
using Microsoft . Extensions . Logging . Abstractions ;
6
7
using Microsoft . Extensions . Options ;
@@ -20,7 +21,6 @@ public async Task DefaultTimeoutWhenNoEndpoint()
20
21
await middlewre . Invoke ( context ) ;
21
22
22
23
Assert . Equal ( originalToken , context . RequestAborted ) ;
23
- CheckTimeoutFeature ( context , shouldExist : true ) ;
24
24
}
25
25
26
26
[ Fact ]
@@ -37,7 +37,6 @@ public async Task DefaultTimeoutWhenNoMetadata()
37
37
await middlewre . Invoke ( context ) ;
38
38
39
39
Assert . Equal ( originalToken , context . RequestAborted ) ;
40
- CheckTimeoutFeature ( context , shouldExist : true ) ;
41
40
}
42
41
43
42
[ Fact ]
@@ -54,7 +53,6 @@ public async Task TimeOutFromMetadataPolicy()
54
53
await middlewre . Invoke ( context ) ;
55
54
56
55
Assert . Equal ( originalToken , context . RequestAborted ) ;
57
- CheckTimeoutFeature ( context , shouldExist : true ) ;
58
56
}
59
57
60
58
[ Fact ]
@@ -70,7 +68,6 @@ public async Task TimeOutFromMetadataAttributeWithPolicy()
70
68
await middlewre . Invoke ( context ) ;
71
69
72
70
Assert . Equal ( originalToken , context . RequestAborted ) ;
73
- CheckTimeoutFeature ( context , shouldExist : true ) ;
74
71
}
75
72
76
73
[ Fact ]
@@ -86,22 +83,23 @@ public async Task TimeOutFromMetadataAttributeWithTimeSpan()
86
83
await middlewre . Invoke ( context ) ;
87
84
88
85
Assert . Equal ( originalToken , context . RequestAborted ) ;
89
- CheckTimeoutFeature ( context , shouldExist : true ) ;
90
86
}
91
87
92
88
[ Fact ]
93
89
public async Task SkipWhenNoDefaultDefaultTimeout ( )
94
90
{
95
91
var context = new DefaultHttpContext ( ) ;
96
92
97
- var middlewre = CreateMiddleware ( originalCancellationToken : context . RequestAborted , linkerCalled : false ) ;
93
+ var middlewre = CreateMiddleware (
94
+ originalCancellationToken : context . RequestAborted ,
95
+ linkerCalled : false ,
96
+ timeoutFeatureExists : false ) ;
98
97
99
98
var originalToken = context . RequestAborted ;
100
99
101
100
await middlewre . Invoke ( context ) ;
102
101
103
102
Assert . Equal ( originalToken , context . RequestAborted ) ;
104
- CheckTimeoutFeature ( context , shouldExist : false ) ;
105
103
}
106
104
107
105
[ Fact ]
@@ -118,7 +116,6 @@ public async Task TimeOutsAttributeWithPolicyWinsOverDefault()
118
116
await middlewre . Invoke ( context ) ;
119
117
120
118
Assert . Equal ( originalToken , context . RequestAborted ) ;
121
- CheckTimeoutFeature ( context , shouldExist : true ) ;
122
119
}
123
120
124
121
[ Fact ]
@@ -135,7 +132,6 @@ public async Task TimeOutsAttributeWithTimeSpanWinsOverDefault()
135
132
await middlewre . Invoke ( context ) ;
136
133
137
134
Assert . Equal ( originalToken , context . RequestAborted ) ;
138
- CheckTimeoutFeature ( context , shouldExist : true ) ;
139
135
}
140
136
141
137
[ Fact ]
@@ -152,7 +148,6 @@ public async Task TimeOutsPolicyWinsOverDefault()
152
148
await middlewre . Invoke ( context ) ;
153
149
154
150
Assert . Equal ( originalToken , context . RequestAborted ) ;
155
- CheckTimeoutFeature ( context , shouldExist : true ) ;
156
151
}
157
152
158
153
[ Fact ]
@@ -161,7 +156,10 @@ public async Task DisableTimeoutAttributeSkipTheMiddleware()
161
156
var context = new DefaultHttpContext ( ) ;
162
157
var originalToken = context . RequestAborted ;
163
158
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 ) ;
165
163
166
164
var endpoint = CreateEndpoint ( new DisableRequestTimeoutAttribute ( ) ,
167
165
new RequestTimeoutPolicy { Timeout = TimeSpan . FromSeconds ( 47 ) } ,
@@ -171,7 +169,6 @@ public async Task DisableTimeoutAttributeSkipTheMiddleware()
171
169
await middlewre . Invoke ( context ) ;
172
170
173
171
Assert . Equal ( originalToken , context . RequestAborted ) ;
174
- CheckTimeoutFeature ( context , shouldExist : false ) ;
175
172
}
176
173
177
174
[ Fact ]
@@ -257,12 +254,6 @@ public async Task SkipHandleTimeoutException()
257
254
Assert . Equal ( originalToken , context . RequestAborted ) ;
258
255
}
259
256
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
-
266
257
private static RequestTimeoutsMiddleware CreateMiddlewareWithCancel (
267
258
double ? expectedTimeSpan = null ,
268
259
double ? defaultTimeout = null ,
@@ -288,7 +279,8 @@ private static RequestTimeoutsMiddleware CreateMiddleware(
288
279
double ? defaultTimeout = null ,
289
280
bool cancelledCts = false ,
290
281
CancellationToken originalCancellationToken = default ,
291
- bool linkerCalled = true )
282
+ bool linkerCalled = true ,
283
+ bool timeoutFeatureExists = true )
292
284
{
293
285
var ctsLinker = new MockCancellationTokenSourceProvider ( expectedTimeSpan . HasValue ? TimeSpan . FromSeconds ( expectedTimeSpan . Value ) : null , cancelledCts ) ;
294
286
var options = new RequestTimeoutOptions
@@ -331,6 +323,9 @@ private static RequestTimeoutsMiddleware CreateMiddleware(
331
323
332
324
Task next ( HttpContext context )
333
325
{
326
+ var timeoutFeature = context . Features . Get < IHttpRequestTimeoutFeature > ( ) ;
327
+ Assert . Equal ( timeoutFeatureExists , timeoutFeature is not null ) ;
328
+
334
329
Assert . Equal ( linkerCalled , ctsLinker . Called ) ;
335
330
if ( ctsLinker . Called )
336
331
{
0 commit comments