public void MissingParametersAreMissingInThePayload() { var parameters = new SearchParameters(); // Search text can never be null. SearchParametersPayload payload = parameters.ToPayload("*"); Assert.True(payload.Count.HasValue); Assert.False(payload.Count.Value); // IncludeTotalCount is non-nullable in the client contract. Assert.NotNull(payload.Facets); Assert.False(payload.Facets.Any()); Assert.Null(payload.Filter); Assert.Null(payload.Highlight); Assert.Null(payload.HighlightPostTag); Assert.Null(payload.HighlightPreTag); Assert.Null(payload.MinimumCoverage); Assert.Null(payload.OrderBy); Assert.True(payload.QueryType.HasValue); Assert.Equal(QueryType.Simple, payload.QueryType.Value); // QueryType is non-nullable in the client contract. Assert.NotNull(payload.ScoringParameters); Assert.False(payload.ScoringParameters.Any()); Assert.Null(payload.ScoringProfile); Assert.Equal("*", payload.Search); Assert.Null(payload.SearchFields); Assert.True(payload.SearchMode.HasValue); Assert.Equal(SearchMode.Any, payload.SearchMode.Value); // SearchMode is non-nullable in the client contract. Assert.Null(payload.Select); Assert.Null(payload.Skip); Assert.Null(payload.Top); }
private static bool NextPageParametersEquals(SearchParametersPayload x, SearchParametersPayload y) { if (x == null && y == null) { return(true); } if ((x == null) != (y == null)) { return(false); } return (x.Count == y.Count && ((x.Facets == null && y.Facets == null) || x.Facets.SequenceEqual(y.Facets)) && x.Filter == y.Filter && x.Highlight == y.Highlight && x.HighlightPostTag == y.HighlightPostTag && x.HighlightPreTag == y.HighlightPreTag && x.MinimumCoverage == y.MinimumCoverage && x.OrderBy == y.OrderBy && x.QueryType == y.QueryType && ((x.ScoringParameters == null && y.ScoringParameters == null) || x.ScoringParameters.SequenceEqual(y.ScoringParameters)) && x.ScoringProfile == y.ScoringProfile && x.Search == y.Search && x.SearchFields == y.SearchFields && x.SearchMode == y.SearchMode && x.Select == y.Select && x.Skip == y.Skip && x.Top == y.Top); }
public void CanConvertToPostPayload() { var parameters = new SearchParameters() { Facets = new[] { "abc", "efg" }, Filter = "x eq y", HighlightFields = new[] { "a", "b" }, HighlightPostTag = "</em>", HighlightPreTag = "<em>", IncludeTotalResultCount = true, MinimumCoverage = 33.3, OrderBy = new[] { "a", "b desc" }, QueryType = QueryType.Full, ScoringParameters = new[] { new ScoringParameter("a", new[] { "b" }), new ScoringParameter("c", GeographyPoint.Create(-16, 55)) }, ScoringProfile = "xyz", SearchFields = new[] { "a", "b", "c" }, SearchMode = SearchMode.All, Select = new[] { "e", "f", "g" }, Skip = 10, Top = 5 }; SearchParametersPayload payload = parameters.ToPayload("find me"); Assert.True(parameters.Facets.SequenceEqual(payload.Facets)); Assert.Equal(parameters.Filter, payload.Filter); Assert.Equal(parameters.HighlightFields.ToCommaSeparatedString(), payload.Highlight); Assert.Equal(parameters.HighlightPostTag, payload.HighlightPostTag); Assert.Equal(parameters.HighlightPreTag, payload.HighlightPreTag); Assert.Equal(parameters.IncludeTotalResultCount, payload.Count); Assert.Equal(parameters.MinimumCoverage, payload.MinimumCoverage); Assert.Equal(parameters.OrderBy.ToCommaSeparatedString(), payload.OrderBy); Assert.Equal(parameters.QueryType, payload.QueryType); Assert.True(parameters.ScoringParameters.Select(p => p.ToString()).SequenceEqual(payload.ScoringParameters)); Assert.Equal(parameters.ScoringProfile, payload.ScoringProfile); Assert.Equal("find me", payload.Search); Assert.Equal(parameters.SearchFields.ToCommaSeparatedString(), payload.SearchFields); Assert.Equal(parameters.SearchMode, payload.SearchMode); Assert.Equal(parameters.Select.ToCommaSeparatedString(), payload.Select); Assert.Equal(parameters.Skip, payload.Skip); Assert.Equal(parameters.Top, payload.Top); }
private async Task <TResponse> DoContinueSearchAsync <TResponse, TResult, TDoc>( string url, SearchParametersPayload searchParameters, bool useGet, bool shouldTrace, string invocationId, CancellationToken cancellationToken, Func <string, DocumentSearchResponsePayload <TResult, TDoc> > deserialize) where TResponse : DocumentSearchResponseBase <TResult, TDoc>, new() where TResult : SearchResultBase <TDoc> where TDoc : class { // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = useGet ? HttpMethod.Get : HttpMethod.Post; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("Accept", "application/json;odata.metadata=none"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request for POST only if (!useGet) { string requestContent = JsonConvert.SerializeObject(searchParameters, JsonUtility.DefaultSerializerSettings); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); } // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result TResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new TResponse(); if (string.IsNullOrEmpty(responseContent) == false) { DocumentSearchResponsePayload <TResult, TDoc> deserializedResult = deserialize(responseContent); result.Count = deserializedResult.Count; result.Coverage = deserializedResult.Coverage; result.Facets = deserializedResult.Facets; result.Results = deserializedResult.Documents; result.ContinuationToken = deserializedResult.NextLink != null ? new SearchContinuationToken( deserializedResult.NextLink, deserializedResult.NextPageParameters) : null; } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("request-id")) { result.RequestId = httpResponse.Headers.GetValues("request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }