diff --git a/readme.md b/readme.md index fb586f30..74016dd1 100644 --- a/readme.md +++ b/readme.md @@ -799,7 +799,7 @@ public async Task ExplicitResponse() await Verify(result); } ``` -snippet source | anchor +snippet source | anchor @@ -852,7 +852,7 @@ public async Task ResponseBuilder() }); } ``` -snippet source | anchor +snippet source | anchor @@ -916,7 +916,7 @@ public async Task EnumerableResponses() }); } ``` -snippet source | anchor +snippet source | anchor @@ -970,7 +970,7 @@ public async Task RecordingMockInteractions() await Verify(); } ``` -snippet source | anchor +snippet source | anchor diff --git a/src/Directory.Build.props b/src/Directory.Build.props index e6ae0737..627c84ed 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;CS8632;NU1608;NU1109 - 7.1.0 + 7.2.0 preview 1.0.0 Http, Verify diff --git a/src/Tests/MockHttpClientTests.ExplicitStatusCodes.verified.txt b/src/Tests/MockHttpClientTests.ExplicitStatusCodes.verified.txt new file mode 100644 index 00000000..0cc0e748 --- /dev/null +++ b/src/Tests/MockHttpClientTests.ExplicitStatusCodes.verified.txt @@ -0,0 +1,28 @@ +{ + httpCall: [ + { + Request: https://fake/get1, + Response: { + Status: 300 MultipleChoices + } + }, + { + Request: https://fake/get2, + Response: { + Status: 502 BadGateway + } + }, + { + Request: https://fake/get3, + Response: { + Status: 504 GatewayTimeout + } + }, + { + Request: https://fake/get4, + Response: { + Status: 500 InternalServerError + } + } + ] +} \ No newline at end of file diff --git a/src/Tests/MockHttpClientTests.cs b/src/Tests/MockHttpClientTests.cs index 761e4c4d..83f1cc86 100644 --- a/src/Tests/MockHttpClientTests.cs +++ b/src/Tests/MockHttpClientTests.cs @@ -75,6 +75,31 @@ public async Task ExplicitStatusCode() await Verify(result); } + #endregion + #region ExplicitStatusCodes + + [Fact] + public async Task ExplicitStatusCodes() + { + using var client = new MockHttpClient( + [ + HttpStatusCode.Ambiguous, + HttpStatusCode.BadGateway, + HttpStatusCode.GatewayTimeout, + HttpStatusCode.InternalServerError + ], + recording: true); + + Recording.Start(); + + await client.GetAsync("https://fake/get1"); + await client.GetAsync("https://fake/get2"); + await client.GetAsync("https://fake/get3"); + await client.GetAsync("https://fake/get4"); + + await Verify(); + } + #endregion #region ExplicitResponse diff --git a/src/Verify.Http/MockHttpClient/MockHttpClient.cs b/src/Verify.Http/MockHttpClient/MockHttpClient.cs index 9fe820e7..58fefc5a 100644 --- a/src/Verify.Http/MockHttpClient/MockHttpClient.cs +++ b/src/Verify.Http/MockHttpClient/MockHttpClient.cs @@ -15,6 +15,11 @@ public MockHttpClient(IEnumerable responses, bool recording { } + public MockHttpClient(IEnumerable statuses, bool recording = false) : + this(new MockHttpHandler(statuses, recording)) + { + } + public MockHttpClient(params HttpResponseMessage[] responses) : this(new MockHttpHandler(responses)) { diff --git a/src/Verify.Http/MockHttpClient/MockHttpHandler.cs b/src/Verify.Http/MockHttpClient/MockHttpHandler.cs index 476e7297..7543daac 100644 --- a/src/Verify.Http/MockHttpClient/MockHttpHandler.cs +++ b/src/Verify.Http/MockHttpClient/MockHttpHandler.cs @@ -42,6 +42,23 @@ public MockHttpHandler(IEnumerable responses, bool recordin }; } + public MockHttpHandler(IEnumerable statuses, bool recording = false) + { + this.recording = recording; + var enumerator = statuses.GetEnumerator(); + disposables.Add(enumerator); + builder = _ => + { + var hasNext = enumerator.MoveNext(); + if (!hasNext) + { + throw new("Not enough responses provided"); + } + + return new(enumerator.Current); + }; + } + public MockHttpHandler(HttpStatusCode status = HttpStatusCode.OK, bool recording = false) { this.recording = recording;