scenario and state

This commit is contained in:
Stef Heyenrath
2017-10-07 13:19:48 +02:00
parent ab017beaa6
commit 5424b1e2e2
10 changed files with 416 additions and 257 deletions

View File

@@ -24,10 +24,9 @@ namespace WireMock.Net.Tests
.Given(Request.Create()
.WithPath("/foo")
.UsingGet())
.InScenario("s")
.WhenStateIs("Test state")
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}"));
.RespondWith(Response.Create());
// when
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
@@ -37,7 +36,7 @@ namespace WireMock.Net.Tests
}
[Fact]
public async Task Should_process_request_if_equals_state()
public async Task Should_process_request_if_equals_state_and_single_state_defined()
{
// given
_server = FluentMockServer.Start();
@@ -46,19 +45,19 @@ namespace WireMock.Net.Tests
.Given(Request.Create()
.WithPath("/foo")
.UsingGet())
.InScenario("s")
.WillSetStateTo("Test state")
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(@"No state msg"));
.WithBody("No state msg"));
_server
.Given(Request.Create()
.WithPath("/foo")
.UsingGet())
.InScenario("s")
.WhenStateIs("Test state")
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(@"Test state msg"));
.WithBody("Test state msg"));
// when
var responseNoState = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
@@ -69,9 +68,66 @@ namespace WireMock.Net.Tests
Check.That(responseWithState).Equals("Test state msg");
}
[Fact]
public async Task Should_process_request_if_equals_state_and_multiple_state_defined()
{
// given
_server = FluentMockServer.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("/foo")
.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("/foo")
.UsingGet())
.InScenario("s2")
.WhenStateIs("Test state 2")
.RespondWith(Response.Create()
.WithBody("Test state msg 2"));
// when
string url = "http://localhost:" + _server.Ports[0];
var responseNoState1 = await new HttpClient().GetStringAsync(url + "/state1");
var responseNoState2 = await new HttpClient().GetStringAsync(url + "/state2");
var responseWithState1 = await new HttpClient().GetStringAsync(url + "/foo");
var responseWithState2 = await new HttpClient().GetStringAsync(url + "/foo");
// then
Check.That(responseNoState1).Equals("No state msg 1");
Check.That(responseWithState1).Equals("Test state msg 1");
Check.That(responseNoState2).Equals("No state msg 2");
Check.That(responseWithState2).Equals("Test state msg 2");
}
public void Dispose()
{
_server?.Dispose();
}
}
}
}

View File

@@ -1,49 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin;
using Moq;
using NFluent;
using WireMock.Owin;
using Xunit;
//using Microsoft.Owin;
//using Moq;
//using NFluent;
//using WireMock.Owin;
//using Xunit;
namespace WireMock.Net.Tests
{
public class WireMockMiddlewareTests
{
private readonly ObjectMother _objectMother = new ObjectMother();
//namespace WireMock.Net.Tests
//{
// public class WireMockMiddlewareTests
// {
// private readonly ObjectMother _objectMother = new ObjectMother();
[Fact]
public void Should_have_default_state_as_null()
{
// given
// [Fact]
// public void Should_have_default_state_as_null()
// {
// // given
// when
var sut = _objectMother.Create();
// // when
// var sut = _objectMother.Create();
// then
Check.That(sut.State).IsNull();
}
// // then
// Check.That(sut.States).IsNull();
// }
internal class ObjectMother
{
public Mock<OwinMiddleware> OwinMiddleware { get; set; }
public Mock<IOwinContext> OwinContext { get; set; }
public WireMockMiddlewareOptions WireMockMiddlewareOptions { get; set; }
// private class ObjectMother
// {
// private Mock<OwinMiddleware> OwinMiddleware { get; }
// private Mock<IOwinContext> OwinContext { get; }
// private WireMockMiddlewareOptions WireMockMiddlewareOptions { get; }
public ObjectMother()
{
OwinContext = new Mock<IOwinContext>();
OwinMiddleware = new Mock<OwinMiddleware>(null);
WireMockMiddlewareOptions = new WireMockMiddlewareOptions();
}
// public ObjectMother()
// {
// OwinContext = new Mock<IOwinContext>();
// OwinMiddleware = new Mock<OwinMiddleware>(null);
// WireMockMiddlewareOptions = new WireMockMiddlewareOptions();
// }
public WireMockMiddleware Create()
{
return new WireMockMiddleware(OwinMiddleware.Object, WireMockMiddlewareOptions);
}
}
}
}
// public WireMockMiddleware Create()
// {
// return new WireMockMiddleware(OwinMiddleware.Object, WireMockMiddlewareOptions);
// }
// }
// }
//}