public async Task ManualInjection_RecommendedPassiveMocking() { // Given m_TeePeeBuilder.ForRequest("https://some.api/path/resource", HttpMethod.Get) .ContainingQueryParam("filter", "those") .Responds() .WithStatus(HttpStatusCode.OK) .WithBody(new { Things = new[] { new { Value = 10 } } }); var controller = new HttpClientFactoryTypedUsageController(new ExampleTypedHttpClient(m_TeePeeBuilder.Build().Manual("https://some.api").CreateClient())); // When var result = await controller.FireAndAct(); // Then Assert.NotNull(result); var okResult = Assert.IsType <OkObjectResult>(result); var resultValue = Assert.IsType <int>(okResult.Value); Assert.Equal(10, resultValue); }
public void ForRequestDoesNotThrowOnDifferentUrlAndMethod(string firstUrl, HttpMethod firstMethod, string secondUrl, HttpMethod secondMethod) { // Given var builder = new TeePeeBuilder(); builder.ForRequest(firstUrl, firstMethod); // When var ex = Record.Exception(() => builder.ForRequest(secondUrl, secondMethod)); Assert.Null(ex); }
public void ForRequestThrowsOnDuplicateUrlAndMethod(string url, HttpMethod method) { // Given var builder = new TeePeeBuilder(); builder.ForRequest(url, method); // When var ex = Record.Exception(() => builder.ForRequest(url, method)); Assert.IsType <ArgumentException>(ex); Assert.Contains("already a request match for", ex.Message); }
public void ContainingQueryParamThrowsIfAnotherMatchForUrlWithQueryStringAlreadyExists() { // Given var builder = new TeePeeBuilder(); builder.ForRequest("https://site.net/api/items?filter=value", HttpMethod.Get); var requestBuilder = builder.ForRequest("https://site.net/api/other", HttpMethod.Get); // When var ex = Record.Exception(() => requestBuilder.ContainingQueryParam("sort", "name")); // Then Assert.IsType <InvalidOperationException>(ex); Assert.Contains("request matches already exist with QueryString matching", ex.Message); }
public async Task ManualInjection_RecommendedPassiveMocking() { // Given m_TeePeeBuilderOne.ForRequest("https://first.api/pathone/resourceone", HttpMethod.Get) .ContainingQueryParam("filter", "those") .Responds() .WithStatus(HttpStatusCode.OK) .WithBody(new { Things = new[] { new { Value = 10 } } }); m_TeePeeBuilderTwo.ForRequest("https://second.api/pathtwo/resourcetwo", HttpMethod.Get) .ContainingQueryParam("filter", "those") .Responds() .WithStatus(HttpStatusCode.OK) .WithBody(new { Things = new[] { new { Value = 30 } } }); var controller = new HttpClientFactoryMultipleNamedUsageController(new[] { m_TeePeeBuilderOne.Build().Manual("https://first.api"), m_TeePeeBuilderTwo.Build().Manual("https://second.api") } .ToHttpClientFactory()); // When var result = await controller.FireAndAct(); // Then Assert.NotNull(result); var okResult = Assert.IsType <OkObjectResult>(result); var resultValue = Assert.IsType <int>(okResult.Value); Assert.Equal(40, resultValue); }
public void WithUrlDoesNotThrowIfAbsolute(string absoluteUrl) { // Given var builder = new TeePeeBuilder(); // When var ex = Record.Exception(() => builder.ForRequest(absoluteUrl, HttpMethod.Get)); // Then Assert.Null(ex); }
public void ForRequestThrowsIfNotAbsolute(string relativeUrl) { // Given var builder = new TeePeeBuilder(); // When var ex = Record.Exception(() => builder.ForRequest(relativeUrl, HttpMethod.Get)); // Then Assert.IsType <ArgumentException>(ex); Assert.Contains("must be an absolute URI", ex.Message); }
public void ContainingQueryParamThrowsIfUrlHasQueryString() { // Given var builder = new TeePeeBuilder(); var requestBuilder = builder.ForRequest("https://site.net/api/items?filter=value", HttpMethod.Get); // When var ex = Record.Exception(() => requestBuilder.ContainingQueryParam("sort", "name")); // Then Assert.IsType <InvalidOperationException>(ex); Assert.Contains("Url has already been configured to match with a QueryString", ex.Message); }
public void WithBodyThrowsIfCalledMoreThanOnce() { // Given var builder = new TeePeeBuilder(); var requestBuilder = builder.ForRequest("https://site.net/api/items", HttpMethod.Get) .WithBody("test"); // When var ex = Record.Exception(() => requestBuilder.WithBody("test")); // Then Assert.IsType <InvalidOperationException>(ex); Assert.Contains("matching Body has already been added", ex.Message); }
public void ContainingHeaderThrowsIfDupeName() { // Given var builder = new TeePeeBuilder(); var requestBuilder = builder.ForRequest("https://site.net/api/other", HttpMethod.Get) .ContainingHeader("Authorization", "Bearer x"); // When var ex = Record.Exception(() => requestBuilder.ContainingHeader("Authorization", "Bearer y")); // Then Assert.IsType <ArgumentException>(ex); Assert.Contains("already been added", ex.Message); }
// Shortcut methods private RequestMatchBuilder RequestMatchBuilder() => m_TrackingBuilder.ForRequest(m_Url, m_HttpMethod);