Rewrite some unit-integration-tests to unit-tests (#206 #207)

Rewrite some unit-integration-tests to unit-tests (#206)
This commit is contained in:
Stef Heyenrath
2018-09-26 17:43:26 +02:00
committed by GitHub
parent 713e59bbf9
commit ada50d893f
134 changed files with 9159 additions and 8434 deletions

View File

@@ -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()