IntegrationTesting MultimediaPartReader / XDocument
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
When trying to do integration testing with a MTOM Related Message, I keep running into an issue where application code that I can run successfully in a webserver does not work in the integration testing framework.
The issue is when using the MultipartReader, we read a section that matches a media type. We then take that MultipartSection.Body Stream and use the XDocument.Load( stream).
Again, in an actual server this code works just fine. When attempting to run an integration test though, the code no longer works and I get an XDocument Exception saying that there is no root element.
Here is an abstraction of the code in the controller that is giving me issue in an integration test
```
var boundary = Request.GetMultipartBoundary();
var reader = new MultipartReader(boundary, Request.Body);
bool done = false;
while (!done)
{
var section = await reader.ReadNextSectionAsync();
if (section != null)
{
foreach (var header in section.Headers)
{
Console.WriteLine($"{header.Key}: {header.Value}");
}
XDocument.Load(section.Body);
// var body = await section.ReadAsStringAsync();
// Console.WriteLine(body);
}
else
{
done = true;
}
}
return "Hello World";
```
With the Xunit Test Method
```
var request = new HttpRequestMessage();
request.Headers.Add("MIME-Version", "1.0");
var outboundPayload = new MultipartContent("related", "uuid:3f4da383-2009-4fbd-a202-2a678f4ec28f+id=9");
outboundPayload.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("type", "\"application/soap+xml\""));
outboundPayload.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("start", "\"\""));
var soapXml = @"
test
urn:uuid:00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000
http://www.w3.org/2005/08/addressing/anonymous
http://test
";
///Ensuring the XML can be loaded
XDocument.Parse(soapXml);
var payload = new ByteArrayContent(Encoding.UTF8.GetBytes(soapXml));
payload.Headers.Add("Content-ID", "");
payload.Headers.Add("Content-Transfer-Encoding", "8bit");
payload.Headers.Add("Content-Type", "application/soap+xml");
outboundPayload.Add(payload);
request.Content = outboundPayload;
request.RequestUri = new Uri($"/test", UriKind.Relative);
request.Method = HttpMethod.Post;
var client = _factory.CreateClient();
var response = await client.SendAsync(request);
```
Using a WebApplicationFactory
```
public class XUnitWebApplicationFactory : WebApplicationFactory where TStartup : class
{
protected override IWebHostBuilder CreateWebHostBuilder() => WebHost.CreateDefaultBuilder().UseStartup();
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseContentRoot(".");
base.ConfigureWebHost(builder);
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.