1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Dispatcher;
using System.Web.Http.Routing;
using Microsoft.Web.Http.Data.Test.Models;
using Xunit;
namespace Microsoft.Web.Http.Data.Test
{
public class DataControllerQueryTests
{
/// <summary>
/// Execute a simple query with limited results
/// </summary>
[Fact]
public void GetProducts()
{
HttpConfiguration config = GetTestConfiguration();
HttpServer server = GetTestCatalogServer(config);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
HttpRequestMessage request = TestHelpers.CreateTestMessage(TestConstants.CatalogUrl + "GetProducts", HttpMethod.Get, config);
HttpResponseMessage response = invoker.SendAsync(request, CancellationToken.None).Result;
Product[] products = response.Content.ReadAsAsync<IQueryable<Product>>().Result.ToArray();
Assert.Equal(9, products.Length);
}
/// <summary>
/// Execute a query with an OData filter specified
/// </summary>
[Fact]
public void Query_Filter()
{
HttpConfiguration config = GetTestConfiguration();
HttpServer server = GetTestCatalogServer(config);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
string query = "?$filter=UnitPrice lt 5.0";
HttpRequestMessage request = TestHelpers.CreateTestMessage(TestConstants.CatalogUrl + "GetProducts" + query, HttpMethod.Get, config);
HttpResponseMessage response = invoker.SendAsync(request, CancellationToken.None).Result;
Product[] products = response.Content.ReadAsAsync<IQueryable<Product>>().Result.ToArray();
Assert.Equal(8, products.Length);
}
/// <summary>
/// Verify that the json/xml formatter instances are not shared between controllers, since
/// their serializers are configured per controller.
/// </summary>
[Fact(Skip = "Need to verify if this test still makes sense given changed ObjectContent design")]
public void Query_VerifyFormatterConfiguration()
{
HttpConfiguration config = GetTestConfiguration();
HttpServer catalogServer = GetTestCatalogServer(config);
HttpMessageInvoker catalogInvoker = new HttpMessageInvoker(catalogServer);
HttpServer citiesServer = GetTestCitiesServer(config);
HttpMessageInvoker citiesInvoker = new HttpMessageInvoker(citiesServer);
// verify products query
HttpRequestMessage request = TestHelpers.CreateTestMessage(TestConstants.CatalogUrl + "GetProducts", HttpMethod.Get, config);
HttpResponseMessage response = catalogInvoker.SendAsync(request, CancellationToken.None).Result;
Product[] products = response.Content.ReadAsAsync<IQueryable<Product>>().Result.ToArray();
Assert.Equal(9, products.Length);
// verify serialization
QueryResult qr = new QueryResult(products, products.Length);
ObjectContent oc = (ObjectContent)response.Content;
MemoryStream ms = new MemoryStream();
Task task = new JsonMediaTypeFormatter().WriteToStreamAsync(typeof(QueryResult), qr, ms, oc.Headers, null);
task.Wait();
Assert.True(ms.Length > 0);
// verify cities query
request = TestHelpers.CreateTestMessage(TestConstants.CitiesUrl + "GetCities", HttpMethod.Get, config);
response = citiesInvoker.SendAsync(request, CancellationToken.None).Result;
City[] cities = response.Content.ReadAsAsync<IQueryable<City>>().Result.ToArray();
Assert.Equal(11, cities.Length);
// verify serialization
qr = new QueryResult(cities, cities.Length);
oc = (ObjectContent)response.Content;
ms = new MemoryStream();
task = new JsonMediaTypeFormatter().WriteToStreamAsync(typeof(QueryResult), qr, ms, oc.Headers, null);
task.Wait();
Assert.True(ms.Length > 0);
}
/// <summary>
/// Execute a query that requests an inline count with a paging query applied.
/// </summary>
[Fact]
public void Query_InlineCount_SkipTop()
{
HttpConfiguration config = GetTestConfiguration();
HttpServer server = GetTestCatalogServer(config);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
string query = "?$filter=UnitPrice lt 5.0&$skip=2&$top=5&$inlinecount=allpages";
HttpRequestMessage request = TestHelpers.CreateTestMessage(TestConstants.CatalogUrl + "GetProducts" + query, HttpMethod.Get, config);
HttpResponseMessage response = invoker.SendAsync(request, CancellationToken.None).Result;
QueryResult queryResult = response.Content.ReadAsAsync<QueryResult>().Result;
Assert.Equal(5, queryResult.Results.Cast<object>().Count());
Assert.Equal(8, queryResult.TotalCount);
}
/// <summary>
/// Execute a query that requests an inline count with only a top operation applied in the query.
/// Expect the total count to not inlcude the take operation.
/// </summary>
[Fact]
public void Query_IncludeTotalCount_Top()
{
HttpConfiguration config = GetTestConfiguration();
HttpServer server = GetTestCatalogServer(config);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
string query = "?$filter=UnitPrice lt 5.0&$top=5&$inlinecount=allpages";
HttpRequestMessage request = TestHelpers.CreateTestMessage(TestConstants.CatalogUrl + "GetProducts" + query, HttpMethod.Get, config);
HttpResponseMessage response = invoker.SendAsync(request, CancellationToken.None).Result;
QueryResult queryResult = response.Content.ReadAsAsync<QueryResult>().Result;
Assert.Equal(5, queryResult.Results.Cast<object>().Count());
Assert.Equal(8, queryResult.TotalCount);
}
/// <summary>
/// Execute a query that requests an inline count with no paging operations specified in the
/// user query. There is however still a server specified limit.
/// </summary>
[Fact]
public void Query_IncludeTotalCount_NoPaging()
{
HttpConfiguration config = GetTestConfiguration();
HttpServer server = GetTestCatalogServer(config);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
string query = "?$filter=UnitPrice lt 5.0&$inlinecount=allpages";
HttpRequestMessage request = TestHelpers.CreateTestMessage(TestConstants.CatalogUrl + "GetProducts" + query, HttpMethod.Get, config);
HttpResponseMessage response = invoker.SendAsync(request, CancellationToken.None).Result;
QueryResult queryResult = response.Content.ReadAsAsync<QueryResult>().Result;
Assert.Equal(8, queryResult.Results.Cast<object>().Count());
Assert.Equal(8, queryResult.TotalCount);
}
/// <summary>
/// Execute a query that sets the inlinecount option explicitly to 'none', and verify count is not returned.
/// </summary>
[Fact]
public void Query_IncludeTotalCount_False()
{
HttpConfiguration config = GetTestConfiguration();
HttpServer server = GetTestCatalogServer(config);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
string query = "?$filter=UnitPrice lt 5.0&$inlinecount=none";
HttpRequestMessage request = TestHelpers.CreateTestMessage(TestConstants.CatalogUrl + "GetProducts" + query, HttpMethod.Get, config);
HttpResponseMessage response = invoker.SendAsync(request, CancellationToken.None).Result;
Product[] products = response.Content.ReadAsAsync<IQueryable<Product>>().Result.ToArray();
Assert.Equal(8, products.Length);
}
/// <summary>
/// Verify that when no skip/top query operations are performed (and no result limits are active
/// on the action server side), the total count returned is -1, indicating that the total count
/// equals the result count. This avoids the fx having to double enumerate the query results to
/// set the count server side.
/// </summary>
[Fact]
public void Query_TotalCount_Equals_ResultCount()
{
HttpConfiguration config = GetTestConfiguration();
HttpServer server = GetTestCatalogServer(config);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
string query = "?$inlinecount=allpages";
HttpRequestMessage request = TestHelpers.CreateTestMessage(TestConstants.CatalogUrl + "GetOrders" + query, HttpMethod.Get, config);
HttpResponseMessage response = invoker.SendAsync(request, CancellationToken.None).Result;
QueryResult result = response.Content.ReadAsAsync<QueryResult>().Result;
Assert.Equal(2, result.Results.Cast<object>().Count());
Assert.Equal(-1, result.TotalCount);
}
private HttpConfiguration GetTestConfiguration()
{
HttpConfiguration config = new HttpConfiguration();
return config;
}
private HttpServer GetTestCatalogServer(HttpConfiguration config)
{
HttpControllerDispatcher dispatcher = new HttpControllerDispatcher(config);
HttpRoute route = new HttpRoute("{controller}/{action}", new HttpRouteValueDictionary("Catalog"));
config.Routes.Add("catalog", route);
HttpServer server = new HttpServer(config, dispatcher);
return server;
}
private HttpServer GetTestCitiesServer(HttpConfiguration config)
{
HttpControllerDispatcher dispatcher = new HttpControllerDispatcher(config);
HttpRoute route = new HttpRoute("{controller}/{action}", new HttpRouteValueDictionary("Cities"));
config.Routes.Add("cities", route);
HttpServer server = new HttpServer(config, dispatcher);
return server;
}
}
}
|