|
| 1 | +//----------------------------------------------------------------------------- |
| 2 | +// <copyright file="IgnoreEmptyParamsTest.cs" company=".NET Foundation"> |
| 3 | +// Copyright (c) .NET Foundation and Contributors. All rights reserved. |
| 4 | +// See License.txt in the project root for license information. |
| 5 | +// </copyright> |
| 6 | +//-- |
| 7 | + |
| 8 | +using System.Net; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Microsoft.AspNetCore.OData.Routing.Controllers; |
| 12 | +using Microsoft.AspNetCore.OData.TestCommon; |
| 13 | +using Microsoft.Extensions.DependencyInjection; |
| 14 | +using Microsoft.OData.Edm; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace Microsoft.AspNetCore.OData.E2E.Tests.UriParserExtension; |
| 18 | + |
| 19 | +public class IgnoreEmptyParamsTest : WebApiTestBase<IgnoreEmptyParamsTest> |
| 20 | +{ |
| 21 | + public IgnoreEmptyParamsTest(WebApiTestFixture<IgnoreEmptyParamsTest> fixture) |
| 22 | + : base(fixture) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + protected static void UpdateConfigureServices(IServiceCollection services) |
| 27 | + { |
| 28 | + services.ConfigureControllers(typeof(CustomersController), typeof(OrdersController), typeof(MetadataController)); |
| 29 | + |
| 30 | + IEdmModel model = UriParserExtenstionEdmModel.GetEdmModel(); |
| 31 | + |
| 32 | + services.AddControllers().AddOData(opt => |
| 33 | + { |
| 34 | + opt.AddRouteComponents("odata", model).Count().Filter().OrderBy().Select().Expand().SetMaxTop(null); |
| 35 | + opt.EnableNoDollarQueryOptions = true; |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + public static TheoryDataSet<string, string, string> IgnoreEmptyParamsCases |
| 40 | + { |
| 41 | + get |
| 42 | + { |
| 43 | + return new TheoryDataSet<string, string, string>() |
| 44 | + { |
| 45 | + { "Get", "Customers?$top=10&$skip=0", "Customers?$top=10&$skip=0&%20" }, |
| 46 | + { "Get", "Customers?$select=Name,Id", "Customers?%20=foo&$select=Name,Id" }, |
| 47 | + { "Get", "Customers?$orderby=Name", "Customers?%20=foo$orderby=Name" }, |
| 48 | + { "Get", "Customers?$filter=Name eq 'test'", "Customers?$filter=Name eq 'test'&" }, |
| 49 | + { "Get", "Customers?$count=true", "Customers?%20=%20&$count=true" }, |
| 50 | + |
| 51 | + { "Get", "Customers?top=10&skip=0", "Customers?top=10&skip=0&%20" }, |
| 52 | + { "Get", "Customers?select=Name,Id", "Customers?&%20=foo&select=Name,Id" }, |
| 53 | + { "Get", "Customers?orderby=Name", "Customers?%20=foo&orderby=Name" }, |
| 54 | + { "Get", "Customers?filter=Name eq 'test'", "Customers?filter=Name eq 'test'&" }, |
| 55 | + { "Get", "Customers?count=true", "Customers?%20=%20&count=true" }, |
| 56 | + |
| 57 | + { "Get", "Customers", "Customers?%20" }, |
| 58 | + { "Get", "Customers(1)", "Customers(1)?=foo" }, |
| 59 | + }; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + [Theory] |
| 64 | + [MemberData(nameof(IgnoreEmptyParamsCases))] |
| 65 | + public async Task ParserIgnoresEmptyParamsTest(string method, string baselinePath, string emptyParamsPath) |
| 66 | + { |
| 67 | + // Baseline scenario: query params are all correct |
| 68 | + HttpClient client = CreateClient(); |
| 69 | + |
| 70 | + var baselineUri = $"odata/{baselinePath}"; |
| 71 | + HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), baselineUri); |
| 72 | + HttpResponseMessage response = await client.SendAsync(request); |
| 73 | + |
| 74 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 75 | + string baselineResponse = await response.Content.ReadAsStringAsync(); |
| 76 | + |
| 77 | + // Test scenario: some params are injected in the query string, such as empty spaces |
| 78 | + var emptyParamsUri = $"odata/{emptyParamsPath}"; |
| 79 | + request = new HttpRequestMessage(new HttpMethod(method), emptyParamsUri); |
| 80 | + response = await client.SendAsync(request); |
| 81 | + |
| 82 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 83 | + string emptyParamsResponse = await response.Content.ReadAsStringAsync(); |
| 84 | + |
| 85 | + // Expected behavior: empty params are ignored and responses from both scenarios are equivalent |
| 86 | + Assert.Equal(baselineResponse, emptyParamsResponse); |
| 87 | + } |
| 88 | +} |
0 commit comments