单元测试时HttpRequestMessage内容处置为空 [英] HttpRequestMessage Content Disposition null when unit testing

查看:30
本文介绍了单元测试时HttpRequestMessage内容处置为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为自定义 MultipartMemoryStreamProvider 编写测试 - 与此非常相似

知道我错过了什么吗?

为了代码,这里是代码的副本.您会注意到它与另一个问题的答案中的相同.我就是无法通过空内容处理.

 var content = new ByteArrayContent(new Byte[100]);content.Headers.Add("Content-Disposition", "form-data");var controllerContext = 新的 HttpControllerContext{请求 = 新的 HttpRequestMessage{内容 = 新多部分内容 { 内容 }}};var controller = new MockController();controller.ControllerContext = controllerContext;

MockController 很简单:

public class MockController : ApiController { }

解决方案

如果您希望 Content-Disposition 标题出现在 MultipartContent 中,您应该在 上设置MultipartContent 不在 ByteArrayContent 上.

但如果你想模拟从网页上传文件,你需要在 ByteArrayContent 上设置 Content-Disposition 标头.在这种情况下,要查看 Content-Disposition 的值,您需要遍历 MultipartContent 的内部内容:

var multipart = await Request.Content.ReadAsMultipartAsync();foreach (var content in result.Contents){var contentDisposition = content.Headers.ContentDisposition;}

您可以查看本教程:在 ASP.NET Web API 中发送 HTML 表单数据.它可能有点过时,但它显示了想法.

刚刚检查过,它似乎与您在问题中引用的答案中提到的教程相同.

I am trying to write tests for a custom MultipartMemoryStreamProvider - one that is very similar to this MultipartFormDataMemoryStreamProvider.cs

In particular, I am trying to test my own implementation of the GetStream(HttpContent parent, HttpContentHeaders headers) method.

It requires an HttpContent and HttpContentHeaders.

To achieve this I am trying to create a controller context and controller, then pass through the appropriate properties from that controllers request.

In fact, I have tried to implement the answer on this (duplicate) question: Testing a Web API method that uses HttpContext.Current.Request.Files?

Everything I try results in the Content-Disposition on the headers being null As shown in the images below:

Any idea what I am missing?

For code-sake, here is a copy of the code. You will notice it's the same as that in the answer on the other question. I just can't get passed the null content-disposition.

  var content = new ByteArrayContent(new Byte[100]);
        content.Headers.Add("Content-Disposition", "form-data");
        var controllerContext = new HttpControllerContext
        {
            Request = new HttpRequestMessage
            {
                Content = new MultipartContent { content }
            }
        };
        var controller = new MockController();
        controller.ControllerContext = controllerContext;

MockController is simply:

public class MockController : ApiController { }

解决方案

If you expect Content-Disposition header to appear in MultipartContent you should set in on MultipartContent not on ByteArrayContent.

But if you want to emulate file upload form a web page you do need to set Content-Disposition header on ByteArrayContent. In that case to see the value of Content-Disposition you need to iterate through inner contents of MultipartContent:

var multipart = await Request.Content.ReadAsMultipartAsync();
foreach (var content in result.Contents)
{
     var contentDisposition = content.Headers.ContentDisposition;
}

You can check this tutorial: Sending HTML Form Data in ASP.NET Web API. It might be a bit outdated but it shows the idea.

EDIT: Just checked and it appears to be same tutorial mentioned in the answer you referenced in your question.

这篇关于单元测试时HttpRequestMessage内容处置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆