public async Task CallAsync_Should_Throw_ArgumentException_If_Path_Is_Null_Or_Empty() { var restApiClient = new RestApiClient(new HttpClient(new Mock <HttpMessageHandler>(MockBehavior.Strict).Object)); await Assert.ThrowsAsync <ArgumentNullException>(async() => await restApiClient.CallAsync(HttpMethod.Get, null)); await Assert.ThrowsAsync <ArgumentException>(async() => await restApiClient.CallAsync(HttpMethod.Get, string.Empty)); }
public async Task When_executing_call_async_with_resilience_handler_and_request_without_response_then_verify_resilience_handler_is_called() { var request = new RequestWithoutResponse(new DummyResourceIdentifier()) { StringProperty = _fixture.Create <string>() }; await _sut.CallAsync(request); _resilienceHandler.Verify(); }
public async Task CallAsync_Should_Call_HttpClient_SendAsync_With_HttpRequestMessage_By_Given_Parameters(string httpMethodStr, string path, string queryParams, string headerParams) { var httpMethod = new HttpMethod(httpMethodStr); IList <KeyValuePair <string, string> > queryStingParameters = queryParams.ToQueryStingParameters(); IDictionary <string, string> headerParameters = headerParams.ToHeaderParameters(); var httpMessageHandler = new Mock <HttpMessageHandler>(MockBehavior.Strict); httpMessageHandler .Protected() // Setup the PROTECTED method to mock .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) // prepare the expected response of the mocked http call .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent("[{'id':1,'value':'1'}]"), }) .Verifiable(); var httpClient = new HttpClient(httpMessageHandler.Object); var restApiClient = new RestApiClient(httpClient, new ApiOptions(Token, Url)); await restApiClient.CallAsync(httpMethod, path, queryStingParameters, headerParameters); httpMessageHandler.Protected() .Verify("SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>(message => IsValidHttpRequestMessage(message, httpMethod, path, queryStingParameters, headerParameters)), ItExpr.IsAny <CancellationToken>()); }
public void When_executing_call_async_and_the_request_is_not_valid_it_throws_an_exception() { var request = new RequestWithResponse(new DummyResourceIdentifier()); Assert.ThrowsAsync <RestClientCallException>(async() => await _sut.CallAsync(request)); }