I have an example server
var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run();
with configuration in Startup class:
public void Configure(IApplicationBuilder app) { app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); }
and I use xunit test (learning):
public TestFixture() { var builder = new WebHostBuilder().UseStartup<TStartup>(); _server = new TestServer(builder); Client = _server.CreateClient(); Client.BaseAddress = new Uri(address); }
and later
var response = await Client.GetAsync("http://localhost:51021/"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var responseString = await response.Content.ReadAsStringAsync(); var content = await response.Content.ReadAsStringAsync(); Assert.Contains("Hello World!", content);
Everything is in order (200). Now I am changing Startup.cs
public void Configure(IApplicationBuilder app) { app.UseDefaultFiles(); app.UseStaticFiles(); //app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); //}); }
If I run the application in a browser, everything is fine (index.html is shown). But if I call it using TestServer, I get (404 Not Found). Where is my mistake?
source share