首页 新闻 会员 周边 捐助

如何在“.NET-Core”中测试处理 HttpPatch 请求?

0
悬赏园豆:20 [已解决问题] 解决于 2017-04-10 19:00

需求如下:



 有一个WebApi 是 HttpPatch 的源码如下:

[HttpPatch]
        [Route("test/{testId}")]
        public async Task<IActionResult> UpdateA([FromBody]TestItem test, int testId)
        {
              //.....
         }


测试代码如下:


 [Fact]
        public async Task UpdateATest()
        {
            var requestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), $"test/{testId}")
            {
                Content = new ObjectContent<dynamic>(new
                {
                    UserID = 1,
                    Title = "Second Queston From Web Api",
                    Content = "Second Question from Web Api"
                }, "application/json")
            };
            var response = await _httpClient.SendAsync(requestMessage);
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }

疑问



  在.NET-CoreObjectContent 是不支持的,请问,我需要如何进行测试 HttpPatch的请求呢?

BUTTERAPPLE的主页 BUTTERAPPLE | 老鸟四级 | 园豆:3190
提问于:2017-04-05 13:59
< >
分享
最佳答案
0

1、可以使用StringContent替代

1.1、具体代码如下:

 var requestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), $"questions/{Qid}")
            {
                Content = new StringContent(JsonConvert.SerializeObject(new
                {
                    UserID = 2,
                    Title = "Second Queston From Web Api",
                    Content = "Second Question from Web Api"
                }),Encoding.UTF8,"Application/json")
            };
            var response = await _httpClient.SendAsync(requestMessage);
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

1.2、下面是一个StringContentObjectContent 的比较:



http://stackoverflow.com/questions/30704584/stringcontent-vs-objectcontent

BUTTERAPPLE | 老鸟四级 |园豆:3190 | 2017-04-05 16:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册