Search code examples
c#restasp.net-coreintegration-testing

ASPNetCore REST API calls triggered unexpectedly


I have a REST API like so:

[ApiController]
[Route("abc/events/v{version:ApiVersion}")]
[ApiVersion("1.0")]
public class ABCController : Controller
{
    [HttpPut("a")]
    [Produces(MediaTypeNames.Application.Json)]
    public ActionResult A(
        [FromBody, BindRequired] AEvent aEvent)
    {
        StatusCodeResult result = Ok();
        // do something with aEvent
        return result;
    }

    [HttpPut("b")]
    [Produces(MediaTypeNames.Application.Json)]
    public ActionResult B(
        [FromBody, BindRequired] BEvent bEvent)
    {
        StatusCodeResult result = Ok();
        // do something with event b
        return result;
    }

    [HttpPut("game")]
    [Produces(MediaTypeNames.Application.Json)]
    public ActionResult C(
        [FromBody, BindRequired] CEvent cEvent)
    {
        StatusCodeResult result = Ok();
        // do something with event c
        return result;
    }
}

I call my API from an Integration test project one of whose tests look like this:

    [TestMethod]
    public async Task Test_Put_A_Event_OK()
    {
        var logger = _container.Resolve<ILogger>();
        var client = _container.Resolve<IMyClientAPI>(
            new NamedParameter("logger", logger),
            new NamedParameter("schema", "http"),
            new NamedParameter("hostname", "localhost"),
            new NamedParameter("hostPort", 5000));
        var result = await client.PutEvent(_AJsonData);
        Assert.IsTrue(result == Result.Success);
    }

The calls these tests make to each of the endpoints occurs correctly. the problem then occurs a second or two after the successfully handled call returns to the client. The server appears to receive a request for each endpoint one after the other (even for those endpoints that have not been requested).

The associated Startup code:

public class Startup
{
    // Location of xml comments for generating
    // additional swagger UI information.
    // Type '///' before any endpoint route function
    // and it will auto-generate the comment structure.
    static string XmlCommentsFilePath
    {
        get
        {
            var exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            return Path.Combine(exePath, "docs", "etsapi.xml");
        }
    }

    public void ConfigureServices(IServiceCollection services)
    {
        var abcService = new ABCService();
        services.AddControllers();
        services.AddApiVersioning();
        services.AddSingleton<IEtsSignageService>(abcService);
        services.AddEndpointsApiExplorer();
        services.AddSwaggerGen(options =>
        {
            options.SwaggerDoc("v1", new OpenApiInfo { Title = "ABCApi", Version = "v1" });
            options.IncludeXmlComments(XmlCommentsFilePath);
        });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();

        // Cors, Authentication and authorization here if needed

        app.UseEndpoints(x => x.MapControllers());
        // Enable middleware to serve generated Swagger as a JSON endpoint.
        if (env.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }
    }
}

Any thoughts on what I may be doing wrong will be greatly appreciated.


Solution

  • The issue I was having was to do with Autofac Dependancy injecting multiple versions of a backend class generating duplicate messages in my queue that issues the the API Requests.