|
| 1 | +using EssentialCSharp.Web.Services; |
| 2 | + |
| 3 | +namespace EssentialCSharp.Web.Tests; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Unit tests for the route parameter regex in RouteConfigurationService. |
| 7 | +/// Calls RouteParameterRegex() directly to verify which patterns match (and should be |
| 8 | +/// excluded from the sitemap) vs. which do not (and should be included). |
| 9 | +/// </summary> |
| 10 | +public class RouteParameterFilterTests |
| 11 | +{ |
| 12 | + // --- patterns that SHOULD match (route is parameterised → excluded from sitemap) --- |
| 13 | + |
| 14 | + [Test] |
| 15 | + public async Task RouteParameterRegex_MatchesSimpleCurlyBraceParameter() |
| 16 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("{chapter}")).IsTrue(); |
| 17 | + |
| 18 | + [Test] |
| 19 | + public async Task RouteParameterRegex_MatchesConstrainedParameter() |
| 20 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("{id:guid}")).IsTrue(); |
| 21 | + |
| 22 | + [Test] |
| 23 | + public async Task RouteParameterRegex_MatchesOptionalParameter() |
| 24 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("{id?}")).IsTrue(); |
| 25 | + |
| 26 | + [Test] |
| 27 | + public async Task RouteParameterRegex_MatchesParameterWithDefault() |
| 28 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("{action=Index}")).IsTrue(); |
| 29 | + |
| 30 | + [Test] |
| 31 | + public async Task RouteParameterRegex_MatchesCatchAllParameter() |
| 32 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("{*catchall}")).IsTrue(); |
| 33 | + |
| 34 | + [Test] |
| 35 | + public async Task RouteParameterRegex_MatchesParameterEmbeddedInPath() |
| 36 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("chapter/{chapter}/listing/{listing}")).IsTrue(); |
| 37 | + |
| 38 | + [Test] |
| 39 | + public async Task RouteParameterRegex_MatchesSquareBracketSegment() |
| 40 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("[optional]")).IsTrue(); |
| 41 | + |
| 42 | + [Test] |
| 43 | + public async Task RouteParameterRegex_MatchesSquareBracketEmbeddedInPath() |
| 44 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("area/[optional]/page")).IsTrue(); |
| 45 | + |
| 46 | + // --- patterns that should NOT match (static route → eligible for sitemap) --- |
| 47 | + |
| 48 | + [Test] |
| 49 | + public async Task RouteParameterRegex_DoesNotMatchBareWord() |
| 50 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("about")).IsFalse(); |
| 51 | + |
| 52 | + [Test] |
| 53 | + public async Task RouteParameterRegex_DoesNotMatchHyphenatedRoute() |
| 54 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("mcp-setup")).IsFalse(); |
| 55 | + |
| 56 | + [Test] |
| 57 | + public async Task RouteParameterRegex_DoesNotMatchStaticMultiSegmentPath() |
| 58 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("identity/account/login")).IsFalse(); |
| 59 | + |
| 60 | + [Test] |
| 61 | + public async Task RouteParameterRegex_DoesNotMatchApiPrefixAlone() |
| 62 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch("api/listing")).IsFalse(); |
| 63 | + |
| 64 | + [Test] |
| 65 | + public async Task RouteParameterRegex_DoesNotMatchEmptyString() |
| 66 | + => await Assert.That(RouteConfigurationService.RouteParameterRegex().IsMatch(string.Empty)).IsFalse(); |
| 67 | +} |
0 commit comments