@@ -76,6 +76,25 @@ public static IEnumerable<object[]> ValidHttpMessageOptions
7676 HttpResponseMessage = SomeFakeResponse
7777 }
7878 } ;
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+ } ;
7998 }
8099 }
81100
@@ -118,17 +137,79 @@ public static IEnumerable<object[]> ValidSomeHttpMessageOptions
118137 }
119138 }
120139
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+
121199 [ Theory ]
122200 [ MemberData ( nameof ( ValidHttpMessageOptions ) ) ]
123201 public async Task GivenAnHttpMessageOptions_GetAsync_ReturnsAFakeResponse ( HttpMessageOptions options )
124202 {
125203 // Arrange.
126204 var fakeHttpMessageHandler = new FakeHttpMessageHandler ( options ) ;
127205
128- // Act & Assert .
206+ // Act.
129207 await DoGetAsync ( RequestUri ,
130208 ExpectedContent ,
131- fakeHttpMessageHandler ) ;
209+ fakeHttpMessageHandler ,
210+ options . Headers ) ;
211+
212+ // Assert.
132213 options . NumberOfTimesCalled . ShouldBe ( 1 ) ;
133214 }
134215
@@ -274,9 +355,37 @@ await DoGetAsync(RequestUri,
274355 options . NumberOfTimesCalled . ShouldBe ( 3 ) ;
275356 }
276357
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+
277385 private static async Task DoGetAsync ( string requestUri ,
278386 string expectedResponseContent ,
279- FakeHttpMessageHandler fakeHttpMessageHandler )
387+ FakeHttpMessageHandler fakeHttpMessageHandler ,
388+ IDictionary < string , IEnumerable < string > > optionalHeaders = null )
280389 {
281390 requestUri . ShouldNotBeNullOrWhiteSpace ( ) ;
282391 expectedResponseContent . ShouldNotBeNullOrWhiteSpace ( ) ;
@@ -286,6 +395,16 @@ private static async Task DoGetAsync(string requestUri,
286395 string content ;
287396 using ( var httpClient = new System . Net . Http . HttpClient ( fakeHttpMessageHandler ) )
288397 {
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+
289408 // Act.
290409 message = await httpClient . GetAsync ( requestUri ) ;
291410 content = await message . Content . ReadAsStringAsync ( ) ;
0 commit comments