将 JSON 字符串加载到 HttpRequestMessage

Posted

技术标签:

【中文标题】将 JSON 字符串加载到 HttpRequestMessage【英文标题】:Load JSON string to HttpRequestMessage 【发布时间】:2019-10-08 10:15:28 【问题描述】:

我正在为我的 WebAPI Web 服务编写一些测试,但无法弄清楚如何在测试中将 JSON 发送到我的服务方法。

ScheduleRequest sr = new ScheduleRequest();
sr.Months = null;
sr.States = null;
sr.Zip = null;
sr.Miles = null;
sr.PCodes = null;
sr.PageStart = 1;
sr.PageLimit = 10;

HttpRequestMessage m = new HttpRequestMessage();
string sr_ = JsonConvert.SerializeObject(sr);

// How do I load it into the HttpRequestMessage???
// m.Content. = sr_;
var controller = new ShoppingCartController();

// Call the controlelr method and test if the return data is correct.
EventSyncResponse res = (EventSyncResponse)controller.CourseSchedule(m);

我这样做对吗?

控制器代码:

public object CourseSchedule(ScheduleRequest request)

    try
    
        var result = cart.GetCourseSchedule(request);
        return Ok(result);
     
    catch (Exception ex)
    
        if (ex.Message.StartsWith(@"ORA-20001"))
        
            return Ok(new ParticipantResponse  FirstName = "No record found" );
        
        throw ex;
    

【问题讨论】:

大部分其他方法都可以通过GET进行测试,不需要JSON数据作为参数。 【参考方案1】:
[TestClass]
public class ShoppingCartControllerTests 
    [TestMethod]
    public void TestCourseSchedule() 
        //Arrange
        var sr = new ScheduleRequest();
        sr.Months = null;
        sr.States = null;
        sr.Zip = null;
        sr.Miles = null;
        sr.PCodes = null;
        sr.PageStart = 1;
        sr.PageLimit = 10;

        var json = JsonConvert.SerializeObject(sr);
        //construct content to send
        var content = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");
        var request = new HttpRequestMessage 
            RequestUri = new Uri("http://localhost/api/shoppingcart"),
            Content = content
        ;

        var controller = new ShoppingCartController();
        //Set a fake request. If your controller creates responses you will need this
        controller.Request = request;
        //Act
        // Call the controller method and test if the return data is correct.
        var response = controller.CourseSchedule(request) as OkNegotiatedContentResult<List<EventSyn‌​cResponse>> ;

        //Assert
        //...other asserts
    

但我的印象是你的 Action 实际上应该在你的控制器中像这样重构

public class ShoppingCartController : ApiController 

    public IHttpActionResult CourseSchedule(ScheduleRequest model)  ... 


这意味着你的独立单元测试应该被重构为...

[TestClass]
public class ShoppingCartControllerTests 
    [TestMethod]
    public void TestCourseSchedule() 
        //Arrange
        var sr = new ScheduleRequest();
        sr.Months = null;
        sr.States = null;
        sr.Zip = null;
        sr.Miles = null;
        sr.PCodes = null;
        sr.PageStart = 1;
        sr.PageLimit = 10;

       var controller = new ShoppingCartController();
        //Set a fake request. If your controller creates responses you will need this
        controller.Request = new HttpRequestMessage 
            RequestUri = new Uri("http://localhost/api/shoppingcart"),
        ;
        //Act
        // Call the controller method and test if the return data is correct.
        var response = controller.CourseSchedule(sr) as OkNegotiatedContentResult<List<EventSyn‌​cResponse>> ;;

        //Assert
        //...
    

【讨论】:

【参考方案2】:

MB34。 您还需要在方法中添加一个 ScheduleRequest 参数。 检查此链接: http://www.lybecker.com/blog/2013/06/26/accessing-http-request-from-asp-net-web-api/

【讨论】:

以上是关于将 JSON 字符串加载到 HttpRequestMessage的主要内容,如果未能解决你的问题,请参考以下文章

将 JSON 加载到 BigQuery:字段有时是数组,有时是字符串

将 JSON 加载到 bigquery - 字段有时是数组,有时是字符串

是否有用于异步 JSON 解析器的 Node 模块,它不会将整个 JSON 字符串加载到内存中?

将 JSON 加载到 Spark 数据框

12-Django-基础篇-HttpRequest对象

JSON 使用