http - 从 IHttpActionResult 返回值获取 HTTP 状态

标签 http get status

在 C# 中,在这种情况下,我到底如何获取 HTTP 状态号 (200) 并返回文本(“ok”)以进行 API Controller 的单元测试?我想我已经尝试了一切。当然,这是一个普遍的需求。谢谢。

public IHttpActionResult Get(string appServer, string action)
{
   return Ok("ok");
}

[TestMethod]
public void AppPoolTestStart()
{
   AppPoolController controller = new AppPoolController();
   IHttpActionResult result = controller.Get("MyServer", "start");
   Assert.??? ; // check for 200 or "ok" 
}

最佳答案

Action Results in Web API 2 :

A Web API controller action can return any of the following:

  1. void
  2. HttpResponseMessage
  3. IHttpActionResult
  4. Some other type

Depending on which of these is returned, Web API uses a different mechanism to create the HTTP response.

void
Return empty 204 (No Content)

HttpResponseMessage
Convert directly to an HTTP response message.

IHttpActionResult
Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message.

Other type Write the serialized return value into the response body; return 200 (OK).

...

IHttpActionResult

The IHttpActionResult interface was introduced in Web API 2. Essentially, it defines an HttpResponseMessage factory. Here are some advantages of using the IHttpActionResult interface:

  • Simplifies unit testing your controllers.
  • Moves common logic for creating HTTP responses into separate classes.
  • Makes the intent of the controller action clearer, by hiding the low-level details of constructing the response.

IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance.

public interface IHttpActionResult
{
    Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken);
}

If a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync method to create an HttpResponseMessage. Then it converts the HttpResponseMessage into an HTTP response message.

因此,在您的情况下,您可以调用 result.ExecuteAsync() 并等待生成的 Task 完成,然后获取其 HttpResponseMessage > 对象。

但是,有一种不同的方法可以解决此问题,以下文档对此进行了更详细的介绍:

Unit Testing Controllers in ASP.NET Web API 2

特别是,请参阅 "Testing Actions that Return IHttpActionResult" 部分:

In Web API 2, a controller action can return IHttpActionResult, which is analogous to ActionResult in ASP.NET MVC. The IHttpActionResult interface defines a command pattern for creating HTTP responses. Instead of creating the response directly, the controller returns an IHttpActionResult. Later, the pipeline invokes the IHttpActionResult to create the response. This approach makes it easier to write unit tests, because you can skip a lot of the setup that is needed for HttpResponseMessage.

在给出的示例中,他们只是将 IHttpActionResult 类型转换为他们正在测试的所需响应类型。例如:

// Act
IHttpActionResult actionResult = controller.Get(42);
var contentResult = actionResult as OkNegotiatedContentResult<Product>;

// Assert
Assert.IsNotNull(contentResult);
Assert.IsNotNull(contentResult.Content);
Assert.AreEqual(42, contentResult.Content.Id);

// Act
IHttpActionResult actionResult = controller.Get(10);

// Assert
Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult));

// Act
IHttpActionResult actionResult = controller.Delete(10);

// Assert
Assert.IsInstanceOfType(actionResult, typeof(OkResult));

等等。

关于http - 从 IHttpActionResult 返回值获取 HTTP 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316798/

相关文章:

angular - 将项目从 Angular 5 迁移到 8 问题与 Response -> HttpResponse

Perl 套接字作为(伪)http 服务器与 ftp 请求中断!

http - 更好的错误处理

c++ - HTTP/S 代理起点

Django - 可以在一个 HttpRequest 中返回 POST 和 GET 请求吗?

mysql - 如何使用mysql group by query

ios - 如何通过iOS应用程序在Facebook上分享应用程序信息?

angularjs - ngRepeat 暂时显示重复列表

c# - 封装C#新手

Javascript:对象不支持 onclick ="status()"的此操作错误