Created
August 23, 2013 18:45
-
-
Save raghuramn/6322659 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using System.Web.Http.Controllers; | |
using System.Web.Http.Filters; | |
using Xunit; | |
using Xunit.Extensions; | |
namespace TestRoutes | |
{ | |
public class TestRoutes | |
{ | |
[Theory] | |
[InlineData("http://localhost/Customers/42", "Test", "GetCustomer")] | |
[InlineData("http://localhost/Customers", "Test", "GetCustomers")] | |
[InlineData("http://localhost/Customers/42/Name", "Test", "GetCustomerName")] | |
public void RegisteredRoutes_MatchRight_ControllerAndAction(string uri, string expectedController, string expectedAction) | |
{ | |
// Arrange | |
HttpConfiguration config = new HttpConfiguration(); | |
config.MapHttpAttributeRoutes(); | |
config.Filters.Add(new TestActionFilter()); | |
HttpServer server = new HttpServer(config); | |
HttpClient client = new HttpClient(server); | |
// Act | |
var response = client.GetAsync(uri).Result; | |
// Assert | |
response.EnsureSuccessStatusCode(); | |
Tuple<string, string> result = response.Content.ReadAsAsync<Tuple<string, string>>().Result; | |
Assert.Equal(expectedController, result.Item2); | |
Assert.Equal(expectedAction, result.Item1); | |
} | |
} | |
// Return the action/controller metadata as response instead of invoking the actual action. | |
public class TestActionFilter : AuthorizationFilterAttribute | |
{ | |
public override void OnAuthorization(HttpActionContext actionContext) | |
{ | |
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, | |
Tuple.Create( | |
actionContext.ActionDescriptor.ActionName, | |
actionContext.ControllerContext.ControllerDescriptor.ControllerName)); | |
} | |
} | |
public class TestController : ApiController | |
{ | |
[Route("Customers")] | |
[Authorize] | |
public void GetCustomers() | |
{ | |
} | |
[Route("Customers/{id:int}")] | |
public void GetCustomer(int id) | |
{ | |
} | |
[Route("Customers/{id:int}/Name")] | |
public void GetCustomerName(int id) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment