Last active
December 15, 2015 05:29
-
-
Save darrelmiller/5209735 to your computer and use it in GitHub Desktop.
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.Net.Http; | |
using System.Web.Http; | |
using System.Web.Http.Routing; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TestRoutes | |
{ | |
[TestClass] | |
public class RouteTests { | |
private HttpConfiguration configuration; | |
[TestInitialize] | |
public void Initialize() { | |
configuration = new HttpConfiguration(); | |
configuration.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", | |
new { id = RouteParameter.Optional }, new { id = @"\d+" }); | |
configuration.Routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}"); | |
configuration.Routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", | |
new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }); | |
configuration.Routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", | |
new {action = "Post"}, new {httpMethod = new HttpMethodConstraint(HttpMethod.Post)}); | |
} | |
[TestMethod] | |
public void TestSimpleControllerMatching() | |
{ | |
// Arrange | |
var request = new HttpRequestMessage() | |
{ | |
RequestUri = new Uri("http://exmaple.org/api/foo") | |
}; | |
// Act | |
var data = configuration.Routes.GetRouteData(request); | |
//Assert | |
Assert.AreEqual("foo", data.Values["controller"]); | |
Assert.AreEqual("Get", data.Values["action"]); | |
} | |
[TestMethod] | |
public void TestSimpleControllerMatchingWithPostRequest() | |
{ | |
// Arrange | |
var request = new HttpRequestMessage() { | |
RequestUri = new Uri("http://exmaple.org/api/foo"), | |
Method = HttpMethod.Post | |
}; | |
// Act | |
var data = configuration.Routes.GetRouteData(request); | |
//Assert | |
Assert.AreEqual("foo", data.Values["controller"]); | |
Assert.AreEqual("Post", data.Values["action"]); | |
} | |
[TestMethod] | |
public void TestControllerWithParameter() | |
{ | |
// Arrange | |
var request = new HttpRequestMessage() {RequestUri = new Uri("http://exmaple.org/api/bar/75")}; | |
// Act | |
var data = configuration.Routes.GetRouteData(request); | |
//Assert | |
Assert.AreEqual("bar", data.Values["controller"]); | |
Assert.AreEqual("75", data.Values["id"]); | |
} | |
[TestMethod] | |
public void TestControllerWithAction() | |
{ | |
// Arrange | |
var request = new HttpRequestMessage() {RequestUri = new Uri("http://exmaple.org/api/bar/explode")}; | |
// Act | |
var data = configuration.Routes.GetRouteData(request); | |
//Assert | |
Assert.AreEqual("bar", data.Values["controller"]); | |
Assert.AreEqual("explode", data.Values["action"]); | |
} | |
} | |
public class FooController : ApiController { } | |
public class BarController : ApiController { } | |
public class BazController : ApiController { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment