-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Match prefixes for routes properly in owin handlers.
- Added functional and unit tests. #1445
- Loading branch information
Showing
8 changed files
with
235 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Microsoft.AspNet.SignalR.Owin/Infrastructure/PrefixMatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
|
||
namespace Microsoft.AspNet.SignalR.Owin.Infrastructure | ||
{ | ||
internal static class PrefixMatcher | ||
{ | ||
public static bool IsMatch(string pathBase, string path) | ||
{ | ||
pathBase = EnsureStartsWithSlash(pathBase); | ||
path = EnsureStartsWithSlash(path); | ||
|
||
var pathLength = path.Length; | ||
var pathBaseLength = pathBase.Length; | ||
|
||
if (pathLength < pathBaseLength) | ||
{ | ||
return false; | ||
} | ||
|
||
if (pathLength > pathBaseLength && path[pathBaseLength] != '/') | ||
{ | ||
return false; | ||
} | ||
|
||
if (!path.StartsWith(pathBase, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static string EnsureStartsWithSlash(string path) | ||
{ | ||
if (path.Length == 0) | ||
{ | ||
return path; | ||
} | ||
|
||
if (path[0] == '/') | ||
{ | ||
return path; | ||
} | ||
|
||
return '/' + path; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
tests/Microsoft.AspNet.SignalR.Tests/PrefixMatcherFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using Microsoft.AspNet.SignalR.Owin.Infrastructure; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNet.SignalR.Tests | ||
{ | ||
public class PrefixMatcherFacts | ||
{ | ||
[Fact] | ||
public void ExactMatch() | ||
{ | ||
// Act | ||
var result = PrefixMatcher.IsMatch("/echo", "/echo"); | ||
|
||
// Assert | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void PrefixMatches() | ||
{ | ||
// Act | ||
var result = PrefixMatcher.IsMatch("/echo", "/echo/foo"); | ||
|
||
// Assert | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void InvalidPrefixDoesNotMatch() | ||
{ | ||
// Act | ||
var result = PrefixMatcher.IsMatch("/echo", "/echo2/foo"); | ||
|
||
// Assert | ||
Assert.False(result); | ||
} | ||
|
||
[Fact] | ||
public void LongerPrefixDoesNotMatch() | ||
{ | ||
// Act | ||
var result = PrefixMatcher.IsMatch("/echo2/foo/bar", "/echo2/foo"); | ||
|
||
// Assert | ||
Assert.False(result); | ||
} | ||
|
||
[Fact] | ||
public void PrefixWithoutSlashesDoesntMatchIfDifferent() | ||
{ | ||
// Act | ||
var result = PrefixMatcher.IsMatch("echo", "echo2"); | ||
|
||
// Assert | ||
Assert.False(result); | ||
} | ||
|
||
[Fact] | ||
public void PrefixWithoutSlashesMatchesIfValidPrefix() | ||
{ | ||
// Act | ||
var result = PrefixMatcher.IsMatch("echo", "echo/negotiate"); | ||
|
||
// Assert | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void EmptyPrefixMatchesEverything() | ||
{ | ||
// Act | ||
var result = PrefixMatcher.IsMatch("", "echo/negotiate"); | ||
|
||
// Assert | ||
Assert.True(result); | ||
} | ||
} | ||
} |