mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:14:23 +01:00
Fix TypeLoader (#1320)
* no ilmerge * . * . * nullable * .UsingNuGet * fix * . * directoriesToSearch * .
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<DefineConstants>$(DefineConstants);GRAPHQL;MIMEKIT;PROTOBUF</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AwesomeAssertions" Version="9.0.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="WireMock.Net" Version="1.8.11" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="SonarAnalyzer.CSharp" Version="10.11.0.117924" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,103 @@
|
||||
// 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 partial class WireMockServerTests
|
||||
{
|
||||
[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 = "text/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);
|
||||
|
||||
// Assert 1
|
||||
response1.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var content1 = await response1.Content.ReadAsStringAsync();
|
||||
content1.Should().Be("POST;This is some plain text");
|
||||
|
||||
// Act 2
|
||||
var response2 = await client.PostAsync("/multipart2", formDataContent);
|
||||
|
||||
// Assert 1
|
||||
response2.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var content2 = await response2.Content.ReadAsStringAsync();
|
||||
content2.Should().Be("OK");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using AnyOfTypes;
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
@@ -57,12 +58,22 @@ public class TypeLoaderTests
|
||||
[Fact]
|
||||
public void LoadNewInstance()
|
||||
{
|
||||
// Act
|
||||
AnyOf<string, StringPattern> pattern = "x";
|
||||
var result = TypeLoader.LoadNewInstance<ICSharpCodeMatcher>(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, pattern);
|
||||
var current = Directory.GetCurrentDirectory();
|
||||
try
|
||||
{
|
||||
Directory.SetCurrentDirectory(Path.GetTempPath());
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
// Act
|
||||
AnyOf<string, StringPattern> pattern = "x";
|
||||
var result = TypeLoader.LoadNewInstance<ICSharpCodeMatcher>(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, pattern);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.SetCurrentDirectory(current);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user