Version 2.x (#1359)

* Version 2.x

* Setup .NET 9

* 12

* cleanup some #if for NETSTANDARD1_3

* cleanup + fix tests for net8

* openapi

* NO ConfigureAwait(false) + cleanup

* .

* #endif

* HashSet

* WireMock.Net.NUnit

* HttpContext

* Add WebSockets (#1423)

* Add WebSockets

* Add tests

* fix

* more tests

* Add tests

* ...

* remove IOwin

* -

* tests

* fluent

* ok

* match

* .

* byte[]

* x

* func

* func

* byte

* trans

* ...

* frameworks.........

* jmes

* xxx

* sc

* using var httpClient = new HttpClient();

* usings

* maxRetries

* up

* xunit v3

* ct

* ---

* ct

* ct2

* T Unit

* WireMock.Net.TUnitTests / 10

* t unit first

* --project

* no tunit

* t2

* --project

* --project

* ci -  --project

* publish ./test/wiremock-coverage.xml

* windows

* .

* log

* ...

* log

* goed

* BodyType

* .

* .

* --scenario

* ...

* pact

* ct

* .

* WireMock.Net.RestClient.AwesomeAssertions (#1427)

* WireMock.Net.RestClient.AwesomeAssertions

* ok

* atpath

* fix test

* sonar fixes

* ports

* proxy test

* FIX?

* ---

* await Task.Delay(100, _ct);

* ?

* --project

* Aspire: use IDistributedApplicationEventingSubscriber (#1428)

* broadcast

* ok

* more tsts

* .

* Collection

* up

* .

* 2

* remove nfluent

* <VersionPrefix>2.0.0-preview-02</VersionPrefix>

* ...

* .

* nuget icon

* .

* <PackageReference Include="JmesPath.Net" Version="1.1.0" />

* x

* 500

* .

* fix some warnings

* ws
This commit is contained in:
Stef Heyenrath
2026-03-11 17:02:47 +01:00
committed by GitHub
parent d6e19532bc
commit a292f28dda
521 changed files with 79740 additions and 5246 deletions

View File

@@ -1,15 +1,11 @@
// Copyright © WireMock.Net
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using NFluent;
using WireMock.Constants;
using WireMock.Handlers;
using WireMock.Matchers;
@@ -19,12 +15,13 @@ using WireMock.ResponseBuilders;
using WireMock.Server;
using WireMock.Settings;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests;
public class WireMockServerProxyTests
{
private readonly CancellationToken _ct = TestContext.Current.CancellationToken;
[Fact(Skip = "Fails in Linux CI")]
public async Task WireMockServer_ProxySSL_Should_log_proxied_requests()
{
@@ -49,17 +46,19 @@ public class WireMockServerProxyTests
RequestUri = new Uri(server.Urls[0])
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(httpClientHandler);
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
Check.That(server.Mappings).HasSize(2);
Check.That(server.LogEntries).HasSize(1);
server.Mappings.Should().HaveCount(2);
server.LogEntries.Should().HaveCount(1);
}
[Fact]
public async Task WireMockServer_Proxy_AdminFalse_With_SaveMapping_Is_True_And_SaveMappingToFile_Is_False_Should_AddInternalMappingOnly()
{
// Assign
var cancellationToken = TestContext.Current.CancellationToken;
var settings = new WireMockServerSettings
{
ProxyAndRecordSettings = new ProxyAndRecordSettings
@@ -67,14 +66,14 @@ public class WireMockServerProxyTests
Url = "http://www.google.com",
SaveMapping = true,
SaveMappingToFile = false,
ExcludedHeaders = new[] { "Connection" } // Needed for .NET 4.5.x and 4.6.x
ExcludedHeaders = ["Connection"] // Needed for .NET 4.5.x and 4.6.x
}
};
var server = WireMockServer.Start(settings);
// Act
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
var client = new HttpClient(httpClientHandler);
using var client = new HttpClient(httpClientHandler);
for (int i = 0; i < 5; i++)
{
var requestMessage = new HttpRequestMessage
@@ -82,7 +81,7 @@ public class WireMockServerProxyTests
Method = HttpMethod.Get,
RequestUri = new Uri(server.Url!)
};
await client.SendAsync(requestMessage).ConfigureAwait(false);
await client.SendAsync(requestMessage, cancellationToken);
}
// Assert
@@ -93,6 +92,7 @@ public class WireMockServerProxyTests
public async Task WireMockServer_Proxy_AdminTrue_With_SaveMapping_Is_True_And_SaveMappingToFile_Is_False_Should_AddInternalMappingOnly()
{
// Assign
var cancellationToken = TestContext.Current.CancellationToken;
var settings = new WireMockServerSettings
{
ProxyAndRecordSettings = new ProxyAndRecordSettings
@@ -115,7 +115,8 @@ public class WireMockServerProxyTests
RequestUri = new Uri(server.Url!)
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(httpClientHandler);
await httpClient.SendAsync(requestMessage, cancellationToken);
}
// Assert
@@ -170,13 +171,13 @@ public class WireMockServerProxyTests
Content = new StringContent(stringBody)
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
await new HttpClient(httpClientHandler).SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
server.Mappings.Should().HaveCount(2);
// Verify
fileSystemHandlerMock.Verify(f => f.WriteMappingFile($"m{System.IO.Path.DirectorySeparatorChar}Proxy Mapping for _{title}.json", It.IsRegex(stringBody)), Times.Once);
fileSystemHandlerMock.Verify(f => f.WriteMappingFile($"m{Path.DirectorySeparatorChar}Proxy Mapping for _{title}.json", It.IsRegex(stringBody)), Times.Once);
}
[Fact]
@@ -205,7 +206,8 @@ public class WireMockServerProxyTests
RequestUri = new Uri(server.Urls[0])
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(httpClientHandler);
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
server.Mappings.Should().HaveCount(1);
@@ -241,7 +243,8 @@ public class WireMockServerProxyTests
RequestUri = new Uri(server.Urls[0])
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(httpClientHandler);
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
server.Mappings.Should().HaveCount(1);
@@ -266,7 +269,7 @@ public class WireMockServerProxyTests
SaveMappingToFile = true,
SaveMappingSettings = new ProxySaveMappingSettings
{
HttpMethods = new ProxySaveMappingSetting<string[]>(new string[] { "GET" }, MatchBehaviour.RejectOnMatch) // To make sure that we don't want this mapping
HttpMethods = new ProxySaveMappingSetting<string[]>(["GET"], MatchBehaviour.RejectOnMatch) // To make sure that we don't want this mapping
}
},
FileSystemHandler = fileSystemHandlerMock.Object
@@ -280,7 +283,8 @@ public class WireMockServerProxyTests
RequestUri = new Uri(server.Urls[0])
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(httpClientHandler);
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
server.Mappings.Should().HaveCount(1);
@@ -311,7 +315,8 @@ public class WireMockServerProxyTests
RequestUri = new Uri(server.Urls[0])
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(httpClientHandler);
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
server.Mappings.Should().HaveCount(2);
@@ -322,6 +327,7 @@ public class WireMockServerProxyTests
public async Task WireMockServer_Proxy_Should_proxy_responses()
{
// Assign
var cancellationToken = TestContext.Current.CancellationToken;
string path = $"/prx_{Guid.NewGuid()}";
var server = WireMockServer.Start();
server
@@ -335,13 +341,14 @@ public class WireMockServerProxyTests
RequestUri = new Uri($"{server.Urls[0]}{path}")
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
var response = await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
using var httpClient = new HttpClient(httpClientHandler);
var response = await httpClient.SendAsync(requestMessage, cancellationToken);
string content = await response.Content.ReadAsStringAsync(cancellationToken);
// Assert
Check.That(server.Mappings).HasSize(1);
Check.That(server.LogEntries).HasSize(1);
Check.That(content).Contains("google");
server.Mappings.Should().HaveCount(1);
server.LogEntries.Should().HaveCount(1);
content.Should().Contain("google");
server.Stop();
}
@@ -376,23 +383,25 @@ public class WireMockServerProxyTests
};
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
requestMessage.Content.Headers.Add("bbb", "test");
await new HttpClient().SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient();
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage;
Check.That(receivedRequest.BodyData.BodyAsString).IsEqualTo("stringContent");
Check.That(receivedRequest.Headers).ContainsKey("Content-Type");
Check.That(receivedRequest.Headers["Content-Type"].First()).Contains("text/plain");
Check.That(receivedRequest.Headers).ContainsKey("bbb");
receivedRequest.BodyData.BodyAsString.Should().Be("stringContent");
receivedRequest.Headers.Should().ContainKey("Content-Type");
receivedRequest.Headers["Content-Type"].First().Should().Contain("text/plain");
receivedRequest.Headers.Should().ContainKey("bbb");
// check that new proxied mapping is added
Check.That(server.Mappings).HasSize(2);
server.Mappings.Should().HaveCount(2);
}
[Fact]
public async Task WireMockServer_Proxy_Should_preserve_Authorization_header_in_proxied_request()
{
// Assign
var cancellationToken = TestContext.Current.CancellationToken;
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
@@ -425,17 +434,17 @@ public class WireMockServerProxyTests
Content = new StringContent("stringContent", Encoding.ASCII)
};
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("BASIC", "test-A");
var result = await new HttpClient().SendAsync(requestMessage).ConfigureAwait(false);
var result = await new HttpClient().SendAsync(requestMessage, cancellationToken);
// Assert
(await result.Content.ReadAsStringAsync().ConfigureAwait(false)).Should().Be("BASIC test-A");
(await result.Content.ReadAsStringAsync(cancellationToken)).Should().Be("BASIC test-A");
var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage;
receivedRequest.Headers!["Authorization"].ToString().Should().Be("BASIC test-A");
server.Mappings.Should().HaveCount(2);
var authorizationRequestMessageHeaderMatcher = ((Request)server.Mappings.Single(m => !m.IsAdminInterface).RequestMatcher)
.GetRequestMessageMatcher<RequestMessageHeaderMatcher>(x => x.Matchers.Any(m => m.GetPatterns().Contains("BASIC test-A")));
.GetRequestMessageMatcher<RequestMessageHeaderMatcher>(x => x.Matchers!.Any(m => m.GetPatterns().Contains("BASIC test-A")));
authorizationRequestMessageHeaderMatcher.Should().NotBeNull();
}
@@ -456,7 +465,7 @@ public class WireMockServerProxyTests
Url = serverForProxyForwarding.Urls[0],
SaveMapping = true,
SaveMappingToFile = false,
ExcludedHeaders = new[] { "excluded-header-X" }
ExcludedHeaders = ["excluded-header-X"]
}
};
var server = WireMockServer.Start(settings);
@@ -471,14 +480,15 @@ public class WireMockServerProxyTests
};
requestMessage.Headers.Add("foobar", "exact_match");
requestMessage.Headers.Add("ok", "ok-value");
await new HttpClient().SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient();
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
var mapping = server.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid);
Check.That(mapping).IsNotNull();
mapping.Should().NotBeNull();
var matchers = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers<RequestMessageHeaderMatcher>().Select(m => m.Name).ToList();
Check.That(matchers).Not.Contains("excluded-header-X");
Check.That(matchers).Contains("ok");
matchers.Should().NotContain("excluded-header-X");
matchers.Should().Contain("ok");
}
[Fact]
@@ -498,7 +508,7 @@ public class WireMockServerProxyTests
Url = serverForProxyForwarding.Urls[0],
SaveMapping = true,
SaveMappingToFile = false,
ExcludedCookies = new[] { "ASP.NET_SessionId" }
ExcludedCookies = ["ASP.NET_SessionId"]
}
};
var server = WireMockServer.Start(settings);
@@ -518,16 +528,17 @@ public class WireMockServerProxyTests
cookieContainer.Add(new Uri("http://localhost"), new Cookie("GoodCookie", "I_should_pass"));
var handler = new HttpClientHandler { CookieContainer = cookieContainer };
await new HttpClient(handler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(handler);
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
var mapping = server.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid);
Check.That(mapping).IsNotNull();
mapping.Should().NotBeNull();
var matchers = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers<RequestMessageCookieMatcher>().Select(m => m.Name).ToList();
Check.That(matchers).Not.Contains("ASP.NET_SessionId");
Check.That(matchers).Not.Contains("AsP.NeT_SessIonID");
Check.That(matchers).Contains("GoodCookie");
matchers.Should().NotContain("ASP.NET_SessionId");
matchers.Should().NotContain("AsP.NeT_SessIonID");
matchers.Should().Contain("GoodCookie");
}
[Fact]
@@ -547,7 +558,7 @@ public class WireMockServerProxyTests
Url = serverForProxyForwarding.Urls[0],
SaveMapping = true,
SaveMappingToFile = false,
ExcludedParams = new[] { "timestamp" }
ExcludedParams = ["timestamp"]
}
};
var server = WireMockServer.Start(settings);
@@ -562,14 +573,15 @@ public class WireMockServerProxyTests
RequestUri = new Uri($"{server.Urls[0]}{path}{param01}{param02}"),
Content = new StringContent("stringContent"),
};
await new HttpClient().SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient();
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
var mapping = server.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid);
Check.That(mapping).IsNotNull();
mapping.Should().NotBeNull();
var matchers = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers<RequestMessageParamMatcher>().Select(m => m.Key).ToList();
Check.That(matchers).Not.Contains("timestamp");
Check.That(matchers).Contains("name");
matchers.Should().NotContain("timestamp");
matchers.Should().Contain("name");
}
[Fact]
@@ -609,13 +621,13 @@ public class WireMockServerProxyTests
};
var handler = new HttpClientHandler();
await new HttpClient(handler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(handler);
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
var mapping = serverForProxyForwarding.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid);
var score = mapping.RequestMatcher.GetMatchingScore(serverForProxyForwarding.LogEntries.First().RequestMessage,
new RequestMatchResult());
Check.That(score).IsEqualTo(1.0);
var score = mapping!.RequestMatcher.GetMatchingScore(serverForProxyForwarding.LogEntries.First().RequestMessage!, new RequestMatchResult());
score.Should().Be(1.0);
}
[Fact]
@@ -641,19 +653,21 @@ public class WireMockServerProxyTests
Content = new StringContent("")
};
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
await new HttpClient().SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient();
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage;
Check.That(receivedRequest.BodyData.BodyAsString).IsEqualTo("");
Check.That(receivedRequest.Headers).ContainsKey("Content-Type");
Check.That(receivedRequest.Headers["Content-Type"].First()).Contains("text/plain");
receivedRequest.BodyData.BodyAsString.Should().Be("");
receivedRequest.Headers.Should().ContainKey("Content-Type");
receivedRequest.Headers["Content-Type"].First().Should().Contain("text/plain");
}
[Fact]
public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_response()
{
// Assign
var cancellationToken = TestContext.Current.CancellationToken;
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
@@ -673,12 +687,14 @@ public class WireMockServerProxyTests
Method = HttpMethod.Get,
RequestUri = new Uri($"{server.Urls[0]}{path}")
};
var response = await new HttpClient().SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient();
var response = await httpClient.SendAsync(requestMessage, cancellationToken);
// Assert
Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).IsEqualTo("body");
Check.That(response.Content.Headers.Contains("Content-Type")).IsTrue();
Check.That(response.Content.Headers.GetValues("Content-Type")).ContainsExactly("text/plain");
string content = await response.Content.ReadAsStringAsync(cancellationToken);
content.Should().Be("body");
response.Content.Headers.Contains("Content-Type").Should().BeTrue();
response.Content.Headers.GetValues("Content-Type").Should().Equal(new[] { "text/plain" });
}
[Fact]
@@ -707,11 +723,12 @@ public class WireMockServerProxyTests
RequestUri = new Uri($"{server.Urls[0]}{path}")
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
var response = await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(httpClientHandler);
var response = await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// Assert
Check.That(response.Headers.Contains("Location")).IsTrue();
Check.That(response.Headers.GetValues("Location")).ContainsExactly("/testpath");
response.Headers.Contains("Location").Should().BeTrue();
response.Headers.GetValues("Location").Should().Equal(new[] { "/testpath" });
}
[Fact]
@@ -738,12 +755,13 @@ public class WireMockServerProxyTests
};
var clientHandler = new HttpClientHandler();
clientHandler.CookieContainer.Add(requestUri, new Cookie("name", "value"));
await new HttpClient(clientHandler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(clientHandler);
await httpClient.SendAsync(requestMessage, TestContext.Current.CancellationToken);
// then
var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage;
Check.That(receivedRequest.Cookies).IsNotNull();
Check.That(receivedRequest.Cookies).ContainsPair("name", "value");
receivedRequest.Cookies.Should().NotBeNull();
receivedRequest.Cookies.Should().ContainKey("name").And.ContainValue("value");
}
/// <summary>
@@ -756,8 +774,7 @@ public class WireMockServerProxyTests
{
// arrange
var jpegHeader = new byte[] { 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00 };
var brokenJpegHeader = new byte[]
{0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00};
var brokenJpegHeader = new byte[] { 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00 };
bool HasCorrectHeader(byte[]? bytes) => bytes?.SequenceEqual(jpegHeader) == true;
bool HasBrokenHeader(byte[]? bytes) => bytes?.SequenceEqual(brokenJpegHeader) == true;
@@ -777,16 +794,18 @@ public class WireMockServerProxyTests
.RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0]));
// act
var response = await new HttpClient().PostAsync(server.Urls[0], new ByteArrayContent(jpegHeader)).ConfigureAwait(false);
using var httpClient = new HttpClient();
var response = await httpClient.PostAsync(server.Urls[0], new ByteArrayContent(jpegHeader), TestContext.Current.CancellationToken);
// assert
Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
[Fact]
public async Task WireMockServer_Proxy_Should_set_BodyAsJson_in_proxied_response()
{
// Assign
var cancellationToken = TestContext.Current.CancellationToken;
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
@@ -806,12 +825,13 @@ public class WireMockServerProxyTests
Method = HttpMethod.Get,
RequestUri = new Uri($"{server.Urls[0]}{path}")
};
var response = await new HttpClient().SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient();
var response = await httpClient.SendAsync(requestMessage, cancellationToken);
// Assert
string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Check.That(content).IsEqualTo("{\"i\":42}");
Check.That(response.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/json; charset=utf-8");
string content = await response.Content.ReadAsStringAsync(cancellationToken);
content.Should().Be("{\"i\":42}");
response.Content.Headers.GetValues("Content-Type").Should().Equal(new[] { "application/json; charset=utf-8" });
}
[Fact]
@@ -837,11 +857,12 @@ public class WireMockServerProxyTests
{
{ new StringContent("data"), "test", "test.txt" }
};
var response = await new HttpClient().PostAsync(uri, form).ConfigureAwait(false);
using var httpClient = new HttpClient();
var response = await httpClient.PostAsync(uri, form, _ct);
// Assert
string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Check.That(content).IsEqualTo("{\"i\":42}");
string content = await response.Content.ReadAsStringAsync(_ct);
content.Should().Be("{\"i\":42}");
}
[Fact]
@@ -872,11 +893,11 @@ public class WireMockServerProxyTests
Method = HttpMethod.Get,
RequestUri = new Uri($"{server.Urls[0]}{path}")
};
var response1 = await new HttpClient().SendAsync(requestMessage1).ConfigureAwait(false);
var response1 = await new HttpClient().SendAsync(requestMessage1, _ct);
// Assert 1
string content1 = await response1.Content.ReadAsStringAsync().ConfigureAwait(false);
Check.That(content1).IsEqualTo("ok");
string content1 = await response1.Content.ReadAsStringAsync(_ct);
content1.Should().Be("ok");
// Act 2
var requestMessage2 = new HttpRequestMessage
@@ -884,11 +905,11 @@ public class WireMockServerProxyTests
Method = HttpMethod.Get,
RequestUri = new Uri($"{server.Urls[0]}/__admin/mappings")
};
var response2 = await new HttpClient().SendAsync(requestMessage2).ConfigureAwait(false);
var response2 = await new HttpClient().SendAsync(requestMessage2, _ct);
// Assert 2
string content2 = await response2.Content.ReadAsStringAsync().ConfigureAwait(false);
Check.That(content2).IsEqualTo("[]");
string content2 = await response2.Content.ReadAsStringAsync(_ct);
content2.Should().Be("[]");
}
// On Ubuntu latest it's : "Resource temporarily unavailable"
@@ -913,15 +934,18 @@ public class WireMockServerProxyTests
RequestUri = new Uri(server.Urls[0])
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
var result = await new HttpClient(httpClientHandler).SendAsync(requestMessage).ConfigureAwait(false);
using var httpClient = new HttpClient(httpClientHandler);
var result = await httpClient.SendAsync(requestMessage, _ct);
// Assert
result.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
var content = await result.Content.ReadAsStringAsync(_ct);
content.Should().NotBeEmpty();
server.LogEntries.Should().HaveCount(1);
server.Stop();
}
}
}