Skip to content

Instantly share code, notes, and snippets.

@jamesrcounts
Created September 8, 2014 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesrcounts/d718d600d9723a1cccb3 to your computer and use it in GitHub Desktop.
Save jamesrcounts/d718d600d9723a1cccb3 to your computer and use it in GitHub Desktop.
WebApi Route Test
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