Last active
August 29, 2015 14:25
-
-
Save JayBazuzi/33682f3b9afad30efbcf 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
public class HttpRequestMessageList : List<HttpRequestMessage> | |
{ | |
public void Add(string uri, HttpMethod httpMethod) | |
{ | |
this.Add(new HttpRequestMessage(httpMethod, uri)); | |
} | |
} |
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
/// <param name="registerWebApiConfigFn">Probably <c>WebApiConfig.Register</c></param> | |
public static void VerifyRouting(Action<HttpConfiguration> registerWebApiConfigFn, | |
HttpRequestMessageList httpRequestMessageList) | |
{ | |
var config = new HttpConfiguration(); | |
registerWebApiConfigFn(config); | |
config.EnsureInitialized(); | |
var stringBuilder = new StringBuilder(); | |
foreach (var request in httpRequestMessageList) | |
{ | |
stringBuilder.Append(GetRoutingForRequest(request, config)); | |
stringBuilder.AppendLine(); | |
} | |
Approvals.Verify(stringBuilder.ToString()); | |
} | |
private static void RemoveOptionalRoutingParameters(IDictionary<string, object> routeValues) | |
{ | |
var optionalParams = routeValues | |
.Where(x => x.Value == RouteParameter.Optional) | |
.Select(x => x.Key) | |
.ToList(); | |
foreach (var key in optionalParams) | |
{ | |
routeValues.Remove(key); | |
} | |
} | |
private static string GetRoutingForRequest(HttpRequestMessage request, HttpConfiguration config) | |
{ | |
StringBuilder stringBuilder = new StringBuilder(); | |
if (!request.RequestUri.IsAbsoluteUri) | |
{ | |
request.RequestUri = | |
new Uri("http://example.com/" + request.RequestUri.OriginalString); | |
} | |
stringBuilder.AppendLine(request.ToString()); | |
var routeData = config.Routes.GetRouteData(request); | |
if (routeData == null) | |
{ | |
stringBuilder.AppendLine("No route found"); | |
return stringBuilder.ToString(); | |
} | |
RemoveOptionalRoutingParameters(routeData.Values); | |
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData; | |
HttpControllerDescriptor httpControllerDescriptor; | |
try | |
{ | |
httpControllerDescriptor = new DefaultHttpControllerSelector(config).SelectController(request); | |
} | |
catch (HttpResponseException ex) | |
{ | |
stringBuilder.AppendLine(string.Format("{0}: {1}", (int)ex.Response.StatusCode, ex.Response.ReasonPhrase)); | |
return stringBuilder.ToString(); | |
} | |
var controllerContext = new HttpControllerContext(config, routeData, request) | |
{ | |
RouteData = routeData, | |
ControllerDescriptor = httpControllerDescriptor | |
}; | |
var actionMapping = new ApiControllerActionSelector().SelectAction(controllerContext); | |
stringBuilder.AppendLine(string.Format("Handled By: {0}.{1}", | |
actionMapping.ControllerDescriptor.ControllerType.FullName, actionMapping.ActionName)); | |
return stringBuilder.ToString(); | |
} |
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
Method: GET, RequestUri: 'http://example.com/api/Values', Version: 1.1, Content: <null>, Headers: | |
{ | |
} | |
Handled By: WebApiGetRouteDataExample.Controllers.ValuesController.Get | |
Method: PUT, RequestUri: 'http://example.com/api/alues/5', Version: 1.1, Content: <null>, Headers: | |
{ | |
} | |
404: Not Found | |
Method: GET, RequestUri: 'http://example.com/DOESNOTEXIST', Version: 1.1, Content: <null>, Headers: | |
{ | |
} | |
No route found | |
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
[TestMethod] | |
public void VerifyRoutingTest() | |
{ | |
VerifyRouting(WebApiConfig.Register, new HttpRequestMessageList | |
{ | |
{"api/Values", HttpMethod.Get}, | |
{"api/alues/5", HttpMethod.Put}, | |
{"DOESNOTEXIST", HttpMethod.Get}, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment