mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-18 15:10:17 +02:00
Add example for IRequestMessage.BodyAsMimeMessage
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
using MimeKit;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
@@ -18,17 +20,90 @@ internal class Program
|
|||||||
Logger = new WireMockConsoleLogger(),
|
Logger = new WireMockConsoleLogger(),
|
||||||
});
|
});
|
||||||
|
|
||||||
server.Given(Request.Create().UsingPost().WithPath("/some/endpoint"))
|
server
|
||||||
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.Created));
|
.Given(Request.Create()
|
||||||
|
.UsingPost()
|
||||||
|
.WithPath("/test")
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody(requestMessage => requestMessage.BodyAsMimeMessage != null ?
|
||||||
|
"BodyAsMimeMessage is present" :
|
||||||
|
"BodyAsMimeMessage is not present")
|
||||||
|
);
|
||||||
|
|
||||||
var httpClient = new HttpClient { BaseAddress = new Uri(server.Url!) };
|
server
|
||||||
var requestUri = new Uri(httpClient.BaseAddress!, "some/endpoint");
|
.Given(Request.Create()
|
||||||
var content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
|
.UsingPost()
|
||||||
|
.WithPath("/some/endpoint")
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithStatusCode(HttpStatusCode.Created)
|
||||||
|
);
|
||||||
|
|
||||||
|
var httpClient = server.CreateClient();
|
||||||
|
var content = new StringContent("abc", Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
await TestAsync(httpClient, content);
|
||||||
|
|
||||||
|
await TestNoMultiPartAsync(httpClient, content);
|
||||||
|
|
||||||
|
await TestMultiPartAsync(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task TestNoMultiPartAsync(HttpClient httpClient, StringContent content)
|
||||||
|
{
|
||||||
|
var response = await httpClient.PostAsync("/test", content);
|
||||||
|
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
(await response.Content.ReadAsStringAsync()).Should().Be("BodyAsMimeMessage is not present");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task TestAsync(HttpClient httpClient, StringContent content)
|
||||||
|
{
|
||||||
|
var response = await httpClient.PostAsync("some/endpoint", content);
|
||||||
|
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.Created);
|
||||||
|
(await response.Content.ReadAsStringAsync()).Should().BeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task TestMultiPartAsync(WireMockServer server)
|
||||||
|
{
|
||||||
|
var textPlainContent = "This is some plain text";
|
||||||
|
var textPlainContentType = "text/plain";
|
||||||
|
|
||||||
|
var textJson = "{ \"Key\" : \"Value\" }";
|
||||||
|
var textJsonContentType = "text/json";
|
||||||
|
|
||||||
|
var imagePngBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC");
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(
|
||||||
|
Request.Create()
|
||||||
|
.UsingPost()
|
||||||
|
.WithPath("/multipart")
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody(requestMessage => requestMessage.BodyAsMimeMessage is MimeMessage mm ?
|
||||||
|
"BodyAsMimeMessage is present: " + ((MimePart)mm.BodyParts.Last()).FileName :
|
||||||
|
"BodyAsMimeMessage is not present")
|
||||||
|
);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var actual = await httpClient.PostAsync(requestUri, content);
|
var formDataContent = new MultipartFormDataContent
|
||||||
|
{
|
||||||
|
{ new StringContent(textPlainContent, Encoding.UTF8, textPlainContentType), "text" },
|
||||||
|
{ new StringContent(textJson, Encoding.UTF8, textJsonContentType), "json" }
|
||||||
|
};
|
||||||
|
|
||||||
// Assert
|
var fileContent = new ByteArrayContent(imagePngBytes);
|
||||||
actual.StatusCode.Should().Be(HttpStatusCode.Created);
|
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
|
||||||
|
formDataContent.Add(fileContent, "somefile", "image.png");
|
||||||
|
|
||||||
|
var client = server.CreateClient();
|
||||||
|
|
||||||
|
var response = await client.PostAsync("/multipart", formDataContent);
|
||||||
|
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
(await response.Content.ReadAsStringAsync()).Should().Be("BodyAsMimeMessage is present: image.png");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,8 +8,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentAssertions" Version="6.11.0" />
|
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||||
<PackageReference Include="WireMock.Net" Version="1.5.42" />
|
<PackageReference Include="WireMock.Net" Version="1.5.51" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user