mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 09:18:27 +02:00
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:
@@ -1,20 +1,17 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using JsonConverter.Newtonsoft.Json;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NFluent;
|
||||
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Models;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.ResponseBuilders;
|
||||
|
||||
@@ -47,15 +44,15 @@ public class ResponseWithBodyTests
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
var responseBuilder = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.String, Encoding.ASCII);
|
||||
var responseBuilder = Response.Create().WithBody([48, 49], BodyDestinationFormat.String, Encoding.ASCII);
|
||||
|
||||
// act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// then
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("01");
|
||||
Check.That(response.Message.BodyData.BodyAsBytes).IsNull();
|
||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||
response.Message.BodyData.BodyAsString.Should().Be("01");
|
||||
response.Message.BodyData.BodyAsBytes.Should().BeNull();
|
||||
response.Message.BodyData.Encoding.Should().Be(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -69,15 +66,15 @@ public class ResponseWithBodyTests
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||
|
||||
var responseBuilder = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.SameAsSource, Encoding.ASCII);
|
||||
var responseBuilder = Response.Create().WithBody([48, 49], BodyDestinationFormat.SameAsSource, Encoding.ASCII);
|
||||
|
||||
// act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// then
|
||||
Check.That(response.Message.BodyData.BodyAsBytes).ContainsExactly(new byte[] { 48, 49 });
|
||||
Check.That(response.Message.BodyData.BodyAsString).IsNull();
|
||||
Check.That(response.Message.BodyData.Encoding).IsNull();
|
||||
response.Message.BodyData.BodyAsBytes.Should().ContainInOrder([48, 49]);
|
||||
response.Message.BodyData.BodyAsString.Should().BeNull();
|
||||
response.Message.BodyData.Encoding.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -94,11 +91,11 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBody("test", null, Encoding.ASCII);
|
||||
|
||||
// act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// then
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("test");
|
||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||
response.Message.BodyData.BodyAsString.Should().Be("test");
|
||||
response.Message.BodyData.Encoding.Should().Be(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -116,11 +113,11 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBodyAsJson(x, Encoding.ASCII);
|
||||
|
||||
// act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// then
|
||||
Check.That(response.Message.BodyData.BodyAsJson).Equals(x);
|
||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||
response.Message.BodyData.BodyAsJson.Should().Be(x);
|
||||
response.Message.BodyData.Encoding.Should().Be(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -132,13 +129,13 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBody("r", BodyDestinationFormat.SameAsSource, Encoding.ASCII);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// Assert
|
||||
Check.That(response.Message.BodyData.BodyAsBytes).IsNull();
|
||||
Check.That(response.Message.BodyData.BodyAsJson).IsNull();
|
||||
Check.That(response.Message.BodyData.BodyAsString).Equals("r");
|
||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||
response.Message.BodyData.BodyAsBytes.Should().BeNull();
|
||||
response.Message.BodyData.BodyAsJson.Should().BeNull();
|
||||
response.Message.BodyData.BodyAsString.Should().Be("r");
|
||||
response.Message.BodyData.Encoding.Should().Be(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -150,13 +147,13 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBody("r", BodyDestinationFormat.Bytes, Encoding.ASCII);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// Assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).IsNull();
|
||||
Check.That(response.Message.BodyData.BodyAsJson).IsNull();
|
||||
Check.That(response.Message.BodyData.BodyAsBytes).IsNotNull();
|
||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||
response.Message.BodyData.BodyAsString.Should().BeNull();
|
||||
response.Message.BodyData.BodyAsJson.Should().BeNull();
|
||||
response.Message.BodyData.BodyAsBytes.Should().NotBeNull();
|
||||
response.Message.BodyData.Encoding.Should().Be(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -168,13 +165,13 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBody("{ \"value\": 42 }", BodyDestinationFormat.Json, Encoding.ASCII);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// Assert
|
||||
Check.That(response.Message.BodyData.BodyAsString).IsNull();
|
||||
Check.That(response.Message.BodyData.BodyAsBytes).IsNull();
|
||||
Check.That(((dynamic)response.Message.BodyData.BodyAsJson).value).Equals(42);
|
||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||
response.Message.BodyData!.BodyAsString.Should().BeNull();
|
||||
response.Message.BodyData.BodyAsBytes.Should().BeNull();
|
||||
((int)((JObject)response.Message.BodyData.BodyAsJson)["value"]!).Should().Be(42);
|
||||
response.Message.BodyData.Encoding.Should().Be(Encoding.ASCII);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -192,11 +189,11 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBodyAsJson(x, true);
|
||||
|
||||
// act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// then
|
||||
Check.That(response.Message.BodyData.BodyAsJson).Equals(x);
|
||||
Check.That(response.Message.BodyData.BodyAsJsonIndented).IsEqualTo(true);
|
||||
response.Message.BodyData.BodyAsJson.Should().Be(x);
|
||||
response.Message.BodyData.BodyAsJsonIndented.Should().Be(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -214,7 +211,7 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBodyAsJson(requestMessage => responseBody);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsJson.Should().BeEquivalentTo(responseBody);
|
||||
@@ -235,7 +232,7 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBodyAsJson(requestMessage => Task.FromResult(responseBody));
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsJson.Should().BeEquivalentTo(responseBody);
|
||||
@@ -257,19 +254,19 @@ public class ResponseWithBodyTests
|
||||
.WithTransformer();
|
||||
|
||||
// Act
|
||||
var response1 = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request1, _settings).ConfigureAwait(false);
|
||||
var response2 = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request2, _settings).ConfigureAwait(false);
|
||||
var response1 = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request1, _settings);
|
||||
var response2 = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request2, _settings);
|
||||
|
||||
// Assert
|
||||
Check.That(((JToken)response1.Message.BodyData.BodyAsJson).SelectToken("id")?.Value<int>()).IsEqualTo(request1Id);
|
||||
Check.That(response1.Message.BodyData.BodyAsBytes).IsNull();
|
||||
Check.That(response1.Message.BodyData.BodyAsString).IsNull();
|
||||
Check.That(response1.Message.StatusCode).IsEqualTo(200);
|
||||
((JToken)response1.Message.BodyData.BodyAsJson).SelectToken("id")?.Value<int>().Should().Be(request1Id);
|
||||
response1.Message.BodyData.BodyAsBytes.Should().BeNull();
|
||||
response1.Message.BodyData.BodyAsString.Should().BeNull();
|
||||
response1.Message.StatusCode.Should().Be(200);
|
||||
|
||||
Check.That(((JToken)response2.Message.BodyData.BodyAsJson).SelectToken("id")?.Value<int>()).IsEqualTo(request2Id);
|
||||
Check.That(response2.Message.BodyData.BodyAsBytes).IsNull();
|
||||
Check.That(response2.Message.BodyData.BodyAsString).IsNull();
|
||||
Check.That(response2.Message.StatusCode).IsEqualTo(200);
|
||||
((JToken)response2.Message.BodyData.BodyAsJson).SelectToken("id")?.Value<int>().Should().Be(request2Id);
|
||||
response2.Message.BodyData.BodyAsBytes.Should().BeNull();
|
||||
response2.Message.BodyData.BodyAsString.Should().BeNull();
|
||||
response2.Message.StatusCode.Should().Be(200);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -282,10 +279,10 @@ public class ResponseWithBodyTests
|
||||
|
||||
var responseBuilder = Response.Create().WithStatusCode(200).WithBody(fileContents);
|
||||
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request1, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request1, _settings);
|
||||
|
||||
Check.That(response.Message.StatusCode).IsEqualTo(200);
|
||||
Check.That(response.Message.BodyData.BodyAsString).Contains(fileContents);
|
||||
response.Message.StatusCode.Should().Be(200);
|
||||
response.Message.BodyData.BodyAsString.Should().Contain(fileContents);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -298,10 +295,10 @@ public class ResponseWithBodyTests
|
||||
|
||||
var responseBuilder = Response.Create().WithStatusCode(200).WithBody(fileContents);
|
||||
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request1, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request1, _settings);
|
||||
|
||||
Check.That(response.Message.StatusCode).IsEqualTo(200);
|
||||
Check.That(response.Message.BodyData.BodyAsString).Contains(fileContents);
|
||||
response.Message.StatusCode.Should().Be(200);
|
||||
response.Message.BodyData.BodyAsString.Should().Contain(fileContents);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -314,10 +311,10 @@ public class ResponseWithBodyTests
|
||||
|
||||
var responseBuilder = Response.Create().WithStatusCode(200).WithBody("File deleted.");
|
||||
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request1, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request1, _settings);
|
||||
|
||||
Check.That(response.Message.StatusCode).IsEqualTo(200);
|
||||
Check.That(response.Message.BodyData?.BodyAsString).Contains("File deleted.");
|
||||
response.Message.StatusCode.Should().Be(200);
|
||||
response.Message.BodyData?.BodyAsString.Should().Contain("File deleted.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -334,13 +331,12 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBody(new { foo = "< > & ' 😀 👍 ❤️", n = 42 }, new NewtonsoftJsonConverter());
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsString.Should().Be("""{"foo":"< > & ' 😀 👍 ❤️","n":42}""");
|
||||
}
|
||||
|
||||
#if !(NET451 || NET452 || NET461)
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_WithBody_SystemTextJsonConverter()
|
||||
{
|
||||
@@ -355,10 +351,9 @@ public class ResponseWithBodyTests
|
||||
var responseBuilder = Response.Create().WithBody(new { foo = "< > & ' 😀 👍 ❤️", n = 42 }, new JsonConverter.System.Text.Json.SystemTextJsonConverter());
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsString.Should().Be("""{"foo":"\u003C \u003E \u0026 \u0027 \uD83D\uDE00 \uD83D\uDC4D \u2764\uFE0F","n":42}""");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user