JasperFx / alba

Easy integration testing for ASP.NET Core applications
https://jasperfx.github.io/alba
Apache License 2.0
407 stars 39 forks source link

404s when using Api Versioning #52

Open joemcbride opened 6 years ago

joemcbride commented 6 years ago

If you setup your ASP.NET Core project with Api Versioning, Alba will give 404s for nearly all routes.

I tracked this down to the following line of code from this sample:

app.UseSwaggerUI(
  options =>
  {
      // build a swagger endpoint for each discovered API version
      foreach ( var description in provider.ApiVersionDescriptions )
      {
          options.SwaggerEndpoint( $"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant() );
      }
  } );

IApiVersionDescriptionProvider implementation appears to lazy-load the route/action information. When that code is run in Alba, it does appear to have the correct versioning information. However, doing a get request through Alba you get a 404. If you comment out the foreach then Alba works fine.

This seems to be some sort of timing issue. My only other guess is that the route versioning information is somehow not loaded properly when that class is initialized. The versioning appears to use an IStartupFilter to register route information, as shown here. Think that could possibly be related?

sealed class InjectApiVersionRoutePolicy : IStartupFilter
{
    readonly IApiVersionRoutePolicy routePolicy;

    public InjectApiVersionRoutePolicy( IApiVersionRoutePolicy routePolicy ) => this.routePolicy = routePolicy;

    public Action<IApplicationBuilder> Configure( Action<IApplicationBuilder> next )
    {
        Contract.Requires( next != null );
        Contract.Ensures( Contract.Result<Action<IApplicationBuilder>>() != null );

        return app =>
        {
            next( app );
            app.UseRouter( builder => builder.Routes.Add( routePolicy ) );
        };
    }
}

IStartupFilter's do appear to be called (I wrote a custom one to see that it did). Any ideas @jeremydmiller ?