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.
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
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:
You could check this document to learn sending POST request with httpclient
It doesn't throw 405 error on my side now:
received the parameter successfully
If you still have problem ,please provide a minimal example that could reproduce the issue