FuncHandler CreateQueryRequestHandler(QueryParameters queryParameters, object response) { // Create a NameValueCollection with all expected query string parameters var expectedQueryStringCollection = new NameValueCollection(); foreach (var parameter in queryParameters.GetQueryParameters()) { if (null != parameter.Value) { expectedQueryStringCollection[parameter.Key] = parameter.Value; } } return(new FuncHandler() { ProduceResultAsync = (request, ct) => { var expectedPath = $"{HttpTests.GetUriForResource(SettingsEnv, KeenConstants.QueriesResource)}/" + $"{queryParameters.GetResourceName()}"; string actualPath = $"{request.RequestUri.Scheme}{Uri.SchemeDelimiter}" + $"{request.RequestUri.Authority}{request.RequestUri.AbsolutePath}"; Assert.AreEqual(expectedPath, actualPath); var actualQueryStringCollection = HttpUtility.ParseQueryString(request.RequestUri.Query); Assert.That(actualQueryStringCollection, Is.EquivalentTo(expectedQueryStringCollection)); return HttpTests.CreateJsonStringResponseAsync(response); } }); }
public async Task QueryFilter_NotContains_Success() { var queriesUrl = HttpTests.GetUriForResource(SettingsEnv, KeenConstants.QueriesResource); var handler = new FuncHandler() { PreProcess = (req, ct) => { var queryStr = req.RequestUri.Query; // Make sure our filter properties are in the query string Assert.IsTrue(queryStr.Contains("propertyName") && queryStr.Contains("four") && queryStr.Contains(QueryFilter.FilterOperator.NotContains())); }, ProduceResultAsync = (req, ct) => { return(HttpTests.CreateJsonStringResponseAsync(new { result = 2 })); }, DeferToDefault = false }; // NOTE : This example shows use of UrlToMessageHandler, but since we only make one // request to a single endpoint, we could just directly use the FuncHandler here. var urlHandler = new UrlToMessageHandler( new Dictionary <Uri, IHttpMessageHandler> { { queriesUrl, handler } }) { DeferToDefault = false }; var client = new KeenClient(SettingsEnv, new TestKeenHttpClientProvider() { ProvideKeenHttpClient = (url) => KeenHttpClientFactory.Create(url, new HttpClientCache(), null, new DelegatingHandlerMock(urlHandler)) }); var filters = new List <QueryFilter> { new QueryFilter("propertyName", QueryFilter.FilterOperator.NotContains(), "four") }; var count = await client.QueryAsync( QueryType.Count(), "testCollection", "", QueryRelativeTimeframe.ThisMonth(), filters); Assert.IsNotNull(count); Assert.AreEqual("2", count); }
public async Task Query_AvailableQueries_Success() { var queriesResource = HttpTests.GetUriForResource(SettingsEnv, KeenConstants.QueriesResource); var expectedQueries = new Dictionary <string, string>() { { "select_unique_url", $"{queriesResource.AbsolutePath}/select_unique" }, { "minimum", $"{queriesResource.AbsolutePath}/minimum" }, { "extraction_url", $"{queriesResource.AbsolutePath}/extraction" }, { "percentile", $"{queriesResource.AbsolutePath}/percentile" }, { "funnel_url", $"{queriesResource.AbsolutePath}/funnel" }, { "average", $"{queriesResource.AbsolutePath}/average" }, { "median", $"{queriesResource.AbsolutePath}/median" }, { "maximum", $"{queriesResource.AbsolutePath}/maximum" }, { "count_url", $"{queriesResource.AbsolutePath}/count" }, { "count_unique_url", $"{queriesResource.AbsolutePath}/count_unique" }, { "sum", $"{queriesResource.AbsolutePath}/sum" } }; FuncHandler handler = new FuncHandler() { ProduceResultAsync = (request, ct) => { return(HttpTests.CreateJsonStringResponseAsync(expectedQueries)); } }; var client = new KeenClient(SettingsEnv, new TestKeenHttpClientProvider() { ProvideKeenHttpClient = (url) => KeenHttpClientFactory.Create(url, new HttpClientCache(), null, new DelegatingHandlerMock(handler)) }); var actualQueries = await client.GetQueriesAsync(); Assert.That(actualQueries, Is.EquivalentTo(expectedQueries)); }