1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
+ using System ;
5
+ using System . Collections . Generic ;
4
6
using System . Threading . Tasks ;
5
7
using Microsoft . AspNetCore . Http ;
6
8
using Microsoft . Extensions . Options ;
@@ -14,7 +16,11 @@ public class HeaderPropagationMiddlewareTest
14
16
public HeaderPropagationMiddlewareTest ( )
15
17
{
16
18
Context = new DefaultHttpContext ( ) ;
17
- Next = ctx => Task . CompletedTask ;
19
+ Next = ctx =>
20
+ {
21
+ CapturedHeaders = State . Headers ;
22
+ return Task . CompletedTask ;
23
+ } ;
18
24
Configuration = new HeaderPropagationOptions ( ) ;
19
25
State = new HeaderPropagationValues ( ) ;
20
26
Middleware = new HeaderPropagationMiddleware ( Next ,
@@ -24,8 +30,10 @@ public HeaderPropagationMiddlewareTest()
24
30
25
31
public DefaultHttpContext Context { get ; set ; }
26
32
public RequestDelegate Next { get ; set ; }
33
+ public Action Assertion { get ; set ; }
27
34
public HeaderPropagationOptions Configuration { get ; set ; }
28
35
public HeaderPropagationValues State { get ; set ; }
36
+ public IDictionary < string , StringValues > CapturedHeaders { get ; set ; }
29
37
public HeaderPropagationMiddleware Middleware { get ; set ; }
30
38
31
39
[ Fact ]
@@ -39,8 +47,8 @@ public async Task HeaderInRequest_AddCorrectValue()
39
47
await Middleware . Invoke ( Context ) ;
40
48
41
49
// Assert
42
- Assert . Contains ( "in" , State . Headers . Keys ) ;
43
- Assert . Equal ( new [ ] { "test" } , State . Headers [ "in" ] ) ;
50
+ Assert . Contains ( "in" , CapturedHeaders . Keys ) ;
51
+ Assert . Equal ( new [ ] { "test" } , CapturedHeaders [ "in" ] ) ;
44
52
}
45
53
46
54
[ Fact ]
@@ -53,7 +61,7 @@ public async Task NoHeaderInRequest_DoesNotAddIt()
53
61
await Middleware . Invoke ( Context ) ;
54
62
55
63
// Assert
56
- Assert . Empty ( State . Headers ) ;
64
+ Assert . Empty ( CapturedHeaders ) ;
57
65
}
58
66
59
67
[ Fact ]
@@ -66,7 +74,7 @@ public async Task HeaderInRequest_NotInOptions_DoesNotAddIt()
66
74
await Middleware . Invoke ( Context ) ;
67
75
68
76
// Assert
69
- Assert . Empty ( State . Headers ) ;
77
+ Assert . Empty ( CapturedHeaders ) ;
70
78
}
71
79
72
80
[ Fact ]
@@ -82,10 +90,10 @@ public async Task MultipleHeadersInRequest_AddAllHeaders()
82
90
await Middleware . Invoke ( Context ) ;
83
91
84
92
// Assert
85
- Assert . Contains ( "in" , State . Headers . Keys ) ;
86
- Assert . Equal ( new [ ] { "test" } , State . Headers [ "in" ] ) ;
87
- Assert . Contains ( "another" , State . Headers . Keys ) ;
88
- Assert . Equal ( new [ ] { "test2" } , State . Headers [ "another" ] ) ;
93
+ Assert . Contains ( "in" , CapturedHeaders . Keys ) ;
94
+ Assert . Equal ( new [ ] { "test" } , CapturedHeaders [ "in" ] ) ;
95
+ Assert . Contains ( "another" , CapturedHeaders . Keys ) ;
96
+ Assert . Equal ( new [ ] { "test2" } , CapturedHeaders [ "another" ] ) ;
89
97
}
90
98
91
99
[ Theory ]
@@ -101,7 +109,7 @@ public async Task HeaderEmptyInRequest_DoesNotAddIt(string headerValue)
101
109
await Middleware . Invoke ( Context ) ;
102
110
103
111
// Assert
104
- Assert . DoesNotContain ( "in" , State . Headers . Keys ) ;
112
+ Assert . DoesNotContain ( "in" , CapturedHeaders . Keys ) ;
105
113
}
106
114
107
115
[ Theory ]
@@ -127,8 +135,8 @@ public async Task UsesValueFilter(string[] filterValues, string[] expectedValues
127
135
await Middleware . Invoke ( Context ) ;
128
136
129
137
// Assert
130
- Assert . Contains ( "in" , State . Headers . Keys ) ;
131
- Assert . Equal ( expectedValues , State . Headers [ "in" ] ) ;
138
+ Assert . Contains ( "in" , CapturedHeaders . Keys ) ;
139
+ Assert . Equal ( expectedValues , CapturedHeaders [ "in" ] ) ;
132
140
Assert . Equal ( "in" , receivedName ) ;
133
141
Assert . Equal ( new StringValues ( "value" ) , receivedValue ) ;
134
142
Assert . Same ( Context , receivedContext ) ;
@@ -145,8 +153,8 @@ public async Task PreferValueFilter_OverRequestHeader()
145
153
await Middleware . Invoke ( Context ) ;
146
154
147
155
// Assert
148
- Assert . Contains ( "in" , State . Headers . Keys ) ;
149
- Assert . Equal ( "test" , State . Headers [ "in" ] ) ;
156
+ Assert . Contains ( "in" , CapturedHeaders . Keys ) ;
157
+ Assert . Equal ( "test" , CapturedHeaders [ "in" ] ) ;
150
158
}
151
159
152
160
[ Fact ]
@@ -159,7 +167,7 @@ public async Task EmptyValuesFromValueFilter_DoesNotAddIt()
159
167
await Middleware . Invoke ( Context ) ;
160
168
161
169
// Assert
162
- Assert . DoesNotContain ( "in" , State . Headers . Keys ) ;
170
+ Assert . DoesNotContain ( "in" , CapturedHeaders . Keys ) ;
163
171
}
164
172
165
173
[ Fact ]
@@ -174,8 +182,46 @@ public async Task MultipleEntries_AddsFirstToProduceValue()
174
182
await Middleware . Invoke ( Context ) ;
175
183
176
184
// Assert
177
- Assert . Contains ( "in" , State . Headers . Keys ) ;
178
- Assert . Equal ( "Test" , State . Headers [ "in" ] ) ;
185
+ Assert . Contains ( "in" , CapturedHeaders . Keys ) ;
186
+ Assert . Equal ( "Test" , CapturedHeaders [ "in" ] ) ;
187
+ }
188
+
189
+ [ Fact ]
190
+ public async Task HeaderInRequest_WithBleedAsyncLocal_HasCorrectValue ( )
191
+ {
192
+ // Arrange
193
+ Configuration . Headers . Add ( "in" ) ;
194
+
195
+ // Process first request
196
+ Context . Request . Headers . Add ( "in" , "dirty" ) ;
197
+ await Middleware . Invoke ( Context ) ;
198
+
199
+ // Process second request
200
+ Context = new DefaultHttpContext ( ) ;
201
+ Context . Request . Headers . Add ( "in" , "test" ) ;
202
+ await Middleware . Invoke ( Context ) ;
203
+
204
+ // Assert
205
+ Assert . Contains ( "in" , CapturedHeaders . Keys ) ;
206
+ Assert . Equal ( new [ ] { "test" } , CapturedHeaders [ "in" ] ) ;
207
+ }
208
+
209
+ [ Fact ]
210
+ public async Task NoHeaderInRequest_WithBleedAsyncLocal_DoesNotHaveIt ( )
211
+ {
212
+ // Arrange
213
+ Configuration . Headers . Add ( "in" ) ;
214
+
215
+ // Process first request
216
+ Context . Request . Headers . Add ( "in" , "dirty" ) ;
217
+ await Middleware . Invoke ( Context ) ;
218
+
219
+ // Process second request
220
+ Context = new DefaultHttpContext ( ) ;
221
+ await Middleware . Invoke ( Context ) ;
222
+
223
+ // Assert
224
+ Assert . Empty ( CapturedHeaders ) ;
179
225
}
180
226
}
181
227
}
0 commit comments