Skip to content

Commit

Permalink
Fixed bugs in default route pattern matcher
Browse files Browse the repository at this point in the history
The regex that were matching parameters only captured values if the
parameter was located at the beginning of the route segment. Also
it the route pattern matcher lost additional characters that were
part of the segment, but not of the parameters, so it ended up being
too greedy in some scenarios
  • Loading branch information
thecodejunkie committed Feb 9, 2012
1 parent ebd40a8 commit eb2eb33
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/Nancy.Tests/Unit/Routing/DefaultRoutePatternMatcherFixture.cs
Expand Up @@ -154,6 +154,32 @@ public void Should_allow_underscore_in_parameter_key()

// Then
((string)results.Parameters["b_ar"]).ShouldEqual(parameter);
}

[Fact]
public void Should_expectation()
{
// Given
const string parameter = "filename";

// When
var results = this.matcher.Match("/foo/" + parameter + ".cshtml", "/foo/{name}.cshtml");

// Then
((string)results.Parameters["name"]).ShouldEqual(parameter);
}

[Fact]
public void Should_expectation3()
{
// Given
const string parameter = "filename";

// When
var results = this.matcher.Match("/foo/bar" + parameter + ".cshtml", "/foo/bar{name}.cshtml");

// Then
((string)results.Parameters["name"]).ShouldEqual(parameter);
}
}
}
4 changes: 2 additions & 2 deletions src/Nancy/Extensions/StringExtensions.cs
Expand Up @@ -14,7 +14,7 @@ public static class StringExtensions
/// <value>A <see cref="Regex"/> object.</value>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly Regex ParameterExpression =
new Regex(@"^\{(?<name>[A-Za-z0-9_]*)\}", RegexOptions.Compiled);
new Regex(@"{(?<name>[A-Za-z0-9_]*)\}", RegexOptions.Compiled);

/// <summary>
/// Extracts the name of a parameter from a segment.
Expand Down Expand Up @@ -64,4 +64,4 @@ public static DynamicDictionary AsQueryDictionary(this string queryString)
return ret;
}
}
}
}
8 changes: 6 additions & 2 deletions src/Nancy/Routing/DefaultRoutePatternMatcher.cs
Expand Up @@ -83,10 +83,14 @@ private static IEnumerable<string> GetParameterizedSegments(IEnumerable<string>
var current = segment;
if (current.IsParameterized())
{
var parameterName = segment.GetParameterName();

var replacement =
string.Format(CultureInfo.InvariantCulture, @"(?<{0}>(.+?))", segment.GetParameterName());
string.Format(CultureInfo.InvariantCulture, @"(?<{0}>(.+?))", parameterName);

current = segment.Replace(segment, replacement);
current = segment.Replace(
string.Concat("{", parameterName, "}"),
replacement);
}

yield return current;
Expand Down

0 comments on commit eb2eb33

Please sign in to comment.