Skip to content

Instantly share code, notes, and snippets.

@cromwellryan
Created February 29, 2012 16:19
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 cromwellryan/1942111 to your computer and use it in GitHub Desktop.
Save cromwellryan/1942111 to your computer and use it in GitHub Desktop.
SelfHostWebApiTests
[TestFixture]
public class MyWebApiIntegrationTests
{
[Test]
public void ShouldWorkWithWebApi4()
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
"Api",
"api/{controller}/{id}",
new { id = RouteParameter.Optional, folder = "" }
);
new HttpSelfHostServer(config)
.OpenAsync()
.ContinueWith(task =>
{
var request = WebRequest.Create("http://localhost:8080/api/Things");
var response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
reader.ReadToEnd().Should().Contain("Boo");
})
.Wait();
}
@neontapir
Copy link

Are you sure this code is doing what you expected? When I tried it with a base address of http://in-memory (an invalid address on my network), I got an error:

System.AggregateException : One or more errors occurred.
  ----> System.Net.WebException : The remote name could not be resolved: 'in-memory'

This suggests to me that WebRequest is actually not communicating with your self-host server. I've been successfully using HttpClient and HttpRequestMessage to work around this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment