Last active
January 16, 2017 21:58
-
-
Save christopherbauer/b179b6bd799e0dfcf73cf45288d08486 to your computer and use it in GitHub Desktop.
How to Unit Test Web API
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
| HttpContext.Current = new HttpContext(new HttpRequest(null, "http://someurl", null), new HttpResponse(new StringWriter())); |
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
| 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