繁体   English   中英

Restsharp:IRestResponse内容返回“ []”

[英]Restsharp: IRestResponse content returns “[]”

我正在尝试使用restsharp对我的rest api进行测试,这似乎是用于简单测试的非常强大且易于使用的工具。

话虽如此,我仍然无法从IRestResponse获取内容。

代码如下:

...
    [TestMethod]
    public void Testing_Whether_Get_Returns_cellphone()
    {
        // Arrange
        RestClient restClient = new RestClient("http://pseudoUrl/Users?UserId={uid}");       
        //restClient.AddDefaultHeader("headerType1", Guid.NewGuid().ToString());
        //restClient.AddDefaultUrlSegment("uid", "25248896");
        RestRequest restRequest = new RestRequest(Method.GET);
        restRequest.AddQueryParameter("uid", "25248896");
        restRequest.AddHeader("headerType1", Guid.NewGuid().ToString());
        // Act
        IRestResponse restResponse = restClient.Execute(restRequest); // restresponse = "StatusCode: Ok.."
        string response = restResponse.Content; // response = "[]"

        // Assert
        Assert.IsTrue(response.Contains("cellphone"));
    }
...

当测试上述内容时,状态码为200,但内容为“ []”,即使我用摇摇欲坠地测试它时,我也会得到一个很好的json响应,其中充满了所需的内容。

如您所见,我已尝试同时使用AddDefaultUrlSegment和AddQueryParameter。 我猜最后一个是最明显的吗?

大张旗鼓的以下网址是:“ http:// pseudoUrl / Users?UserId = 25248896 ”再次添加了标头:headerType1-效果很好。

有人看到我在做什么错吗?

当前输入的AddQueryParameter会将?uid=25248896添加到URL。

只需将没有查询http://pseudoUrl/Users URL放入RestClient构造函数中,然后在AddQueryParameter中将“ uid”更改为“ UserId”

[TestMethod]
public void Testing_Whether_Get_Returns_cellphone()
{
    // Arrange
    RestClient restClient = new RestClient("http://pseudoUrl/Users");
    RestRequest restRequest = new RestRequest(Method.GET);
    restRequest.AddQueryParameter("UserId", "25248896");
    restRequest.AddHeader("headerType1", Guid.NewGuid().ToString());
    // Act
    IRestResponse restResponse = restClient.Execute(restRequest); 
    string response = restResponse.Content; // response = "[]"

    // Assert
    Assert.IsTrue(response.Contains("cellphone"));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM