mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-12 21:35:55 +01:00
* 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
105 lines
3.9 KiB
C#
105 lines
3.9 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using AwesomeAssertions;
|
|
using WireMock.Matchers;
|
|
using WireMock.RequestBuilders;
|
|
using WireMock.ResponseBuilders;
|
|
using WireMock.Server;
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
namespace WireMock.Net.Tests;
|
|
|
|
public class WireMockServerTests
|
|
{
|
|
private readonly CancellationToken _ct = TestContext.Current.CancellationToken;
|
|
|
|
[Fact]
|
|
public async Task WireMockServer_WithMultiPartBody_Using_MimePartMatchers()
|
|
{
|
|
// Arrange
|
|
using var server = WireMockServer.Start();
|
|
|
|
var textPlainContent = "This is some plain text";
|
|
var textPlainContentType = "text/plain";
|
|
var textPlainContentTypeMatcher = new ContentTypeMatcher(textPlainContentType);
|
|
var textPlainContentMatcher = new ExactMatcher(textPlainContent);
|
|
var textPlainMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textPlainContentTypeMatcher, null, null, textPlainContentMatcher);
|
|
|
|
var textJson = "{ \"Key\" : \"Value\" }";
|
|
var textJsonContentType = "applicatiom/json";
|
|
var textJsonContentTypeMatcher = new ContentTypeMatcher(textJsonContentType);
|
|
var textJsonContentMatcher = new JsonMatcher(new { Key = "Value" }, true);
|
|
var jsonMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textJsonContentTypeMatcher, null, null, textJsonContentMatcher);
|
|
|
|
var imagePngBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC");
|
|
var imagePngContentMatcher = new ExactObjectMatcher(imagePngBytes);
|
|
var imagePngMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, null, null, null, imagePngContentMatcher);
|
|
|
|
var matchers = new IMatcher[]
|
|
{
|
|
textPlainMatcher,
|
|
jsonMatcher,
|
|
imagePngMatcher
|
|
};
|
|
|
|
server
|
|
.Given(Request.Create()
|
|
.UsingPost()
|
|
.WithPath("/multipart")
|
|
.WithMultiPart(matchers)
|
|
)
|
|
.RespondWith(Response.Create()
|
|
.WithBody("{{request.Method}};{{request.BodyAsMimeMessage.TextBody}}")
|
|
.WithTransformer()
|
|
);
|
|
|
|
server
|
|
.Given(Request.Create()
|
|
.UsingPost()
|
|
.WithPath("/multipart2")
|
|
.WithMultiPart(matchers)
|
|
)
|
|
.RespondWith(Response.Create()
|
|
.WithBody(request =>
|
|
{
|
|
if (request.BodyAsMimeMessage == null)
|
|
{
|
|
throw new InvalidProgramException("Not expected");
|
|
}
|
|
return "OK";
|
|
})
|
|
.WithTransformer()
|
|
);
|
|
|
|
var formDataContent = new MultipartFormDataContent
|
|
{
|
|
{ new StringContent(textPlainContent, Encoding.UTF8, textPlainContentType), "text" },
|
|
{ new StringContent(textJson, Encoding.UTF8, textJsonContentType), "json" }
|
|
};
|
|
|
|
var fileContent = new ByteArrayContent(imagePngBytes);
|
|
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
|
|
formDataContent.Add(fileContent, "somefile", "image.png");
|
|
|
|
var client = server.CreateClient();
|
|
|
|
// Act 1
|
|
var response1 = await client.PostAsync("/multipart", formDataContent, _ct);
|
|
|
|
// Assert 1
|
|
response1.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
var content1 = await response1.Content.ReadAsStringAsync(_ct);
|
|
content1.Should().Be("POST;This is some plain text");
|
|
|
|
// Act 2
|
|
var response2 = await client.PostAsync("/multipart2", formDataContent, _ct);
|
|
|
|
// Assert 1
|
|
response2.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
var content2 = await response2.Content.ReadAsStringAsync(_ct);
|
|
content2.Should().Be("OK");
|
|
}
|
|
} |