Created
May 18, 2012 12:08
-
-
Save csainty/2724951 to your computer and use it in GitHub Desktop.
Nancy Route Tests
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Nancy; | |
using Nancy.Testing; | |
using Nancy.Testing.Fakes; | |
using Xunit; | |
using Xunit.Extensions; | |
namespace ClassLibrary1 | |
{ | |
public class Class1 | |
{ | |
private FakeNancyModule SetupModule(string Path, Func<dynamic, Response> Response) | |
{ | |
return new FakeNancyModule(with => | |
{ | |
with.Get(Path, Response); | |
}); | |
} | |
private ConfigurableBootstrapper SetupBoot(NancyModule module) | |
{ | |
return new ConfigurableBootstrapper(with => | |
{ | |
with.Module(module, module.GetType().FullName); | |
}); | |
} | |
[Theory] | |
[InlineData("/dinners", "/dinners/123")] | |
[InlineData("/dinners/edit", "/dinners/edit/123")] | |
public void Browser_GetRequest_AcceptsOptionalInt(string Root, string Path) | |
{ | |
//Arrange | |
var module = SetupModule(Root + Route.AnyIntOptional("id"), x => "OptionalInt"); | |
var boostrapper = SetupBoot(module); | |
var browser = new Browser(boostrapper); | |
//Act | |
var result = browser.Get(Path, with => | |
{ | |
with.HttpRequest(); | |
}); | |
//Assert | |
Assert.Equal("OptionalInt", result.Body.AsString()); | |
} | |
} | |
public static class Route | |
{ | |
public static RouteParameters AnyIntAtLeastOnce(string NamedGroup, int lengthStart, int lengthEnd) | |
{ | |
return new RouteParameters().AnyIntAtLeastOnce(NamedGroup, lengthStart, lengthEnd); | |
} | |
public static RouteParameters AnyIntAtLeastOnce(string NamedGroup) | |
{ | |
return new RouteParameters().AnyIntAtLeastOnce(NamedGroup); | |
} | |
public static RouteParameters AnyIntOptional(string NamedGroup) | |
{ | |
return new RouteParameters().AnyIntOptional(NamedGroup); | |
} | |
public static RouteParameters And() | |
{ | |
return new RouteParameters().And(); | |
} | |
public static RouteParameters Root() | |
{ | |
return new RouteParameters().Root(); | |
} | |
} | |
public class RouteParameters | |
{ | |
private StringBuilder builder; | |
public StringBuilder RouteBuilder | |
{ | |
get | |
{ | |
if (builder == null) | |
builder = new StringBuilder(); | |
return builder; | |
} | |
} | |
/// <summary> | |
/// Implicitly convert class to string | |
/// </summary> | |
/// <example>string MyString = new RouteParameters.Root().And().AnyIntAtLeastOnce("id");</example> | |
/// <param name="f"></param> | |
/// <returns></returns> | |
public static implicit operator string(RouteParameters f) { return f.RouteBuilder.ToString(); } | |
/// <summary> | |
/// Creates a route that expects at least one integer and with a defined length | |
/// </summary> | |
/// <example>/1 or /1234</example> | |
/// <param name="NamedGroup">The name of your parameter identifier</param> | |
/// <param name="lengthStart"></param> | |
/// <param name="lengthEnd"></param> | |
/// <returns></returns> | |
public RouteParameters AnyIntAtLeastOnce(string NamedGroup, int lengthStart, int lengthEnd) | |
{ | |
RouteBuilder.Append(@"(?<" + NamedGroup + @">[\d]{" + lengthStart + "," + lengthEnd + "})"); | |
return this; | |
} | |
/// <summary> | |
/// Creates a route that expects at least one integer | |
/// </summary> | |
/// <example> | |
/// /products/1 or /products/123 | |
/// </example> | |
/// <returns></returns> | |
public RouteParameters AnyIntAtLeastOnce(string NamedGroup) | |
{ | |
RouteBuilder.Append(@"(?<" + NamedGroup + @">[\d]+)"); | |
return this; | |
} | |
/// <summary> | |
/// Creates a route that will accept an int but the URL doesn't always have to contain one | |
/// </summary> | |
/// <example> | |
/// /products/1 or /products | |
/// </example> | |
/// <returns></returns> | |
public RouteParameters AnyIntOptional(string NamedGroup) | |
{ | |
RouteBuilder.Append(@"/?(?<" + NamedGroup + @">[\d]*)"); | |
return this; | |
} | |
/// <summary> | |
/// Seperates pattern segments | |
/// </summary> | |
/// <returns></returns> | |
public RouteParameters And() | |
{ | |
Root(); | |
return this; | |
} | |
/// <summary> | |
/// Creates a route segment | |
/// </summary> | |
/// <returns></returns> | |
public RouteParameters Root() | |
{ | |
RouteBuilder.Append("/"); | |
return this; | |
} | |
//string, string length, certain string, certain number | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment