本文整理汇总了C#中TestContext.GetRestClient方法的典型用法代码示例。如果您正苦于以下问题:C# TestContext.GetRestClient方法的具体用法?C# TestContext.GetRestClient怎么用?C# TestContext.GetRestClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TestContext
的用法示例。
在下文中一共展示了TestContext.GetRestClient方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RestClientGetRequestReturnsResponse
public async Task RestClientGetRequestReturnsResponse()
{
var tc = new TestContext();
var client = tc.GetRestClient();
var anyRequest = new RestRequest
{
Method = Method.GET,
Resource = AnyResource,
RootElement = AnyRootElement
};
var successfulResponseMessage = CreateSuccessfulResponseMessage();
tc.HttpClient.GetAsync(AnyRequestUri + anyRequest.Resource, CancellationToken.None)
.Returns(Task.FromResult(successfulResponseMessage));
var response =
await client.ExecuteRequestAsync<AnyResponse>(anyRequest, AnyRequestUri, CancellationToken.None);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(AnyStringField, response.Data.AnyStringField);
Assert.Equal(AnyIntField, response.Data.AnyIntField);
Assert.Null(response.Exception);
var unused = tc.HttpClient.Received(1).GetAsync(AnyRequestUri + anyRequest.Resource, CancellationToken.None);
}
示例2: RestClientPostRequestReturnsResponse
public async Task RestClientPostRequestReturnsResponse()
{
var tc = new TestContext();
var client = tc.GetRestClient();
var anyRequest = new RestRequest
{
Method = Method.POST,
Resource = AnyResource,
RootElement = AnyRootElement,
Content = new StringContent(AnyPostContent, Encoding, MediaType)
};
var successfulResponseMessage = CreateSuccessfulResponseMessage();
tc.HttpClient.PostAsync(AnyRequestUri + anyRequest.Resource,
Arg.Is<StringContent>(
stringContent =>
stringContent.Headers.ContentType.MediaType == MediaType &&
stringContent.ReadAsStringAsync().Result == AnyPostContent),
CancellationToken.None)
.Returns(Task.FromResult(successfulResponseMessage));
var response =
await client.ExecuteRequestAsync<AnyResponse>(anyRequest, AnyRequestUri, CancellationToken.None);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(AnyStringField, response.Data.AnyStringField);
Assert.Equal(AnyIntField, response.Data.AnyIntField);
Assert.Null(response.Exception);
var unused = tc.HttpClient.Received(1).PostAsync(AnyRequestUri + anyRequest.Resource,
Arg.Is<StringContent>(
stringContent =>
stringContent.Headers.ContentType.MediaType == MediaType &&
stringContent.ReadAsStringAsync().Result == AnyPostContent),
CancellationToken.None);
}
示例3: RestClientRequestReturnsFailureResponse
public async Task RestClientRequestReturnsFailureResponse()
{
var tc = new TestContext();
var client = tc.GetRestClient();
var anyRequest = new RestRequest
{
Method = Method.GET,
Resource = AnyResource,
RootElement = AnyRootElement
};
var successfulResponseMessage = CreateFailedResponseMessage();
tc.HttpClient.GetAsync(AnyRequestUri + anyRequest.Resource, CancellationToken.None)
.Returns(Task.FromResult(successfulResponseMessage));
var response =
await client.ExecuteRequestAsync<AnyResponse>(anyRequest, AnyRequestUri, CancellationToken.None);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Null(response.Data);
Assert.NotNull(response.Exception);
Assert.IsType(typeof(HttpRequestException), response.Exception);
var unused = tc.HttpClient.Received(1).GetAsync(AnyRequestUri + anyRequest.Resource, CancellationToken.None);
}