mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-28 03:37:45 +02:00
@@ -1,7 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
@@ -11,172 +10,142 @@ using Xunit;
|
|||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
public class StatefulBehaviorTests : IDisposable
|
public class StatefulBehaviorTests
|
||||||
{
|
{
|
||||||
private FluentMockServer _server;
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Should_skip_non_relevant_states()
|
public async Task Should_skip_non_relevant_states()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
_server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/foo").UsingGet())
|
||||||
.WithPath("/foo")
|
|
||||||
.UsingGet())
|
|
||||||
.InScenario("s")
|
.InScenario("s")
|
||||||
.WhenStateIs("Test state")
|
.WhenStateIs("Test state")
|
||||||
.RespondWith(Response.Create());
|
.RespondWith(Response.Create());
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
var response = await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + "/foo");
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
||||||
|
|
||||||
|
server.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Should_process_request_if_equals_state_and_single_state_defined()
|
public async Task Should_process_request_if_equals_state_and_single_state_defined()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
_server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/foo").UsingGet())
|
||||||
.WithPath("/foo")
|
|
||||||
.UsingGet())
|
|
||||||
.InScenario("s")
|
.InScenario("s")
|
||||||
.WillSetStateTo("Test state")
|
.WillSetStateTo("Test state")
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create().WithBody("No state msg"));
|
||||||
.WithBody("No state msg"));
|
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/foo").UsingGet())
|
||||||
.WithPath("/foo")
|
|
||||||
.UsingGet())
|
|
||||||
.InScenario("s")
|
.InScenario("s")
|
||||||
.WhenStateIs("Test state")
|
.WhenStateIs("Test state")
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create().WithBody("Test state msg"));
|
||||||
.WithBody("Test state msg"));
|
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var responseNoState = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
var responseNoState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo");
|
||||||
var responseWithState = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
var responseWithState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo");
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(responseNoState).Equals("No state msg");
|
Check.That(responseNoState).Equals("No state msg");
|
||||||
Check.That(responseWithState).Equals("Test state msg");
|
Check.That(responseWithState).Equals("Test state msg");
|
||||||
|
|
||||||
|
server.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Scenario_and_State_TodoList_Example()
|
public async Task Scenario_and_State_TodoList_Example()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
_server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||||
.WithPath("/todo/items")
|
|
||||||
.UsingGet())
|
|
||||||
.InScenario("To do list")
|
.InScenario("To do list")
|
||||||
.WillSetStateTo("TodoList State Started")
|
.WillSetStateTo("TodoList State Started")
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create().WithBody("Buy milk"));
|
||||||
.WithBody("Buy milk"));
|
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/todo/items").UsingPost())
|
||||||
.WithPath("/todo/items")
|
|
||||||
.UsingPost())
|
|
||||||
.InScenario("To do list")
|
.InScenario("To do list")
|
||||||
.WhenStateIs("TodoList State Started")
|
.WhenStateIs("TodoList State Started")
|
||||||
.WillSetStateTo("Cancel newspaper item added")
|
.WillSetStateTo("Cancel newspaper item added")
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create().WithStatusCode(201));
|
||||||
.WithStatusCode(201));
|
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/todo/items").UsingGet())
|
||||||
.WithPath("/todo/items")
|
|
||||||
.UsingGet())
|
|
||||||
.InScenario("To do list")
|
.InScenario("To do list")
|
||||||
.WhenStateIs("Cancel newspaper item added")
|
.WhenStateIs("Cancel newspaper item added")
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create().WithBody("Buy milk;Cancel newspaper subscription"));
|
||||||
.WithBody("Buy milk;Cancel newspaper subscription"));
|
|
||||||
|
|
||||||
// Act and Assert
|
// Act and Assert
|
||||||
string url = "http://localhost:" + _server.Ports[0];
|
string url = "http://localhost:" + server.Ports[0];
|
||||||
string getResponse1 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
|
string getResponse1 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
|
||||||
Check.That(getResponse1).Equals("Buy milk");
|
Check.That(getResponse1).Equals("Buy milk");
|
||||||
|
|
||||||
var postResponse = new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription")).Result;
|
var postResponse = await new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription"));
|
||||||
Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created);
|
Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created);
|
||||||
|
|
||||||
string getResponse2 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
|
string getResponse2 = await new HttpClient().GetStringAsync(url + "/todo/items");
|
||||||
Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription");
|
Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription");
|
||||||
|
|
||||||
|
server.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_process_request_if_equals_state_and_multiple_state_defined()
|
public async Task Should_process_request_if_equals_state_and_multiple_state_defined()
|
||||||
{
|
{
|
||||||
// given
|
// Assign
|
||||||
_server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/state1").UsingGet())
|
||||||
.WithPath("/state1")
|
|
||||||
.UsingGet())
|
|
||||||
.InScenario("s1")
|
.InScenario("s1")
|
||||||
.WillSetStateTo("Test state 1")
|
.WillSetStateTo("Test state 1")
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create().WithBody("No state msg 1"));
|
||||||
.WithBody("No state msg 1"));
|
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/fooX").UsingGet())
|
||||||
.WithPath("/foo")
|
|
||||||
.UsingGet())
|
|
||||||
.InScenario("s1")
|
.InScenario("s1")
|
||||||
.WhenStateIs("Test state 1")
|
.WhenStateIs("Test state 1")
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create().WithBody("Test state msg 1"));
|
||||||
.WithBody("Test state msg 1"));
|
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/state2").UsingGet())
|
||||||
.WithPath("/state2")
|
|
||||||
.UsingGet())
|
|
||||||
.InScenario("s2")
|
.InScenario("s2")
|
||||||
.WillSetStateTo("Test state 2")
|
.WillSetStateTo("Test state 2")
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create().WithBody("No state msg 2"));
|
||||||
.WithBody("No state msg 2"));
|
|
||||||
|
|
||||||
_server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create().WithPath("/fooX").UsingGet())
|
||||||
.WithPath("/foo")
|
|
||||||
.UsingGet())
|
|
||||||
.InScenario("s2")
|
.InScenario("s2")
|
||||||
.WhenStateIs("Test state 2")
|
.WhenStateIs("Test state 2")
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create().WithBody("Test state msg 2"));
|
||||||
.WithBody("Test state msg 2"));
|
|
||||||
|
|
||||||
Thread.Sleep(500);
|
// Act and Assert
|
||||||
|
string url = "http://localhost:" + server.Ports[0];
|
||||||
// when / then
|
var responseNoState1 = await new HttpClient().GetStringAsync(url + "/state1");
|
||||||
string url = "http://localhost:" + _server.Ports[0];
|
|
||||||
var responseNoState1 = new HttpClient().GetStringAsync(url + "/state1").Result;
|
|
||||||
Check.That(responseNoState1).Equals("No state msg 1");
|
Check.That(responseNoState1).Equals("No state msg 1");
|
||||||
|
|
||||||
var responseNoState2 = new HttpClient().GetStringAsync(url + "/state2").Result;
|
var responseNoState2 = await new HttpClient().GetStringAsync(url + "/state2");
|
||||||
Check.That(responseNoState2).Equals("No state msg 2");
|
Check.That(responseNoState2).Equals("No state msg 2");
|
||||||
|
|
||||||
var responseWithState1 = new HttpClient().GetStringAsync(url + "/foo").Result;
|
var responseWithState1 = await new HttpClient().GetStringAsync(url + "/fooX");
|
||||||
Check.That(responseWithState1).Equals("Test state msg 1");
|
Check.That(responseWithState1).Equals("Test state msg 1");
|
||||||
|
|
||||||
var responseWithState2 = new HttpClient().GetStringAsync(url + "/foo").Result;
|
var responseWithState2 = await new HttpClient().GetStringAsync(url + "/fooX");
|
||||||
Check.That(responseWithState2).Equals("Test state msg 2");
|
Check.That(responseWithState2).Equals("Test state msg 2");
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
server.Dispose();
|
||||||
{
|
|
||||||
_server?.Dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user