0

I'm using an ASP.NET minimal API which works fine when launching normally, but when I try to reuse the Program class from my integration tests there is one endpoint that fails in a custom middleware.

I used the trick from https://github.com/DamianEdwards/MinimalApiPlayground to access the Program class from a test project. That is this line at the end of Program.cs:

public partial class Program { }

I then have a factory class with no content that uses the Program class :

internal class ProductionWebApiTesterFactory : WebApplicationFactory<Program> {}

and finally I use it as follows :

await using var factory = new ProductionWebApiTesterFactory();
using HttpClient client = factory.CreateDefaultClient();

This way I can have a client that allows access directly on to my API.

Except in this configuration, I have the follow problem in a custom authentication middleware.

enter image description here

There are other methods on the same controller that work correctly that are all defined in much the same way. I don't see what is different.

[Route("is-pfd/api/viewer")]
[ApiController]
[AllowAnonymous]
public class DocuViewareController : ControllerBase
{
    // 405
    [HttpPost("baserequest")]
    public string baserequest([FromBody] object jsonString)
    {
        return DocuViewareControllerActionsHandler.baserequest(jsonString);
    }

    // OK
    [HttpGet("getresource")]
    public IActionResult GetResource(string resourceID, string version)
    {
        DocuViewareControllerActionsHandler.getresource(resourceID, version, out HttpStatusCode statusCode, out byte[] content, out string contentType, out string fileName, out string reasonPhrase);
        if (statusCode == HttpStatusCode.OK)
        {
            return File(content, contentType, fileName);
        }
        else
        {
            return StatusCode((int)statusCode, reasonPhrase);
        }
    }
    ///...
}

Any ideas?

Thanking you in advance, Loren

dakotapearl
  • 373
  • 1
  • 15

1 Answers1

1

the examples(examples.cs) in the link you provided are all for GET endpoints.

For POST endpoints you shouldn'y simply try with client.GetAsync("uri"); just like you send a GET request with postman to POST endpoint,you would get 405 error

The error I reproduced:

enter image description here

You could check this document to learn sending POST request with httpclient

It doesn't throw 405 error on my side now:

enter image description here

received the parameter successfully

enter image description here

If you still have problem ,please provide a minimal example that could reproduce the issue

Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11
  • Yes, perfect. You're exactly right, thank you. In fact I had a proxy (Titanium) in place for my secondary resource calls and the proxy only handled get verbs. I completely assume responsibility :D – dakotapearl May 05 '23 at 09:04