protected override async Task <SystemHttpResponseMessage> SendAsync(SystemHttpRequestMessage request, CancellationToken cancellationToken) { var setups = this.setups.Find(request).ToList(); // there are no setups that were found successful. if (setups.All(x => x.Exception is MockHttpClientException)) { throw MockHttpClientException.Combined(setups.Select(x => x.Exception)); } // there are more than 1 setup that was found. if (setups.Count(x => x.Exception is null) > 1) { throw await MockHttpClientException.MatchedMultipleRequests(request, setups.Count()); } var(setup, exception) = setups.FirstOrDefault(x => x.Exception is null); if (setup is null) { throw await MockHttpClientException.NoSetupAsync(request); } var result = setup.Results.GetResultNext(); if (result is null) { throw await MockHttpClientException.NoMatchingResponses(request); } result.Matched = true; return(result.HttpResponseMessage); }
private MockHttpClientException TryVerify(Func <Result, MockHttpClientException> verify) { List <MockHttpClientException> errors = new List <MockHttpClientException>(); foreach (var result in Results) { var error = verify(result); if (error is MockHttpClientException) { errors.Add(error); } } if (errors.Any()) { return(MockHttpClientException.Combined(errors)); } return(null); }
private MockHttpClientException TryVerify(Func <Setup, MockHttpClientException> verify) { List <MockHttpClientException> errors = new List <MockHttpClientException>(); foreach (var setup in Setups) { var error = verify(setup); if (error is MockHttpClientException) { errors.Add(error); } } if (errors.Any()) { return(MockHttpClientException.Combined(errors)); } return(null); }
public async Task <MockHttpClientException> Matches(SystemHttpRequestMessage message) { if (request.HttpMethod.Method != message.Method.Method) { return(await MockHttpClientException.UnmatchedHttpMethod(request, message)); } // TODO: better match request uri if (request.RequestUri != message.RequestUri.PathAndQuery) { return(await MockHttpClientException.UnmatchedRequestUri(request, message)); } if (request.HeadersValidator is Delegate) { var headers = new HttpRequestHeaders(message.Headers, message.Content?.Headers); var b = request.HeadersValidator.DynamicInvoke(headers); if (b is false) { return(await MockHttpClientException.UnmatchedRequestHeaders(request, message)); } } if (request.ContentValidator is Delegate) { var content = await request.Deserializer(message); var b = request.ContentValidator.DynamicInvoke(content); if (b is false) { return(await MockHttpClientException.UnmatchedRequestContent(request, message)); } } return(null); }
public MockHttpClientException VerifyAll() { return(Matched ? null : MockHttpClientException.UnmatchedResult(this)); }