-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomResponse.cs
More file actions
45 lines (41 loc) · 1.45 KB
/
CustomResponse.cs
File metadata and controls
45 lines (41 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Text;
using WireMock;
using WireMock.ResponseBuilders;
using WireMock.ResponseProviders;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
namespace APITestingRunner.Unit.Tests.TestRunnerTests
{
public class CustomResponse : IResponseProvider
{
private static int _count = 0;
public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings)
{
ResponseMessage response;
if (_count % 2 == 0)
{
response = new ResponseMessage() { StatusCode = 200 };
SetBody(response, @"{ ""msg"": ""Hello from wiremock!"" }");
}
else
{
response = new ResponseMessage() { StatusCode = 500 };
SetBody(response, @"{ ""msg"": ""Hello some error from wiremock!"" }");
}
_count++;
(ResponseMessage, IMapping) tuple = (response, null);
return await Task.FromResult(tuple);
}
private void SetBody(ResponseMessage response, string body)
{
response.BodyDestination = BodyDestinationFormat.SameAsSource;
response.BodyData = new BodyData
{
Encoding = Encoding.UTF8,
DetectedBodyType = BodyType.String,
BodyAsString = body
};
}
}
}