11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Net ;
45using System . Net . Http ;
56using System . Threading . Tasks ;
@@ -119,72 +120,103 @@ public static IEnumerable<object[]> ValidSomeHttpMessageOptions
119120
120121 [ Theory ]
121122 [ MemberData ( nameof ( ValidHttpMessageOptions ) ) ]
122- public async Task GivenAnHttpMessageOptions_GetAsync_ReturnsAFakeResponse ( HttpMessageOptions option )
123+ public async Task GivenAnHttpMessageOptions_GetAsync_ReturnsAFakeResponse ( HttpMessageOptions options )
123124 {
124- var fakeHttpMessageHandler = new FakeHttpMessageHandler ( option ) ;
125+ // Arrange.
126+ var fakeHttpMessageHandler = new FakeHttpMessageHandler ( options ) ;
125127
128+ // Act & Assert.
126129 await DoGetAsync ( RequestUri ,
127130 ExpectedContent ,
128131 fakeHttpMessageHandler ) ;
132+ options . NumberOfTimesCalled . ShouldBe ( 1 ) ;
129133 }
130134
131135 [ Theory ]
132136 [ MemberData ( nameof ( ValidSomeHttpMessageOptions ) ) ]
133- public async Task GivenSomeHttpMessageOptions_GetAsync_ReturnsAFakeResponse ( IEnumerable < HttpMessageOptions > lotsOfOption )
137+ public async Task GivenSomeHttpMessageOptions_GetAsync_ReturnsAFakeResponse ( IList < HttpMessageOptions > lotsOfOptions )
134138 {
135- var fakeHttpMessageHandler = new FakeHttpMessageHandler ( lotsOfOption ) ;
139+ // Arrange.
140+ var fakeHttpMessageHandler = new FakeHttpMessageHandler ( lotsOfOptions ) ;
136141
142+ // Act & Assert.
137143 await DoGetAsync ( RequestUri ,
138144 ExpectedContent ,
139145 fakeHttpMessageHandler ) ;
146+ lotsOfOptions . Sum ( x => x . NumberOfTimesCalled ) . ShouldBe ( 1 ) ;
140147 }
141148
142149 [ Fact ]
143150 public async Task GivenAnHttpResponseMessage_GetAsync_ReturnsAFakeResponse ( )
144151 {
152+ // Arrange.
145153 var httpResponseMessage = FakeHttpMessageHandler . GetStringHttpResponseMessage ( ExpectedContent ) ;
146- var fakeHttpMessageHandler = new FakeHttpMessageHandler ( httpResponseMessage ) ;
154+ var options = new HttpMessageOptions
155+ {
156+ HttpResponseMessage = httpResponseMessage
157+ } ;
158+ var fakeHttpMessageHandler = new FakeHttpMessageHandler ( options ) ;
147159
160+ // Act & Assert.
148161 await DoGetAsync ( RequestUri ,
149162 ExpectedContent ,
150163 fakeHttpMessageHandler ) ;
164+ options . NumberOfTimesCalled . ShouldBe ( 1 ) ;
151165 }
152166
153167 [ Fact ]
154168 public async Task GivenSomeHttpResponseMessages_GetAsync_ReturnsAFakeResponse ( )
155169 {
156- const string requestUrl1 = RequestUri ;
157- const string responseData1 = ExpectedContent ;
158- var messageResponse1 = FakeHttpMessageHandler . GetStringHttpResponseMessage ( responseData1 ) ;
170+ // Arrange.
171+ var messageResponse1 = FakeHttpMessageHandler . GetStringHttpResponseMessage ( ExpectedContent ) ;
159172
160- const string requestUrl2 = "http://www.something.com/another/site" ;
161173 const string responseData2 = "Html, I am not." ;
162174 var messageResponse2 = FakeHttpMessageHandler . GetStringHttpResponseMessage ( responseData2 ) ;
163175
164- const string requestUrl3 = "http://www.whatever.com/" ;
165176 const string responseData3 = "<html><head><body>pew pew</body></head>" ;
166177 var messageResponse3 = FakeHttpMessageHandler . GetStringHttpResponseMessage ( responseData3 ) ;
167178
168- var messageResponses = new Dictionary < string , HttpResponseMessage >
179+ var options = new List < HttpMessageOptions >
169180 {
170- { requestUrl1 , messageResponse1 } ,
171- { requestUrl2 , messageResponse2 } ,
172- { requestUrl3 , messageResponse3 }
181+ new HttpMessageOptions
182+ {
183+ RequestUri = RequestUri ,
184+ HttpResponseMessage = messageResponse1
185+ } ,
186+ new HttpMessageOptions
187+ {
188+ RequestUri = "http://www.something.com/another/site" ,
189+ HttpResponseMessage = messageResponse2
190+ } ,
191+ new HttpMessageOptions
192+ {
193+ RequestUri = "http://www.whatever.com/" ,
194+ HttpResponseMessage = messageResponse3
195+ } ,
173196 } ;
174197
175- var fakeHttpMessageHandler = new FakeHttpMessageHandler ( messageResponses ) ;
198+ var fakeHttpMessageHandler = new FakeHttpMessageHandler ( options ) ;
176199
200+ // Act & Assert.
177201 await DoGetAsync ( RequestUri ,
178202 ExpectedContent ,
179203 fakeHttpMessageHandler ) ;
204+ options [ 0 ] . NumberOfTimesCalled . ShouldBe ( 1 ) ;
205+ options [ 1 ] . NumberOfTimesCalled . ShouldBe ( 0 ) ;
206+ options [ 2 ] . NumberOfTimesCalled . ShouldBe ( 0 ) ;
180207 }
181208
182209 [ Fact ]
183210 public async Task GivenAnUnauthorisedStatusCodeResponse_GetAsync_ReturnsAFakeResponseWithAnUnauthorisedStatusCode ( )
184211 {
185212 // Arrange.
186213 var messageResponse = FakeHttpMessageHandler . GetStringHttpResponseMessage ( "pew pew" , HttpStatusCode . Unauthorized ) ;
187- var messageHandler = new FakeHttpMessageHandler ( RequestUri , messageResponse ) ;
214+ var options = new HttpMessageOptions
215+ {
216+ RequestUri = RequestUri ,
217+ HttpResponseMessage = messageResponse
218+ } ;
219+ var messageHandler = new FakeHttpMessageHandler ( options ) ;
188220
189221 HttpResponseMessage message ;
190222 using ( var httpClient = new System . Net . Http . HttpClient ( messageHandler ) )
@@ -195,6 +227,7 @@ public async Task GivenAnUnauthorisedStatusCodeResponse_GetAsync_ReturnsAFakeRes
195227
196228 // Assert.
197229 message . StatusCode . ShouldBe ( HttpStatusCode . Unauthorized ) ;
230+ options . NumberOfTimesCalled . ShouldBe ( 1 ) ;
198231 }
199232
200233 [ Fact ]
@@ -217,6 +250,30 @@ public async Task GivenAValidHttpRequest_GetSomeDataAsync_ReturnsAFoo()
217250 exception . Message . ShouldBe ( errorMessage ) ;
218251 }
219252
253+ [ Fact ]
254+ public async Task GivenAFewCallsToAnHttpRequest_GetSomeDataAsync_ReturnsAFakeResponse ( )
255+ {
256+ // Arrange.
257+ var httpResponseMessage = FakeHttpMessageHandler . GetStringHttpResponseMessage ( ExpectedContent ) ;
258+ var options = new HttpMessageOptions
259+ {
260+ HttpResponseMessage = httpResponseMessage
261+ } ;
262+ var fakeHttpMessageHandler = new FakeHttpMessageHandler ( options ) ;
263+
264+ // Act & Assert
265+ await DoGetAsync ( RequestUri ,
266+ ExpectedContent ,
267+ fakeHttpMessageHandler ) ;
268+ await DoGetAsync ( RequestUri ,
269+ ExpectedContent ,
270+ fakeHttpMessageHandler ) ;
271+ await DoGetAsync ( RequestUri ,
272+ ExpectedContent ,
273+ fakeHttpMessageHandler ) ;
274+ options . NumberOfTimesCalled . ShouldBe ( 3 ) ;
275+ }
276+
220277 private static async Task DoGetAsync ( string requestUri ,
221278 string expectedResponseContent ,
222279 FakeHttpMessageHandler fakeHttpMessageHandler )
0 commit comments