mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-21 00:28:59 +01:00
stateful behavior + tests
This commit is contained in:
@@ -407,7 +407,7 @@ namespace WireMock.Net.Tests
|
||||
|
||||
var requestLoggedB = _server.LogEntries.Last();
|
||||
Check.That(requestLoggedB.RequestMessage.Path).EndsWith("/foo3");
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
77
test/WireMock.Net.Tests/StatefulBehaviorTests.cs
Normal file
77
test/WireMock.Net.Tests/StatefulBehaviorTests.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
public class StatefulBehaviorTests : IDisposable
|
||||
{
|
||||
private FluentMockServer _server;
|
||||
|
||||
[Fact]
|
||||
public async Task Should_skip_non_relevant_states()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/foo")
|
||||
.UsingGet())
|
||||
.WhenStateIs("Test state")
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ msg: ""Hello world!""}"));
|
||||
|
||||
// when
|
||||
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||
|
||||
// then
|
||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_process_request_if_equals_state()
|
||||
{
|
||||
// given
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/foo")
|
||||
.UsingGet())
|
||||
.WillSetStateTo("Test state")
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"No state msg"));
|
||||
|
||||
_server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/foo")
|
||||
.UsingGet())
|
||||
.WhenStateIs("Test state")
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"Test state msg"));
|
||||
|
||||
// when
|
||||
var responseNoState = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||
var responseWithState = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||
|
||||
// then
|
||||
Check.That(responseNoState).Equals("No state msg");
|
||||
Check.That(responseWithState).Equals("Test state msg");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_server?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
49
test/WireMock.Net.Tests/WireMockMiddlewareTests.cs
Normal file
49
test/WireMock.Net.Tests/WireMockMiddlewareTests.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
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;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
public class WireMockMiddlewareTests
|
||||
{
|
||||
private readonly ObjectMother _objectMother = new ObjectMother();
|
||||
|
||||
[Fact]
|
||||
public void Should_have_default_state_as_null()
|
||||
{
|
||||
// given
|
||||
|
||||
// when
|
||||
var sut = _objectMother.Create();
|
||||
|
||||
// then
|
||||
Check.That(sut.State).IsNull();
|
||||
}
|
||||
|
||||
internal class ObjectMother
|
||||
{
|
||||
public Mock<OwinMiddleware> OwinMiddleware { get; set; }
|
||||
public Mock<IOwinContext> OwinContext { get; set; }
|
||||
public WireMockMiddlewareOptions WireMockMiddlewareOptions { get; set; }
|
||||
|
||||
public ObjectMother()
|
||||
{
|
||||
OwinContext = new Mock<IOwinContext>();
|
||||
OwinMiddleware = new Mock<OwinMiddleware>(null);
|
||||
WireMockMiddlewareOptions = new WireMockMiddlewareOptions();
|
||||
}
|
||||
|
||||
public WireMockMiddleware Create()
|
||||
{
|
||||
return new WireMockMiddleware(OwinMiddleware.Object, WireMockMiddlewareOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user