Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set dummy IP address in integration test with Asp.Net Core TestServer

Tags:

I have a C# Asp.Net Core (1.x) project, implementing a web REST API, and its related integration test project, where before any test there's a setup similar to:

// ...  IWebHostBuilder webHostBuilder = GetWebHostBuilderSimilarToRealOne()     .UseStartup<MyTestStartup>();  TestServer server = new TestServer(webHostBuilder); server.BaseAddress = new Uri("http://localhost:5000");  HttpClient client = server.CreateClient();  // ... 

During tests, the client is used to send HTTP requests to web API (the system under test) and retrieve responses.

Within actual system under test there's some component extracting sender IP address from each request, as in:

HttpContext httpContext = ReceiveHttpContextDuringAuthentication();  // edge cases omitted for brevity string remoteIpAddress = httpContext?.Connection?.RemoteIpAddress?.ToString() 

Now during integration tests this bit of code fails to find an IP address, as RemoteIpAddress is always null.

Is there a way to set that to some known value from within test code? I searched here on SO but could not find anything similar. TA

like image 976
superjos Avatar asked Mar 12 '18 20:03

superjos


2 Answers

You can write middleware to set custom IP Address since this property is writable:

public class FakeRemoteIpAddressMiddleware {     private readonly RequestDelegate next;     private readonly IPAddress fakeIpAddress = IPAddress.Parse("127.168.1.32");      public FakeRemoteIpAddressMiddleware(RequestDelegate next)     {         this.next = next;     }      public async Task Invoke(HttpContext httpContext)     {         httpContext.Connection.RemoteIpAddress = fakeIpAddress;          await this.next(httpContext);     } } 

Then you can create StartupStub class like this:

public class StartupStub : Startup {     public StartupStub(IConfiguration configuration) : base(configuration)     {     }      public override void Configure(IApplicationBuilder app, IHostingEnvironment env)     {         app.UseMiddleware<FakeRemoteIpAddressMiddleware>();         base.Configure(app, env);     } } 

And use it to create a TestServer:

new TestServer(new WebHostBuilder().UseStartup<StartupStub>()); 
like image 195
Pavel Agarkov Avatar answered Sep 20 '22 10:09

Pavel Agarkov


As per this answer in ASP.NET Core, is there any way to set up middleware from Program.cs?

It's also possible to configure the middleware from ConfigureServices, which allows you to create a custom WebApplicationFactory without the need for a StartupStub class:

public class CustomWebApplicationFactory : WebApplicationFactory<Startup> {     protected override IWebHostBuilder CreateWebHostBuilder()     {         return WebHost             .CreateDefaultBuilder<Startup>(new string[0])             .ConfigureServices(services =>             {                 services.AddSingleton<IStartupFilter, CustomStartupFilter>();             });     } }   public class CustomStartupFilter : IStartupFilter {     public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)     {         return app =>         {             app.UseMiddleware<FakeRemoteIpAddressMiddleware>();             next(app);         };     } } 
like image 29
Elliott Avatar answered Sep 18 '22 10:09

Elliott