@@ -76,6 +76,25 @@ public static IEnumerable<object[]> ValidHttpMessageOptions
76
76
HttpResponseMessage = SomeFakeResponse
77
77
}
78
78
} ;
79
+
80
+ // Has to match GET + URI + Header
81
+ yield return new object [ ]
82
+ {
83
+ new HttpMessageOptions
84
+ {
85
+ HttpMethod = HttpMethod . Get ,
86
+ RequestUri = RequestUri ,
87
+ Headers = new Dictionary < string , IEnumerable < string > >
88
+ {
89
+ { "Bearer" , new [ ]
90
+ {
91
+ "pewpew"
92
+ }
93
+ }
94
+ } ,
95
+ HttpResponseMessage = SomeFakeResponse
96
+ }
97
+ } ;
79
98
}
80
99
}
81
100
@@ -118,17 +137,79 @@ public static IEnumerable<object[]> ValidSomeHttpMessageOptions
118
137
}
119
138
}
120
139
140
+
141
+ public static IEnumerable < object [ ] > DifferentHttpMessageOptions
142
+ {
143
+ get
144
+ {
145
+ yield return new object [ ]
146
+ {
147
+ // Different uri.
148
+ new HttpMessageOptions
149
+ {
150
+ RequestUri = "http://this.is.a.different.website"
151
+ }
152
+ } ;
153
+
154
+ yield return new object [ ]
155
+ {
156
+ // Different Method.
157
+ new HttpMessageOptions
158
+ {
159
+ HttpMethod = HttpMethod . Head
160
+ }
161
+ } ;
162
+
163
+ yield return new object [ ]
164
+ {
165
+ // Different header (different key).
166
+ new HttpMessageOptions
167
+ {
168
+ Headers = new Dictionary < string , IEnumerable < string > >
169
+ {
170
+ {
171
+ "xxxx" , new [ ]
172
+ {
173
+ "pewpew"
174
+ }
175
+ }
176
+ }
177
+ }
178
+ } ;
179
+
180
+ yield return new object [ ]
181
+ {
182
+ // Different header (found key, different content).
183
+ new HttpMessageOptions
184
+ {
185
+ Headers = new Dictionary < string , IEnumerable < string > >
186
+ {
187
+ {
188
+ "Bearer" , new [ ]
189
+ {
190
+ "pewpew"
191
+ }
192
+ }
193
+ }
194
+ }
195
+ } ;
196
+ }
197
+ }
198
+
121
199
[ Theory ]
122
200
[ MemberData ( nameof ( ValidHttpMessageOptions ) ) ]
123
201
public async Task GivenAnHttpMessageOptions_GetAsync_ReturnsAFakeResponse ( HttpMessageOptions options )
124
202
{
125
203
// Arrange.
126
204
var fakeHttpMessageHandler = new FakeHttpMessageHandler ( options ) ;
127
205
128
- // Act & Assert .
206
+ // Act.
129
207
await DoGetAsync ( RequestUri ,
130
208
ExpectedContent ,
131
- fakeHttpMessageHandler ) ;
209
+ fakeHttpMessageHandler ,
210
+ options . Headers ) ;
211
+
212
+ // Assert.
132
213
options . NumberOfTimesCalled . ShouldBe ( 1 ) ;
133
214
}
134
215
@@ -274,9 +355,37 @@ await DoGetAsync(RequestUri,
274
355
options . NumberOfTimesCalled . ShouldBe ( 3 ) ;
275
356
}
276
357
358
+ [ Theory ]
359
+ [ MemberData ( nameof ( DifferentHttpMessageOptions ) ) ]
360
+ public async Task GivenSomeDifferentHttpMessageOptions_GetAsync_ShouldThrowAnException ( HttpMessageOptions options )
361
+ {
362
+ // Arrange.
363
+ var fakeHttpMessageHandler = new FakeHttpMessageHandler ( options ) ;
364
+ var headers = new Dictionary < string , IEnumerable < string > >
365
+ {
366
+ {
367
+ "hi" , new [ ]
368
+ {
369
+ "there"
370
+ }
371
+ }
372
+ } ;
373
+
374
+ // Act.
375
+ var exception = await Should . ThrowAsync < Exception > ( ( ) => DoGetAsync ( RequestUri ,
376
+ ExpectedContent ,
377
+ fakeHttpMessageHandler ,
378
+ headers ) ) ;
379
+
380
+ // Assert.
381
+ exception . Message . ShouldStartWith ( "No HttpResponseMessage found for the Request Uri:" ) ;
382
+ options . NumberOfTimesCalled . ShouldBe ( 0 ) ;
383
+ }
384
+
277
385
private static async Task DoGetAsync ( string requestUri ,
278
386
string expectedResponseContent ,
279
- FakeHttpMessageHandler fakeHttpMessageHandler )
387
+ FakeHttpMessageHandler fakeHttpMessageHandler ,
388
+ IDictionary < string , IEnumerable < string > > optionalHeaders = null )
280
389
{
281
390
requestUri . ShouldNotBeNullOrWhiteSpace ( ) ;
282
391
expectedResponseContent . ShouldNotBeNullOrWhiteSpace ( ) ;
@@ -286,6 +395,16 @@ private static async Task DoGetAsync(string requestUri,
286
395
string content ;
287
396
using ( var httpClient = new System . Net . Http . HttpClient ( fakeHttpMessageHandler ) )
288
397
{
398
+ // Do we have any Headers?
399
+ if ( optionalHeaders != null &&
400
+ optionalHeaders . Any ( ) )
401
+ {
402
+ foreach ( var keyValue in optionalHeaders )
403
+ {
404
+ httpClient . DefaultRequestHeaders . Add ( keyValue . Key , keyValue . Value ) ;
405
+ }
406
+ }
407
+
289
408
// Act.
290
409
message = await httpClient . GetAsync ( requestUri ) ;
291
410
content = await message . Content . ReadAsStringAsync ( ) ;
0 commit comments