Created
September 8, 2014 21:12
-
-
Save jamesrcounts/d718d600d9723a1cccb3 to your computer and use it in GitHub Desktop.
WebApi Route Test
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.Collections.Generic; | |
using System.Net.Http; | |
using System.Text; | |
using System.Web.Http; | |
using ApprovalTests; | |
using ApprovalUtilities.Utilities; | |
public static class WebApiApprovals | |
{ | |
public static void VerifyRouting(Action<HttpRouteCollection> registerRoutes, params string[] urls) | |
{ | |
var routes = new HttpRouteCollection(); | |
registerRoutes(routes); | |
var sb = new StringBuilder(); | |
foreach (var url in urls) | |
{ | |
var correctedUrl = "http://localhost" + url.TrimStart('~'); | |
var request = new HttpRequestMessage(HttpMethod.Get, correctedUrl); | |
var route = routes.GetRouteData(request); | |
if (route == null) | |
{ | |
sb.AppendFormat("{0} => {1}\r\n", url, "No route found"); | |
continue; | |
} | |
var values = route.Values ?? new Dictionary<string, object>(); | |
sb.AppendFormat("{0} => {1} \r\n", url, values.ToReadableString()); | |
} | |
Approvals.Verify(sb.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment