mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-25 10:31:01 +01:00
Rewrite some unit-integration-tests to unit-tests (#206)
This commit is contained in:
@@ -19,7 +19,7 @@ namespace WireMock.Net.Tests
|
||||
server.SetBasicAuthentication("x", "y");
|
||||
|
||||
// Assert
|
||||
var options = server.GetPrivateFieldValue<WireMockMiddlewareOptions>("_options");
|
||||
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
|
||||
Check.That(options.AuthorizationMatcher.Name).IsEqualTo("RegexMatcher");
|
||||
Check.That(options.AuthorizationMatcher.MatchBehaviour).IsEqualTo(MatchBehaviour.AcceptOnMatch);
|
||||
Check.That(options.AuthorizationMatcher.GetPatterns()).ContainsExactly("^(?i)BASIC eDp5$");
|
||||
@@ -36,7 +36,7 @@ namespace WireMock.Net.Tests
|
||||
server.RemoveBasicAuthentication();
|
||||
|
||||
// Assert
|
||||
var options = server.GetPrivateFieldValue<WireMockMiddlewareOptions>("_options");
|
||||
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
|
||||
Check.That(options.AuthorizationMatcher).IsNull();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Linq;
|
||||
using Moq;
|
||||
using NFluent;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Owin;
|
||||
using WireMock.Server;
|
||||
using WireMock.Settings;
|
||||
@@ -9,6 +11,14 @@ namespace WireMock.Net.Tests
|
||||
{
|
||||
public class FluentMockServerSettingsTests
|
||||
{
|
||||
private Mock<IWireMockLogger> _loggerMock;
|
||||
|
||||
public FluentMockServerSettingsTests()
|
||||
{
|
||||
_loggerMock = new Mock<IWireMockLogger>();
|
||||
_loggerMock.Setup(l => l.Info(It.IsAny<string>(), It.IsAny<object[]>()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FluentMockServer_FluentMockServerSettings_StartAdminInterfaceTrue_BasicAuthenticationIsSet()
|
||||
{
|
||||
@@ -21,7 +31,7 @@ namespace WireMock.Net.Tests
|
||||
});
|
||||
|
||||
// Assert
|
||||
var options = server.GetPrivateFieldValue<WireMockMiddlewareOptions>("_options");
|
||||
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
|
||||
Check.That(options.AuthorizationMatcher).IsNotNull();
|
||||
}
|
||||
|
||||
@@ -37,7 +47,7 @@ namespace WireMock.Net.Tests
|
||||
});
|
||||
|
||||
// Assert
|
||||
var options = server.GetPrivateFieldValue<WireMockMiddlewareOptions>("_options");
|
||||
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
|
||||
Check.That(options.AuthorizationMatcher).IsNull();
|
||||
}
|
||||
|
||||
@@ -93,5 +103,38 @@ namespace WireMock.Net.Tests
|
||||
Check.That(mappings.Count()).IsEqualTo(1);
|
||||
Check.That(mappings[0].Priority).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FluentMockServer_FluentMockServerSettings_AllowPartialMapping()
|
||||
{
|
||||
// Assign and Act
|
||||
var server = FluentMockServer.Start(new FluentMockServerSettings
|
||||
{
|
||||
Logger = _loggerMock.Object,
|
||||
AllowPartialMapping = true
|
||||
});
|
||||
|
||||
// Assert
|
||||
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
|
||||
Check.That(options.AllowPartialMapping).IsTrue();
|
||||
|
||||
// Verify
|
||||
_loggerMock.Verify(l => l.Info(It.IsAny<string>(), It.IsAny<bool>()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FluentMockServer_FluentMockServerSettings_RequestLogExpirationDuration()
|
||||
{
|
||||
// Assign and Act
|
||||
var server = FluentMockServer.Start(new FluentMockServerSettings
|
||||
{
|
||||
Logger = _loggerMock.Object,
|
||||
RequestLogExpirationDuration = 1
|
||||
});
|
||||
|
||||
// Assert
|
||||
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
|
||||
Check.That(options.RequestLogExpirationDuration).IsEqualTo(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
using NFluent;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.RequestBuilders;
|
||||
@@ -17,117 +14,6 @@ namespace WireMock.Net.Tests
|
||||
{
|
||||
public class FluentMockServerTests
|
||||
{
|
||||
private static string jsonRequestMessage = @"{ ""message"" : ""Hello server"" }";
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_methodPatch()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
_server.Given(Request.Create().WithPath(path).UsingMethod("patch"))
|
||||
.RespondWith(Response.Create().WithBody("hello patch"));
|
||||
|
||||
// when
|
||||
var msg = new HttpRequestMessage(new HttpMethod("PATCH"), new Uri("http://localhost:" + _server.Ports[0] + path))
|
||||
{
|
||||
Content = new StringContent("{\"data\": {\"attr\":\"value\"}}")
|
||||
};
|
||||
var response = await new HttpClient().SendAsync(msg);
|
||||
|
||||
// then
|
||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
Check.That(responseBody).IsEqualTo("hello patch");
|
||||
|
||||
Check.That(_server.LogEntries).HasSize(1);
|
||||
var requestLogged = _server.LogEntries.First();
|
||||
Check.That(requestLogged.RequestMessage.Method).IsEqualTo("PATCH");
|
||||
Check.That(requestLogged.RequestMessage.Body).IsNotNull();
|
||||
Check.That(requestLogged.RequestMessage.Body).IsEqualTo("{\"data\": {\"attr\":\"value\"}}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_bodyAsString()
|
||||
{
|
||||
// given
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/foo")
|
||||
.UsingGet())
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody("Hello world!"));
|
||||
|
||||
// when
|
||||
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||
|
||||
// then
|
||||
Check.That(response).IsEqualTo("Hello world!");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_BodyAsJson()
|
||||
{
|
||||
// Assign
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create().UsingAnyMethod())
|
||||
.RespondWith(Response.Create().WithBodyAsJson(new { message = "Hello" }));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0]);
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("{\"message\":\"Hello\"}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_BodyAsJson_Indented()
|
||||
{
|
||||
// Assign
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create().UsingAnyMethod())
|
||||
.RespondWith(Response.Create().WithBodyAsJson(new { message = "Hello" }, true));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0]);
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo($"{{{Environment.NewLine} \"message\": \"Hello\"{Environment.NewLine}}}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_bodyAsCallback()
|
||||
{
|
||||
// Assign
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/foo")
|
||||
.UsingGet())
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(500)
|
||||
.WithHeader("H1", "X1")
|
||||
.WithBody(req => $"path: {req.Path}"));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||
|
||||
// Assert
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
Check.That(content).IsEqualTo("path: /foo");
|
||||
Check.That((int) response.StatusCode).IsEqualTo(500);
|
||||
Check.That(response.Headers.GetValues("H1")).ContainsExactly("X1");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_bodyAsBase64()
|
||||
{
|
||||
@@ -143,97 +29,6 @@ namespace WireMock.Net.Tests
|
||||
Check.That(response).IsEqualTo("Hello World?");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_bodyAsBytes()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
_server.Given(Request.Create().WithPath(path).UsingGet()).RespondWith(Response.Create().WithBody(new byte[] { 48, 49 }));
|
||||
|
||||
// when
|
||||
var responseAsString = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + path);
|
||||
var responseAsBytes = await new HttpClient().GetByteArrayAsync("http://localhost:" + _server.Ports[0] + path);
|
||||
|
||||
// then
|
||||
Check.That(responseAsString).IsEqualTo("01");
|
||||
Check.That(responseAsBytes).ContainsExactly(new byte[] { 48, 49 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_valid_matchers_when_sent_json()
|
||||
{
|
||||
// Assign
|
||||
var validMatchersForHelloServerJsonMessage = new List<object[]>
|
||||
{
|
||||
new object[] { new WildcardMatcher("*Hello server*"), "application/json" },
|
||||
new object[] { new WildcardMatcher("*Hello server*"), "text/plain" },
|
||||
new object[] { new ExactMatcher(jsonRequestMessage), "application/json" },
|
||||
new object[] { new ExactMatcher(jsonRequestMessage), "text/plain" },
|
||||
new object[] { new RegexMatcher("Hello server"), "application/json" },
|
||||
new object[] { new RegexMatcher("Hello server"), "text/plain" },
|
||||
new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "application/json" },
|
||||
new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "text/plain" }
|
||||
};
|
||||
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
foreach (var item in validMatchersForHelloServerJsonMessage)
|
||||
{
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
_server
|
||||
.Given(Request.Create().WithPath(path).WithBody((IMatcher)item[0]))
|
||||
.RespondWith(Response.Create().WithBody("Hello client"));
|
||||
|
||||
// Act
|
||||
var content = new StringContent(jsonRequestMessage, Encoding.UTF8, (string)item[1]);
|
||||
var response = await new HttpClient().PostAsync("http://localhost:" + _server.Ports[0] + path, content);
|
||||
|
||||
// Assert
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
Check.That(responseString).Equals("Hello client");
|
||||
|
||||
_server.ResetMappings();
|
||||
_server.ResetLogEntries();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_404_for_unexpected_request()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo{Guid.NewGuid()}";
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
// when
|
||||
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + path);
|
||||
|
||||
// then
|
||||
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
|
||||
Check.That((int)response.StatusCode).IsEqualTo(404);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_find_a_request_satisfying_a_request_spec()
|
||||
{
|
||||
// Assign
|
||||
string path = $"/bar_{Guid.NewGuid()}";
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
// when
|
||||
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + path);
|
||||
|
||||
// then
|
||||
var result = _server.FindLogEntries(Request.Create().WithPath(new RegexMatcher("^/b.*"))).ToList();
|
||||
Check.That(result).HasSize(1);
|
||||
|
||||
var requestLogged = result.First();
|
||||
Check.That(requestLogged.RequestMessage.Path).IsEqualTo(path);
|
||||
Check.That(requestLogged.RequestMessage.Url).IsEqualTo("http://localhost:" + _server.Ports[0] + path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_reset_requestlogs()
|
||||
{
|
||||
@@ -362,23 +157,6 @@ namespace WireMock.Net.Tests
|
||||
// Check.That(result).Contains("google");
|
||||
//}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_request_callback()
|
||||
{
|
||||
// Assign
|
||||
var _server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create().WithPath("/foo").UsingGet())
|
||||
.RespondWith(Response.Create().WithCallback(req => new ResponseMessage { Body = req.Path + "Bar" }));
|
||||
|
||||
// Act
|
||||
string response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("/fooBar");
|
||||
}
|
||||
|
||||
#if !NET452
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_not_exclude_restrictedResponseHeader_for_ASPNETCORE()
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using NFluent;
|
||||
using WireMock.Owin;
|
||||
using WireMock.Owin.Mappers;
|
||||
using Xunit;
|
||||
#if NET452
|
||||
using IContext = Microsoft.Owin.IOwinContext;
|
||||
using IResponse = Microsoft.Owin.IOwinResponse;
|
||||
#else
|
||||
using IContext = Microsoft.AspNetCore.Http.HttpContext;
|
||||
using IResponse = Microsoft.AspNetCore.Http.HttpResponse;
|
||||
#endif
|
||||
|
||||
namespace WireMock.Net.Tests.Owin
|
||||
{
|
||||
public class GlobalExceptionMiddlewareTests
|
||||
{
|
||||
private Mock<IWireMockMiddlewareOptions> _optionsMock;
|
||||
private Mock<IOwinResponseMapper> _responseMapperMock;
|
||||
private Mock<IContext> _contextMock;
|
||||
|
||||
private GlobalExceptionMiddleware _sut;
|
||||
|
||||
public GlobalExceptionMiddlewareTests()
|
||||
{
|
||||
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
||||
_optionsMock.SetupAllProperties();
|
||||
|
||||
_responseMapperMock = new Mock<IOwinResponseMapper>();
|
||||
_responseMapperMock.SetupAllProperties();
|
||||
_responseMapperMock.Setup(m => m.MapAsync(It.IsAny<ResponseMessage>(), It.IsAny<IResponse>())).Returns(Task.FromResult(true));
|
||||
|
||||
_sut = new GlobalExceptionMiddleware(null, _optionsMock.Object, _responseMapperMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GlobalExceptionMiddleware_Invoke_NullAsNext_Throws()
|
||||
{
|
||||
// Act
|
||||
Check.ThatAsyncCode(() => _sut.Invoke(_contextMock.Object)).ThrowsAny();
|
||||
}
|
||||
}
|
||||
}
|
||||
166
test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs
Normal file
166
test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Xunit;
|
||||
using Moq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using WireMock.Owin.Mappers;
|
||||
using WireMock.Util;
|
||||
#if NET452
|
||||
using Microsoft.Owin;
|
||||
using IResponse = Microsoft.Owin.IOwinResponse;
|
||||
using Response = Microsoft.Owin.OwinResponse;
|
||||
#else
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using IResponse = Microsoft.AspNetCore.Http.HttpResponse;
|
||||
using Response = Microsoft.AspNetCore.Http.HttpResponse;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
#endif
|
||||
|
||||
namespace WireMock.Net.Tests.Owin.Mappers
|
||||
{
|
||||
public class OwinResponseMapperTests
|
||||
{
|
||||
private static Task completedTask = Task.FromResult(true);
|
||||
private OwinResponseMapper _sut;
|
||||
private Mock<IResponse> _responseMock;
|
||||
private Mock<Stream> _stream;
|
||||
private Mock<IHeaderDictionary> _headers;
|
||||
|
||||
public OwinResponseMapperTests()
|
||||
{
|
||||
_stream = new Mock<Stream>();
|
||||
_stream.SetupAllProperties();
|
||||
_stream.Setup(s => s.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>())).Returns(completedTask);
|
||||
|
||||
_headers = new Mock<IHeaderDictionary>();
|
||||
_headers.SetupAllProperties();
|
||||
#if NET452
|
||||
_headers.Setup(h => h.AppendValues(It.IsAny<string>(), It.IsAny<string[]>()));
|
||||
#else
|
||||
_headers.Setup(h => h.Add(It.IsAny<string>(), It.IsAny<StringValues>()));
|
||||
#endif
|
||||
|
||||
_responseMock = new Mock<IResponse>();
|
||||
_responseMock.SetupAllProperties();
|
||||
_responseMock.SetupGet(r => r.Body).Returns(_stream.Object);
|
||||
_responseMock.SetupGet(r => r.Headers).Returns(_headers.Object);
|
||||
|
||||
_sut = new OwinResponseMapper();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void OwinResponseMapper_MapAsync_Null()
|
||||
{
|
||||
// Act
|
||||
await _sut.MapAsync(null, _responseMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void OwinResponseMapper_MapAsync_StatusCode()
|
||||
{
|
||||
// Assign
|
||||
var responseMessage = new ResponseMessage
|
||||
{
|
||||
StatusCode = 302
|
||||
};
|
||||
|
||||
// Act
|
||||
await _sut.MapAsync(responseMessage, _responseMock.Object);
|
||||
|
||||
// Assert
|
||||
_responseMock.VerifySet(r => r.StatusCode = 302, Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void OwinResponseMapper_MapAsync_NoBody()
|
||||
{
|
||||
// Assign
|
||||
var responseMessage = new ResponseMessage
|
||||
{
|
||||
Headers = new Dictionary<string, WireMockList<string>>()
|
||||
};
|
||||
|
||||
// Act
|
||||
await _sut.MapAsync(responseMessage, _responseMock.Object);
|
||||
|
||||
// Assert
|
||||
_stream.Verify(s => s.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void OwinResponseMapper_MapAsync_Body()
|
||||
{
|
||||
// Assign
|
||||
string body = "abc";
|
||||
var responseMessage = new ResponseMessage
|
||||
{
|
||||
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||
Body = body
|
||||
};
|
||||
|
||||
// Act
|
||||
await _sut.MapAsync(responseMessage, _responseMock.Object);
|
||||
|
||||
// Assert
|
||||
_stream.Verify(s => s.WriteAsync(new byte[] { 97, 98, 99 }, 0, 3, It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void OwinResponseMapper_MapAsync_BodyAsBytes()
|
||||
{
|
||||
// Assign
|
||||
var bytes = new byte[] { 48, 49 };
|
||||
var responseMessage = new ResponseMessage
|
||||
{
|
||||
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||
BodyAsBytes = bytes
|
||||
};
|
||||
|
||||
// Act
|
||||
await _sut.MapAsync(responseMessage, _responseMock.Object);
|
||||
|
||||
// Assert
|
||||
_stream.Verify(s => s.WriteAsync(bytes, 0, bytes.Length, It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void OwinResponseMapper_MapAsync_BodyAsJson()
|
||||
{
|
||||
// Assign
|
||||
var responseMessage = new ResponseMessage
|
||||
{
|
||||
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||
BodyAsJson = new { t = "x", i = (string)null },
|
||||
BodyAsJsonIndented = false
|
||||
};
|
||||
|
||||
// Act
|
||||
await _sut.MapAsync(responseMessage, _responseMock.Object);
|
||||
|
||||
// Assert
|
||||
_stream.Verify(s => s.WriteAsync(new byte[] { 123, 34, 116, 34, 58, 34, 120, 34, 125 }, 0, 9, It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void OwinResponseMapper_MapAsync_SetResponseHeaders()
|
||||
{
|
||||
// Assign
|
||||
var responseMessage = new ResponseMessage
|
||||
{
|
||||
Headers = new Dictionary<string, WireMockList<string>> { { "h", new WireMockList<string>("x", "y") } }
|
||||
};
|
||||
|
||||
// Act
|
||||
await _sut.MapAsync(responseMessage, _responseMock.Object);
|
||||
|
||||
// Assert
|
||||
#if NET452
|
||||
_headers.Verify(h => h.AppendValues("h", new string[] { "x", "y" } ), Times.Once);
|
||||
#else
|
||||
var v = new StringValues();
|
||||
_headers.Verify(h => h.TryGetValue("h", out v), Times.Once);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
99
test/WireMock.Net.Tests/Owin/MappingMatcherTests.cs
Normal file
99
test/WireMock.Net.Tests/Owin/MappingMatcherTests.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using Moq;
|
||||
using NFluent;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.Owin;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Owin
|
||||
{
|
||||
public class MappingMatcherTests
|
||||
{
|
||||
private Mock<IWireMockMiddlewareOptions> _optionsMock;
|
||||
|
||||
private IMappingMatcher _sut;
|
||||
|
||||
public MappingMatcherTests()
|
||||
{
|
||||
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
||||
_optionsMock.SetupAllProperties();
|
||||
_optionsMock.Setup(o => o.Mappings).Returns(new ConcurrentDictionary<Guid, IMapping>());
|
||||
_optionsMock.Setup(o => o.LogEntries).Returns(new ConcurentObservableCollection<LogEntry>());
|
||||
_optionsMock.Setup(o => o.Scenarios).Returns(new ConcurrentDictionary<string, ScenarioState>());
|
||||
|
||||
_sut = new MappingMatcher(_optionsMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MappingMatcher_Match_NoMappingsDefined()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
||||
|
||||
// Act
|
||||
var result = _sut.Match(request);
|
||||
|
||||
// Assert and Verify
|
||||
Check.That(result.Mapping).IsNull();
|
||||
Check.That(result.RequestMatchResult).IsNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MappingMatcher_Match_GetBestMapping_Exact()
|
||||
{
|
||||
// Assign
|
||||
var mappings = InitMappings(new[] { (Guid.Parse("00000000-0000-0000-0000-000000000001"), 0.1), (Guid.Parse("00000000-0000-0000-0000-000000000002"), 1.0) });
|
||||
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
||||
|
||||
// Act
|
||||
var result = _sut.Match(request);
|
||||
|
||||
// Assert and Verify
|
||||
Check.That(result.Mapping.Guid).IsEqualTo(Guid.Parse("00000000-0000-0000-0000-000000000002"));
|
||||
Check.That(result.RequestMatchResult.AverageTotalScore).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MappingMatcher_Match_GetBestMapping_AllowPartialMapping()
|
||||
{
|
||||
// Assign
|
||||
_optionsMock.SetupGet(o => o.AllowPartialMapping).Returns(true);
|
||||
var mappings = InitMappings(new[] { (Guid.Parse("00000000-0000-0000-0000-000000000001"), 0.1), (Guid.Parse("00000000-0000-0000-0000-000000000002"), 0.9) });
|
||||
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
||||
|
||||
// Act
|
||||
var result = _sut.Match(request);
|
||||
|
||||
// Assert and Verify
|
||||
Check.That(result.Mapping.Guid).IsEqualTo(Guid.Parse("00000000-0000-0000-0000-000000000002"));
|
||||
Check.That(result.RequestMatchResult.AverageTotalScore).IsEqualTo(0.9);
|
||||
}
|
||||
|
||||
private ConcurrentDictionary<Guid, IMapping> InitMappings(params (Guid guid, double match)[] matches)
|
||||
{
|
||||
var mappings = new ConcurrentDictionary<Guid, IMapping>();
|
||||
|
||||
foreach (var match in matches)
|
||||
{
|
||||
var mappingMock = new Mock<IMapping>();
|
||||
mappingMock.SetupGet(m => m.Guid).Returns(match.guid);
|
||||
|
||||
var partialMatchResult = new RequestMatchResult();
|
||||
partialMatchResult.AddScore(typeof(object), match.match);
|
||||
mappingMock.Setup(m => m.GetRequestMatchResult(It.IsAny<RequestMessage>(), It.IsAny<string>())).Returns(partialMatchResult);
|
||||
|
||||
mappings.TryAdd(match.guid, mappingMock.Object);
|
||||
}
|
||||
|
||||
return mappings;
|
||||
}
|
||||
}
|
||||
}
|
||||
138
test/WireMock.Net.Tests/Owin/WireMockMiddlewareTests.cs
Normal file
138
test/WireMock.Net.Tests/Owin/WireMockMiddlewareTests.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Models;
|
||||
using WireMock.Owin;
|
||||
using WireMock.Owin.Mappers;
|
||||
using WireMock.Util;
|
||||
using WireMock.Admin.Requests;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Matchers;
|
||||
using System.Collections.Generic;
|
||||
#if NET452
|
||||
using Microsoft.Owin;
|
||||
using IContext = Microsoft.Owin.IOwinContext;
|
||||
using IRequest = Microsoft.Owin.IOwinRequest;
|
||||
using IResponse = Microsoft.Owin.IOwinResponse;
|
||||
#else
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using IContext = Microsoft.AspNetCore.Http.HttpContext;
|
||||
using IRequest = Microsoft.AspNetCore.Http.HttpRequest;
|
||||
using IResponse = Microsoft.AspNetCore.Http.HttpResponse;
|
||||
#endif
|
||||
|
||||
namespace WireMock.Net.Tests.Owin
|
||||
{
|
||||
public class WireMockMiddlewareTests
|
||||
{
|
||||
private WireMockMiddleware _sut;
|
||||
|
||||
private Mock<IWireMockMiddlewareOptions> _optionsMock;
|
||||
private Mock<IOwinRequestMapper> _requestMapperMock;
|
||||
private Mock<IOwinResponseMapper> _responseMapperMock;
|
||||
private Mock<IMappingMatcher> _matcherMock;
|
||||
private Mock<IMapping> _mappingMock;
|
||||
private Mock<IContext> _contextMock;
|
||||
|
||||
public WireMockMiddlewareTests()
|
||||
{
|
||||
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
||||
_optionsMock.SetupAllProperties();
|
||||
_optionsMock.Setup(o => o.Mappings).Returns(new ConcurrentDictionary<Guid, IMapping>());
|
||||
_optionsMock.Setup(o => o.LogEntries).Returns(new ConcurentObservableCollection<LogEntry>());
|
||||
_optionsMock.Setup(o => o.Scenarios).Returns(new ConcurrentDictionary<string, ScenarioState>());
|
||||
_optionsMock.Setup(o => o.Logger.Warn(It.IsAny<string>(), It.IsAny<object[]>()));
|
||||
_optionsMock.Setup(o => o.Logger.Error(It.IsAny<string>(), It.IsAny<object[]>()));
|
||||
_optionsMock.Setup(o => o.Logger.DebugRequestResponse(It.IsAny<LogEntryModel>(), It.IsAny<bool>()));
|
||||
|
||||
_requestMapperMock = new Mock<IOwinRequestMapper>();
|
||||
_requestMapperMock.SetupAllProperties();
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>())).ReturnsAsync(request);
|
||||
|
||||
_responseMapperMock = new Mock<IOwinResponseMapper>();
|
||||
_responseMapperMock.SetupAllProperties();
|
||||
_responseMapperMock.Setup(m => m.MapAsync(It.IsAny<ResponseMessage>(), It.IsAny<IResponse>())).Returns(Task.FromResult(true));
|
||||
|
||||
_matcherMock = new Mock<IMappingMatcher>();
|
||||
_matcherMock.SetupAllProperties();
|
||||
_matcherMock.Setup(m => m.Match(It.IsAny<RequestMessage>())).Returns(((IMapping)null, (RequestMatchResult)null));
|
||||
|
||||
_contextMock = new Mock<IContext>();
|
||||
|
||||
_mappingMock = new Mock<IMapping>();
|
||||
|
||||
_sut = new WireMockMiddleware(null, _optionsMock.Object, _requestMapperMock.Object, _responseMapperMock.Object, _matcherMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void WireMockMiddleware_Invoke_NoMatch()
|
||||
{
|
||||
// Act
|
||||
await _sut.Invoke(_contextMock.Object);
|
||||
|
||||
// Assert and Verify
|
||||
_optionsMock.Verify(o => o.Logger.Warn(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
|
||||
|
||||
Expression<Func<ResponseMessage, bool>> match = r => r.StatusCode == 404 && ((StatusModel)r.BodyAsJson).Status == "No matching mapping found";
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void WireMockMiddleware_Invoke_IsAdminInterface_EmptyHeaders_401()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]>());
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>())).ReturnsAsync(request);
|
||||
|
||||
_optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher());
|
||||
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
|
||||
_matcherMock.Setup(m => m.Match(It.IsAny<RequestMessage>())).Returns((_mappingMock.Object, (RequestMatchResult)null));
|
||||
|
||||
// Act
|
||||
await _sut.Invoke(_contextMock.Object);
|
||||
|
||||
// Assert and Verify
|
||||
_optionsMock.Verify(o => o.Logger.Error(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
|
||||
|
||||
Expression<Func<ResponseMessage, bool>> match = r => r.StatusCode == 401;
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void WireMockMiddleware_Invoke_IsAdminInterface_MissingHeader_401()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]> { { "h", new[] { "x" } } });
|
||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>())).ReturnsAsync(request);
|
||||
|
||||
_optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher());
|
||||
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
|
||||
_matcherMock.Setup(m => m.Match(It.IsAny<RequestMessage>())).Returns((_mappingMock.Object, (RequestMatchResult)null));
|
||||
|
||||
// Act
|
||||
await _sut.Invoke(_contextMock.Object);
|
||||
|
||||
// Assert and Verify
|
||||
_optionsMock.Verify(o => o.Logger.Error(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
|
||||
|
||||
Expression<Func<ResponseMessage, bool>> match = r => r.StatusCode == 401;
|
||||
_responseMapperMock.Verify(m => m.MapAsync(It.Is(match), It.IsAny<IResponse>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void WireMockMiddleware_Invoke_RequestLogExpirationDurationIsDefined()
|
||||
{
|
||||
// Assign
|
||||
_optionsMock.SetupGet(o => o.RequestLogExpirationDuration).Returns(1);
|
||||
|
||||
// Act
|
||||
await _sut.Invoke(_contextMock.Object);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,25 @@
|
||||
using System.Collections.Generic;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
public class RequestBuilderUsingMethodTests
|
||||
{
|
||||
[Fact]
|
||||
public void RequestBuilder_UsingPatch()
|
||||
{
|
||||
// Act
|
||||
var requestBuilder = (Request)Request.Create().UsingPatch();
|
||||
|
||||
// Assert 1
|
||||
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||
Check.That(matchers.Count()).IsEqualTo(1);
|
||||
Check.That((matchers[0] as RequestMessageMethodMatcher).Methods).ContainsExactly("PATCH");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestBuilder_UsingAnyMethod_ClearsAllOtherMatches()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
public class RequestBuilderWithBodyTests
|
||||
{
|
||||
[Fact]
|
||||
public void RequestBuilder_WithBody_IMatcher()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new WildcardMatcher("x");
|
||||
|
||||
// Act
|
||||
var requestBuilder = (Request)Request.Create().WithBody(matcher);
|
||||
|
||||
// Assert
|
||||
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||
Check.That(matchers.Count()).IsEqualTo(1);
|
||||
Check.That(((RequestMessageBodyMatcher) matchers[0]).Matcher).IsEqualTo(matcher);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,7 @@
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
@@ -24,7 +22,7 @@ namespace WireMock.Net.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestBuilder_WithCookie_String_IExactMatcher()
|
||||
public void RequestBuilder_WithCookie_String_IStringMatcher()
|
||||
{
|
||||
// Act
|
||||
var requestBuilder = (Request)Request.Create().WithCookie("c", new ExactMatcher("v"));
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace WireMock.Net.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestBuilder_WithHeader_String_IExactMatcher()
|
||||
public void RequestBuilder_WithHeader_String_IStringMatcher()
|
||||
{
|
||||
// Act
|
||||
var requestBuilder = (Request)Request.Create().WithHeader("h", new ExactMatcher("v"));
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.Models;
|
||||
using WireMock.ResponseBuilders;
|
||||
@@ -9,14 +8,12 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
||||
{
|
||||
public class ResponseCreateTests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
[Fact]
|
||||
public async Task Response_Create()
|
||||
public async Task Response_Create_Func()
|
||||
{
|
||||
// Assign
|
||||
var responseMessage = new ResponseMessage { StatusCode = 500 };
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", ClientIp);
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", "::1");
|
||||
|
||||
var response = Response.Create(() => responseMessage);
|
||||
|
||||
|
||||
@@ -96,6 +96,28 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
|
||||
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBody_Object_Indented()
|
||||
{
|
||||
// given
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "abc"
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
object x = new { message = "Hello" };
|
||||
var response = Response.Create().WithBodyAsJson(x, true);
|
||||
|
||||
// act
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// then
|
||||
Check.That(responseMessage.BodyAsJson).IsNotNull();
|
||||
Check.That(responseMessage.BodyAsJson).Equals(x);
|
||||
Check.That(responseMessage.BodyAsJsonIndented).IsEqualTo(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBody_String_SameAsSource_Encoding()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.Models;
|
||||
using WireMock.ResponseBuilders;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.ResponseBuilderTests
|
||||
{
|
||||
public class ResponseWithCallbackTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Response_WithCallback()
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
||||
var response = Response.Create().WithCallback(req => new ResponseMessage { Body = req.Path + "Bar", StatusCode = 302 });
|
||||
|
||||
// Act
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// Assert
|
||||
Check.That(responseMessage.Body).IsEqualTo("/fooBar");
|
||||
Check.That(responseMessage.StatusCode).IsEqualTo(302);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.Models;
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
//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.Scenarios).IsNull();
|
||||
// }
|
||||
|
||||
// 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 WireMockMiddleware Create()
|
||||
// {
|
||||
// return new WireMockMiddleware(OwinMiddleware.Object, WireMockMiddlewareOptions);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user