Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ public async Task ExplicitResponse()
await Verify(result);
}
```
<sup><a href='/src/Tests/MockHttpClientTests.cs#L80-L96' title='Snippet source file'>snippet source</a> | <a href='#snippet-ExplicitResponse' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/MockHttpClientTests.cs#L105-L121' title='Snippet source file'>snippet source</a> | <a href='#snippet-ExplicitResponse' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -852,7 +852,7 @@ public async Task ResponseBuilder()
});
}
```
<sup><a href='/src/Tests/MockHttpClientTests.cs#L125-L150' title='Snippet source file'>snippet source</a> | <a href='#snippet-ResponseBuilder' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/MockHttpClientTests.cs#L150-L175' title='Snippet source file'>snippet source</a> | <a href='#snippet-ResponseBuilder' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -916,7 +916,7 @@ public async Task EnumerableResponses()
});
}
```
<sup><a href='/src/Tests/MockHttpClientTests.cs#L98-L123' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnumerableResponses' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/MockHttpClientTests.cs#L123-L148' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnumerableResponses' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -970,7 +970,7 @@ public async Task RecordingMockInteractions()
await Verify();
}
```
<sup><a href='/src/Tests/MockHttpClientTests.cs#L291-L305' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingMockInteractions' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/MockHttpClientTests.cs#L316-L330' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingMockInteractions' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;CS8632;NU1608;NU1109</NoWarn>
<Version>7.1.0</Version>
<Version>7.2.0</Version>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Http, Verify</PackageTags>
Expand Down
28 changes: 28 additions & 0 deletions src/Tests/MockHttpClientTests.ExplicitStatusCodes.verified.txt
Original file line number Diff line number Diff line change
@@ -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
}
}
]
}
25 changes: 25 additions & 0 deletions src/Tests/MockHttpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Verify.Http/MockHttpClient/MockHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public MockHttpClient(IEnumerable<HttpResponseMessage> responses, bool recording
{
}

public MockHttpClient(IEnumerable<HttpStatusCode> statuses, bool recording = false) :
this(new MockHttpHandler(statuses, recording))
{
}

public MockHttpClient(params HttpResponseMessage[] responses) :
this(new MockHttpHandler(responses))
{
Expand Down
17 changes: 17 additions & 0 deletions src/Verify.Http/MockHttpClient/MockHttpHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public MockHttpHandler(IEnumerable<HttpResponseMessage> responses, bool recordin
};
}

public MockHttpHandler(IEnumerable<HttpStatusCode> 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;
Expand Down