public Expectation Respond(ResponseBuilder responseBuilder) { if (responseBuilder == null) { throw new ArgumentNullException(nameof(responseBuilder)); } var expectation = new Expectation { HttpRequest = _requestBuilder.Create(), HttpResponse = responseBuilder.Create() }; var task = Task.Run(async() => { await _mockServerClient.SetExpectations(expectation); }); task.Wait(); return(expectation); }
public async void GivenSetExpectationsIsCalled_ThenApiIsCalledToSetExpectations() { var expectedUri = "http://mockserverhost:1080/expectation"; var messageHandler = new Mock <HttpMessageHandler>(MockBehavior.Strict); messageHandler .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK }) .Verifiable(); var httpClient = new HttpClient(messageHandler.Object); httpClient.BaseAddress = new Uri("http://mockserverhost:1080/"); var mockServerClient = new MockServerClient(httpClient); await mockServerClient.SetExpectations(new Expectation()); messageHandler.Protected().Verify( "SendAsync", Moq.Times.Exactly(1), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Put && req.RequestUri.ToString() == expectedUri ), ItExpr.IsAny <CancellationToken>() ); }