mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Add Scenario set State method (#1322)
* Add SetScenarioState * add tests * summary * . * 1.8.13-preview-01 * fix * fix name
This commit is contained in:
@@ -17,6 +17,7 @@ using RestEase;
|
||||
using VerifyTests;
|
||||
using VerifyXunit;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Admin.Scenarios;
|
||||
using WireMock.Admin.Settings;
|
||||
using WireMock.Client;
|
||||
using WireMock.Client.Extensions;
|
||||
@@ -743,6 +744,57 @@ public partial class WireMockAdminApiTests
|
||||
status.Status.Should().Be("No scenario found by name 'x'.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_UpdateNonExistingScenarioState()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.StartWithAdminInterface();
|
||||
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var update = new ScenarioStateUpdateModel
|
||||
{
|
||||
State = null
|
||||
};
|
||||
var status = await api.PutScenarioStateAsync("x", update).ConfigureAwait(false);
|
||||
status.Status.Should().Be("No scenario found by name 'x'.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_UpdateScenarioState()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.StartWithAdminInterface();
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/state1")
|
||||
.UsingGet())
|
||||
.InScenario("s1")
|
||||
.WillSetStateTo("Test state 1")
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody("No state msg 1"));
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/foostate1")
|
||||
.UsingGet())
|
||||
.InScenario("s1")
|
||||
.WhenStateIs("Test state 1")
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody("Test state msg 1"));
|
||||
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var update = new ScenarioStateUpdateModel
|
||||
{
|
||||
State = null
|
||||
};
|
||||
var status = await api.PutScenarioStateAsync("s1", update).ConfigureAwait(false);
|
||||
status.Status.Should().Be("Scenario state set to ''");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_GetMappingByGuidAsync()
|
||||
{
|
||||
|
||||
@@ -6,5 +6,5 @@ internal static class Constants
|
||||
{
|
||||
internal const int NumStaticMappings = 10;
|
||||
|
||||
internal const int NumAdminMappings = 36;
|
||||
internal const int NumAdminMappings = 37;
|
||||
}
|
||||
@@ -12,340 +12,447 @@ using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
public class StatefulBehaviorTests
|
||||
{
|
||||
public class StatefulBehaviorTests
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_skip_non_relevant_states()
|
||||
{
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_skip_non_relevant_states()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("s")
|
||||
.WhenStateIs("Test state")
|
||||
.RespondWith(Response.Create());
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("s")
|
||||
.WhenStateIs("Test state")
|
||||
.RespondWith(Response.Create());
|
||||
|
||||
// when
|
||||
var response = await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
// when
|
||||
var response = await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
|
||||
// then
|
||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
||||
// then
|
||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_process_request_if_equals_state_and_single_state_defined()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_process_request_if_equals_state_and_single_state_defined()
|
||||
{
|
||||
// Arrange
|
||||
var path = $"/foo_{Guid.NewGuid()}";
|
||||
using var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("s")
|
||||
.WillSetStateTo("Test state")
|
||||
.RespondWith(Response.Create().WithBody("No state msg"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("s")
|
||||
.WillSetStateTo("Test state")
|
||||
.RespondWith(Response.Create().WithBody("No state msg"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("s")
|
||||
.WhenStateIs("Test state")
|
||||
.RespondWith(Response.Create().WithBody("Test state msg"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("s")
|
||||
.WhenStateIs("Test state")
|
||||
.RespondWith(Response.Create().WithBody("Test state msg"));
|
||||
|
||||
// when
|
||||
var responseNoState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseWithState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
// Act
|
||||
var responseNoState = await new HttpClient().GetStringAsync(server.Url + path).ConfigureAwait(false);
|
||||
var responseWithState = await new HttpClient().GetStringAsync(server.Url + path).ConfigureAwait(false);
|
||||
|
||||
// then
|
||||
Check.That(responseNoState).Equals("No state msg");
|
||||
Check.That(responseWithState).Equals("Test state msg");
|
||||
// Assert
|
||||
Check.That(responseNoState).Equals("No state msg");
|
||||
Check.That(responseWithState).Equals("Test state msg");
|
||||
}
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
[Theory]
|
||||
[InlineData(null, "step 1", "step 2")]
|
||||
[InlineData("step 2", "step 2", "step 3")]
|
||||
public async Task Scenarios_Should_ContinueOnCorrectState_WhenStateIsUpdated(string? state, string expected1, string expected2)
|
||||
{
|
||||
// Arrange
|
||||
var path = $"/foo_{Guid.NewGuid()}";
|
||||
var scenario = "s";
|
||||
using var server = WireMockServer.Start();
|
||||
|
||||
[Fact]
|
||||
public async Task Scenarios_With_Same_Path_Should_Use_Times_When_Moving_To_Next_State()
|
||||
{
|
||||
// given
|
||||
const int times = 2;
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
string body1 = "Scenario S1, No State, Setting State T2";
|
||||
string body2 = "Scenario S1, State T2, End";
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(scenario)
|
||||
.WillSetStateTo("step 2")
|
||||
.RespondWith(Response.Create().WithBody("step 1"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WillSetStateTo(2, times)
|
||||
.RespondWith(Response.Create().WithBody(body1));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(scenario)
|
||||
.WhenStateIs("step 2")
|
||||
.WillSetStateTo("step 3")
|
||||
.RespondWith(Response.Create().WithBody("step 2"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WhenStateIs(2)
|
||||
.RespondWith(Response.Create().WithBody(body2));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(scenario)
|
||||
.WhenStateIs("step 3")
|
||||
.RespondWith(Response.Create().WithBody("step 3"));
|
||||
|
||||
// when
|
||||
var client = new HttpClient();
|
||||
var responseScenario1 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseScenario2 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseWithState = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
// Act
|
||||
var client = server.CreateClient();
|
||||
var response1 = await client.GetStringAsync(server.Url + path);
|
||||
var response2 = await client.GetStringAsync(server.Url + path);
|
||||
var response3 = await client.GetStringAsync(server.Url + path);
|
||||
|
||||
// then
|
||||
responseScenario1.Should().Be(body1);
|
||||
responseScenario2.Should().Be(body1);
|
||||
responseWithState.Should().Be(body2);
|
||||
server.SetScenarioState(scenario, state);
|
||||
var responseA = await client.GetStringAsync(server.Url + path);
|
||||
var responseB = await client.GetStringAsync(server.Url + path);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
// Assert
|
||||
Check.That(response1).Equals("step 1");
|
||||
Check.That(response2).Equals("step 2");
|
||||
Check.That(response3).Equals("step 3");
|
||||
Check.That(responseA).Equals(expected1);
|
||||
Check.That(responseB).Equals(expected2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Scenarios_With_Different_Paths_Should_Use_Times_When_Moving_To_Next_State()
|
||||
{
|
||||
// given
|
||||
const int times = 2;
|
||||
string path1 = $"/a_{Guid.NewGuid()}";
|
||||
string path2 = $"/b_{Guid.NewGuid()}";
|
||||
string path3 = $"/c_{Guid.NewGuid()}";
|
||||
string body1 = "Scenario S1, No State, Setting State T2";
|
||||
string body2 = "Scenario S1, State T2, Setting State T3";
|
||||
string body3 = "Scenario S1, State T3, End";
|
||||
[Fact]
|
||||
public async Task Scenarios_With_Same_Path_Should_Use_Times_When_Moving_To_Next_State()
|
||||
{
|
||||
// given
|
||||
const int times = 2;
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
string body1 = "Scenario S1, No State, Setting State T2";
|
||||
string body2 = "Scenario S1, State T2, End";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WillSetStateTo(2, times)
|
||||
.RespondWith(Response.Create().WithBody(body1));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path1).UsingGet())
|
||||
.InScenario("S1")
|
||||
.WillSetStateTo("T2", times)
|
||||
.RespondWith(Response.Create().WithBody(body1));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WhenStateIs(2)
|
||||
.RespondWith(Response.Create().WithBody(body2));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path2).UsingGet())
|
||||
.InScenario("S1")
|
||||
.WhenStateIs("T2")
|
||||
.WillSetStateTo("T3", times)
|
||||
.RespondWith(Response.Create().WithBody(body2));
|
||||
// when
|
||||
var client = new HttpClient();
|
||||
var responseScenario1 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseScenario2 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseWithState = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path3).UsingGet())
|
||||
.InScenario("S1")
|
||||
.WhenStateIs("T3")
|
||||
.RespondWith(Response.Create().WithBody(body3));
|
||||
// then
|
||||
responseScenario1.Should().Be(body1);
|
||||
responseScenario2.Should().Be(body1);
|
||||
responseWithState.Should().Be(body2);
|
||||
|
||||
// when
|
||||
var client = new HttpClient();
|
||||
var t1a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1).ConfigureAwait(false);
|
||||
var t1b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1).ConfigureAwait(false);
|
||||
var t2a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2).ConfigureAwait(false);
|
||||
var t2b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2).ConfigureAwait(false);
|
||||
var t3 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path3).ConfigureAwait(false);
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
// then
|
||||
t1a.Should().Be(body1);
|
||||
t1b.Should().Be(body1);
|
||||
t2a.Should().Be(body2);
|
||||
t2b.Should().Be(body2);
|
||||
t3.Should().Be(body3);
|
||||
[Fact]
|
||||
public async Task Scenarios_With_Different_Paths_Should_Use_Times_When_Moving_To_Next_State()
|
||||
{
|
||||
// given
|
||||
const int times = 2;
|
||||
string path1 = $"/a_{Guid.NewGuid()}";
|
||||
string path2 = $"/b_{Guid.NewGuid()}";
|
||||
string path3 = $"/c_{Guid.NewGuid()}";
|
||||
string body1 = "Scenario S1, No State, Setting State T2";
|
||||
string body2 = "Scenario S1, State T2, Setting State T3";
|
||||
string body3 = "Scenario S1, State T3, End";
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_Respect_Int_Valued_Scenarios_and_States()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create().WithPath(path1).UsingGet())
|
||||
.InScenario("S1")
|
||||
.WillSetStateTo("T2", times)
|
||||
.RespondWith(Response.Create().WithBody(body1));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WillSetStateTo(2)
|
||||
.RespondWith(Response.Create().WithBody("Scenario 1, Setting State 2"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path2).UsingGet())
|
||||
.InScenario("S1")
|
||||
.WhenStateIs("T2")
|
||||
.WillSetStateTo("T3", times)
|
||||
.RespondWith(Response.Create().WithBody(body2));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WhenStateIs(2)
|
||||
.RespondWith(Response.Create().WithBody("Scenario 1, State 2"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path3).UsingGet())
|
||||
.InScenario("S1")
|
||||
.WhenStateIs("T3")
|
||||
.RespondWith(Response.Create().WithBody(body3));
|
||||
|
||||
// when
|
||||
var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
// when
|
||||
var client = new HttpClient();
|
||||
var t1a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1).ConfigureAwait(false);
|
||||
var t1b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1).ConfigureAwait(false);
|
||||
var t2a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2).ConfigureAwait(false);
|
||||
var t2b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2).ConfigureAwait(false);
|
||||
var t3 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path3).ConfigureAwait(false);
|
||||
|
||||
// then
|
||||
Check.That(responseIntScenario).Equals("Scenario 1, Setting State 2");
|
||||
Check.That(responseWithIntState).Equals("Scenario 1, State 2");
|
||||
// then
|
||||
t1a.Should().Be(body1);
|
||||
t1b.Should().Be(body1);
|
||||
t2a.Should().Be(body2);
|
||||
t2b.Should().Be(body2);
|
||||
t3.Should().Be(body3);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_Respect_Mixed_String_Scenario_and_Int_State()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_Respect_Int_Valued_Scenarios_and_States()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("state string")
|
||||
.WillSetStateTo(1)
|
||||
.RespondWith(Response.Create().WithBody("string state, Setting State 2"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WillSetStateTo(2)
|
||||
.RespondWith(Response.Create().WithBody("Scenario 1, Setting State 2"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("state string")
|
||||
.WhenStateIs(1)
|
||||
.RespondWith(Response.Create().WithBody("string state, State 2"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WhenStateIs(2)
|
||||
.RespondWith(Response.Create().WithBody("Scenario 1, State 2"));
|
||||
|
||||
// when
|
||||
var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
// when
|
||||
var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
|
||||
// then
|
||||
Check.That(responseIntScenario).Equals("string state, Setting State 2");
|
||||
Check.That(responseWithIntState).Equals("string state, State 2");
|
||||
// then
|
||||
Check.That(responseIntScenario).Equals("Scenario 1, Setting State 2");
|
||||
Check.That(responseWithIntState).Equals("Scenario 1, State 2");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_Respect_Mixed_Int_Scenario_and_String_Scenario_and_String_State()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_Respect_Mixed_String_Scenario_and_Int_State()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WillSetStateTo("Next State")
|
||||
.RespondWith(Response.Create().WithBody("int state, Setting State 2"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("state string")
|
||||
.WillSetStateTo(1)
|
||||
.RespondWith(Response.Create().WithBody("string state, Setting State 2"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("1")
|
||||
.WhenStateIs("Next State")
|
||||
.RespondWith(Response.Create().WithBody("string state, State 2"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("state string")
|
||||
.WhenStateIs(1)
|
||||
.RespondWith(Response.Create().WithBody("string state, State 2"));
|
||||
|
||||
// when
|
||||
var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
// when
|
||||
var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
|
||||
// then
|
||||
Check.That(responseIntScenario).Equals("int state, Setting State 2");
|
||||
Check.That(responseWithIntState).Equals("string state, State 2");
|
||||
// then
|
||||
Check.That(responseIntScenario).Equals("string state, Setting State 2");
|
||||
Check.That(responseWithIntState).Equals("string state, State 2");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Scenarios_TodoList_Example()
|
||||
{
|
||||
// Assign
|
||||
var server = WireMockServer.Start();
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_Respect_Mixed_Int_Scenario_and_String_Scenario_and_String_State()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||
.InScenario("To do list")
|
||||
.WillSetStateTo("TodoList State Started")
|
||||
.RespondWith(Response.Create().WithBody("Buy milk"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario(1)
|
||||
.WillSetStateTo("Next State")
|
||||
.RespondWith(Response.Create().WithBody("int state, Setting State 2"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingPost())
|
||||
.InScenario("To do list")
|
||||
.WhenStateIs("TodoList State Started")
|
||||
.WillSetStateTo("Cancel newspaper item added")
|
||||
.RespondWith(Response.Create().WithStatusCode(201));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.InScenario("1")
|
||||
.WhenStateIs("Next State")
|
||||
.RespondWith(Response.Create().WithBody("string state, State 2"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||
.InScenario("To do list")
|
||||
.WhenStateIs("Cancel newspaper item added")
|
||||
.RespondWith(Response.Create().WithBody("Buy milk;Cancel newspaper subscription"));
|
||||
// when
|
||||
var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
|
||||
Check.That(server.Scenarios.Any()).IsFalse();
|
||||
// then
|
||||
Check.That(responseIntScenario).Equals("int state, Setting State 2");
|
||||
Check.That(responseWithIntState).Equals("string state, State 2");
|
||||
|
||||
// Act and Assert
|
||||
string url = "http://localhost:" + server.Ports[0];
|
||||
string getResponse1 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
|
||||
Check.That(getResponse1).Equals("Buy milk");
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
Check.That(server.Scenarios["To do list"].Name).IsEqualTo("To do list");
|
||||
Check.That(server.Scenarios["To do list"].NextState).IsEqualTo("TodoList State Started");
|
||||
Check.That(server.Scenarios["To do list"].Started).IsTrue();
|
||||
Check.That(server.Scenarios["To do list"].Finished).IsFalse();
|
||||
[Fact]
|
||||
public async Task Scenarios_TodoList_Example()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
var client = server.CreateClient();
|
||||
|
||||
var postResponse = await new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription")).ConfigureAwait(false);
|
||||
Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created);
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||
.InScenario("To do list")
|
||||
.WillSetStateTo("TodoList State Started")
|
||||
.RespondWith(Response.Create().WithBody("Buy milk"));
|
||||
|
||||
Check.That(server.Scenarios["To do list"].Name).IsEqualTo("To do list");
|
||||
Check.That(server.Scenarios["To do list"].NextState).IsEqualTo("Cancel newspaper item added");
|
||||
Check.That(server.Scenarios["To do list"].Started).IsTrue();
|
||||
Check.That(server.Scenarios["To do list"].Finished).IsFalse();
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingPost())
|
||||
.InScenario("To do list")
|
||||
.WhenStateIs("TodoList State Started")
|
||||
.WillSetStateTo("Cancel newspaper item added")
|
||||
.RespondWith(Response.Create().WithStatusCode(201));
|
||||
|
||||
string getResponse2 = await new HttpClient().GetStringAsync(url + "/todo/items").ConfigureAwait(false);
|
||||
Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription");
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||
.InScenario("To do list")
|
||||
.WhenStateIs("Cancel newspaper item added")
|
||||
.RespondWith(Response.Create().WithBody("Buy milk;Cancel newspaper subscription"));
|
||||
|
||||
Check.That(server.Scenarios["To do list"].Name).IsEqualTo("To do list");
|
||||
Check.That(server.Scenarios["To do list"].NextState).IsNull();
|
||||
Check.That(server.Scenarios["To do list"].Started).IsTrue();
|
||||
Check.That(server.Scenarios["To do list"].Finished).IsTrue();
|
||||
Check.That(server.Scenarios.Any()).IsFalse();
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
// Act and Assert
|
||||
var getResponse1 = await client.GetStringAsync("/todo/items").ConfigureAwait(false);
|
||||
Check.That(getResponse1).Equals("Buy milk");
|
||||
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_process_request_if_equals_state_and_multiple_state_defined()
|
||||
{
|
||||
// Assign
|
||||
var server = WireMockServer.Start();
|
||||
Check.That(server.Scenarios["To do list"].Name).IsEqualTo("To do list");
|
||||
Check.That(server.Scenarios["To do list"].NextState).IsEqualTo("TodoList State Started");
|
||||
Check.That(server.Scenarios["To do list"].Started).IsTrue();
|
||||
Check.That(server.Scenarios["To do list"].Finished).IsFalse();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/state1").UsingGet())
|
||||
.InScenario("s1")
|
||||
.WillSetStateTo("Test state 1")
|
||||
.RespondWith(Response.Create().WithBody("No state msg 1"));
|
||||
var postResponse = await client.PostAsync("/todo/items", new StringContent("Cancel newspaper subscription")).ConfigureAwait(false);
|
||||
Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created);
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/foo1X").UsingGet())
|
||||
.InScenario("s1")
|
||||
.WhenStateIs("Test state 1")
|
||||
.RespondWith(Response.Create().WithBody("Test state msg 1"));
|
||||
Check.That(server.Scenarios["To do list"].Name).IsEqualTo("To do list");
|
||||
Check.That(server.Scenarios["To do list"].NextState).IsEqualTo("Cancel newspaper item added");
|
||||
Check.That(server.Scenarios["To do list"].Started).IsTrue();
|
||||
Check.That(server.Scenarios["To do list"].Finished).IsFalse();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/state2").UsingGet())
|
||||
.InScenario("s2")
|
||||
.WillSetStateTo("Test state 2")
|
||||
.RespondWith(Response.Create().WithBody("No state msg 2"));
|
||||
string getResponse2 = await client.GetStringAsync("/todo/items").ConfigureAwait(false);
|
||||
Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription");
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/foo2X").UsingGet())
|
||||
.InScenario("s2")
|
||||
.WhenStateIs("Test state 2")
|
||||
.RespondWith(Response.Create().WithBody("Test state msg 2"));
|
||||
Check.That(server.Scenarios["To do list"].Name).IsEqualTo("To do list");
|
||||
Check.That(server.Scenarios["To do list"].NextState).IsNull();
|
||||
Check.That(server.Scenarios["To do list"].Started).IsTrue();
|
||||
Check.That(server.Scenarios["To do list"].Finished).IsTrue();
|
||||
|
||||
// Act and Assert
|
||||
string url = "http://localhost:" + server.Ports[0];
|
||||
var responseNoState1 = await new HttpClient().GetStringAsync(url + "/state1").ConfigureAwait(false);
|
||||
Check.That(responseNoState1).Equals("No state msg 1");
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
var responseNoState2 = await new HttpClient().GetStringAsync(url + "/state2").ConfigureAwait(false);
|
||||
Check.That(responseNoState2).Equals("No state msg 2");
|
||||
[Fact]
|
||||
public async Task Scenarios_TodoList_WithSetState()
|
||||
{
|
||||
// Arrange
|
||||
var scenario = "To do list";
|
||||
using var server = WireMockServer.Start();
|
||||
var client = server.CreateClient();
|
||||
|
||||
var responseWithState1 = await new HttpClient().GetStringAsync(url + "/foo1X").ConfigureAwait(false);
|
||||
Check.That(responseWithState1).Equals("Test state msg 1");
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||
.InScenario(scenario)
|
||||
.WhenStateIs("Buy milk")
|
||||
.RespondWith(Response.Create().WithBody("Buy milk"));
|
||||
|
||||
var responseWithState2 = await new HttpClient().GetStringAsync(url + "/foo2X").ConfigureAwait(false);
|
||||
Check.That(responseWithState2).Equals("Test state msg 2");
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||
.InScenario(scenario)
|
||||
.WhenStateIs("Cancel newspaper")
|
||||
.RespondWith(Response.Create().WithBody("Buy milk;Cancel newspaper subscription"));
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
// Act and Assert
|
||||
server.SetScenarioState(scenario, "Buy milk");
|
||||
server.Scenarios[scenario].Should().BeEquivalentTo(new { Name = scenario, NextState = "Buy milk" });
|
||||
|
||||
var getResponse1 = await client.GetStringAsync("/todo/items").ConfigureAwait(false);
|
||||
getResponse1.Should().Be("Buy milk");
|
||||
|
||||
server.SetScenarioState(scenario, "Cancel newspaper");
|
||||
server.Scenarios[scenario].Name.Should().Be(scenario);
|
||||
server.Scenarios[scenario].Should().BeEquivalentTo(new { Name = scenario, NextState = "Cancel newspaper" });
|
||||
|
||||
var getResponse2 = await client.GetStringAsync("/todo/items").ConfigureAwait(false);
|
||||
getResponse2.Should().Be("Buy milk;Cancel newspaper subscription");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Scenarios_TodoList_WithSetStateToNull_ShouldThrowException()
|
||||
{
|
||||
// Arrange
|
||||
var scenario = "To do list";
|
||||
using var server = WireMockServer.Start();
|
||||
var client = server.CreateClient();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||
.InScenario(scenario)
|
||||
.WhenStateIs("Buy milk")
|
||||
.RespondWith(Response.Create().WithBody("Buy milk"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||
.InScenario(scenario)
|
||||
.WhenStateIs("Cancel newspaper")
|
||||
.RespondWith(Response.Create().WithBody("Buy milk;Cancel newspaper subscription"));
|
||||
|
||||
// Act
|
||||
server.SetScenarioState(scenario, null);
|
||||
var action = async () => await client.GetStringAsync("/todo/items");
|
||||
|
||||
// Assert
|
||||
action.Should().ThrowAsync<HttpRequestException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Scenarios_Should_process_request_if_equals_state_and_multiple_state_defined()
|
||||
{
|
||||
// Assign
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/state1").UsingGet())
|
||||
.InScenario("s1")
|
||||
.WillSetStateTo("Test state 1")
|
||||
.RespondWith(Response.Create().WithBody("No state msg 1"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/foo1X").UsingGet())
|
||||
.InScenario("s1")
|
||||
.WhenStateIs("Test state 1")
|
||||
.RespondWith(Response.Create().WithBody("Test state msg 1"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/state2").UsingGet())
|
||||
.InScenario("s2")
|
||||
.WillSetStateTo("Test state 2")
|
||||
.RespondWith(Response.Create().WithBody("No state msg 2"));
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath("/foo2X").UsingGet())
|
||||
.InScenario("s2")
|
||||
.WhenStateIs("Test state 2")
|
||||
.RespondWith(Response.Create().WithBody("Test state msg 2"));
|
||||
|
||||
// Act and Assert
|
||||
string url = "http://localhost:" + server.Ports[0];
|
||||
var responseNoState1 = await new HttpClient().GetStringAsync(url + "/state1").ConfigureAwait(false);
|
||||
Check.That(responseNoState1).Equals("No state msg 1");
|
||||
|
||||
var responseNoState2 = await new HttpClient().GetStringAsync(url + "/state2").ConfigureAwait(false);
|
||||
Check.That(responseNoState2).Equals("No state msg 2");
|
||||
|
||||
var responseWithState1 = await new HttpClient().GetStringAsync(url + "/foo1X").ConfigureAwait(false);
|
||||
Check.That(responseWithState1).Equals("Test state msg 1");
|
||||
|
||||
var responseWithState2 = await new HttpClient().GetStringAsync(url + "/foo2X").ConfigureAwait(false);
|
||||
Check.That(responseWithState2).Equals("Test state msg 2");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user