Permalink
Browse files

#4097 Adding unit tests to assert autocomplete service hijack properl…

…y constructs query strings (#4121)
  • Loading branch information...
1 parent 68e84b6 commit d91a591b3c919278ab092437029161415f1fff6b @xavierdecoster xavierdecoster committed Jun 14, 2017
@@ -36,6 +36,19 @@ public AutoCompleteServiceQuery(IAppConfiguration configuration)
bool? includePrerelease,
string semVerLevel = null)
{
+ queryString = BuildQueryString(queryString, includePrerelease, semVerLevel);
+
+ var endpoints = await _serviceDiscoveryClient.GetEndpointsForResourceType(_autocompleteServiceResourceType);
+ endpoints = endpoints.Select(e => new Uri(e + queryString)).AsEnumerable();
+
+ var result = await _httpClient.GetStringAsync(endpoints);
+ var resultObject = JObject.Parse(result);
+
+ return resultObject["data"].Select(entry => entry.ToString());
+ }
+
+ internal string BuildQueryString(string queryString, bool? includePrerelease, string semVerLevel = null)
+ {
queryString += $"&prerelease={includePrerelease ?? false}";
NuGetVersion semVerLevelVersion;
@@ -44,13 +57,12 @@ public AutoCompleteServiceQuery(IAppConfiguration configuration)
queryString += $"&semVerLevel={semVerLevel}";
}
- var endpoints = await _serviceDiscoveryClient.GetEndpointsForResourceType(_autocompleteServiceResourceType);
- endpoints = endpoints.Select(e => new Uri(e + "?" + queryString)).AsEnumerable();
-
- var result = await _httpClient.GetStringAsync(endpoints);
- var resultObject = JObject.Parse(result);
+ if (string.IsNullOrEmpty(queryString))
+ {
+ return string.Empty;
+ }
- return resultObject["data"].Select(entry => entry.ToString());
+ return "?" + queryString.TrimStart('&');
}
}
}
@@ -43,5 +43,22 @@ private IAppConfiguration GetConfiguration()
var result = await query.Execute("jquery", false);
Assert.Contains("jquery", result, StringComparer.OrdinalIgnoreCase);
}
+
+ [Theory]
+ [InlineData(true, null, "?take=30&q=Json&prerelease=True")]
+ [InlineData(true, "2.0.0", "?take=30&q=Json&prerelease=True&semVerLevel=2.0.0")]
+ [InlineData(false, null, "?take=30&q=Json&prerelease=False")]
+ [InlineData(false, "2.0.0", "?take=30&q=Json&prerelease=False&semVerLevel=2.0.0")]
+ public void PackageIdQueryBuildsCorrectQueryString(bool includePrerelease, string semVerLevel, string expectedQueryString)
+ {
+ // Arrange
+ var query = new AutoCompleteServicePackageIdsQuery(GetConfiguration());
+
+ // Act
+ var actualQueryString = query.BuildQueryString("take=30&q=Json", includePrerelease, semVerLevel);
+
+ // Assert
+ Assert.Equal(expectedQueryString, actualQueryString);
+ }
}
}
@@ -16,15 +16,15 @@ private IAppConfiguration GetConfiguration()
{
var mockConfiguration = new Mock<IAppConfiguration>();
mockConfiguration.SetupGet(c => c.ServiceDiscoveryUri).Returns(new Uri("https://api.nuget.org/v3/index.json"));
- mockConfiguration.SetupGet(c => c.AutocompleteServiceResourceType).Returns("SearchAutocompleteService/3.0.0-rc");
+ mockConfiguration.SetupGet(c => c.AutocompleteServiceResourceType).Returns("SearchAutocompleteService");
return mockConfiguration.Object;
}
[Fact]
public async Task ExecuteThrowsForEmptyId()
{
var query = new AutoCompleteServicePackageVersionsQuery(GetConfiguration());
- await Assert.ThrowsAsync<ArgumentNullException>(async () => await query.Execute("", false));
+ await Assert.ThrowsAsync<ArgumentNullException>(async () => await query.Execute(string.Empty, false));
}
[Fact]
@@ -34,5 +34,22 @@ private IAppConfiguration GetConfiguration()
var result = await query.Execute("newtonsoft.json", false);
Assert.True(result.Any());
}
+
+ [Theory]
+ [InlineData(true, null, "?id=Newtonsoft.Json&prerelease=True")]
+ [InlineData(true, "2.0.0", "?id=Newtonsoft.Json&prerelease=True&semVerLevel=2.0.0")]
+ [InlineData(false, null, "?id=Newtonsoft.Json&prerelease=False")]
+ [InlineData(false, "2.0.0", "?id=Newtonsoft.Json&prerelease=False&semVerLevel=2.0.0")]
+ public void PackageVersionsQueryBuildsCorrectQueryString(bool includePrerelease, string semVerLevel, string expectedQueryString)
+ {
+ // Arrange
+ var query = new AutoCompleteServicePackageVersionsQuery(GetConfiguration());
+
+ // Act
+ var actualQueryString = query.BuildQueryString("id=Newtonsoft.Json", includePrerelease, semVerLevel);
+
+ // Assert
+ Assert.Equal(expectedQueryString, actualQueryString);
+ }
}
}

0 comments on commit d91a591

Please sign in to comment.