Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Last active January 16, 2017 21:58
Show Gist options
  • Select an option

  • Save christopherbauer/b179b6bd799e0dfcf73cf45288d08486 to your computer and use it in GitHub Desktop.

Select an option

Save christopherbauer/b179b6bd799e0dfcf73cf45288d08486 to your computer and use it in GitHub Desktop.
How to Unit Test Web API
HttpContext.Current = new HttpContext(new HttpRequest(null, "http://someurl", null), new HttpResponse(new StringWriter()));
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Get, "http://host/api/my");
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "my" } });
var controller = new MyController();
controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment