I'm trying to test my .NET Core REST api using WebClientFactory
method indicated in this link. When I test the GET
method everything goes well, but I don't know why when I send data through a PostAsync
method it arrives as null
.
Here is the Test code:
public class TravelAgentEndToEndTest : IClassFixture<WebApplicationFactory<Startup>>
{
private readonly WebApplicationFactory<Startup> factory;
private readonly HttpClient httpClient;
public TravelAgentEndToEndTest(WebApplicationFactory<Startup> factory)
{
this.factory = factory;
this.httpClient = factory.CreateClient();
}
[Fact]
public async Task TravelAgentReceivesInvalidClientDataAndRejectsIt()
{
string emptyClientData = "Bad";
string url = "/api/clients";
HttpResponseMessage response = await httpClient.PostAsync(url,
new StringContent(emptyClientData,Encoding.UTF8, "application/json"));
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
}
the POST method in my controller:
[HttpPost]
public void Post([FromBody]string value)
{
Console.WriteLine(value);
}
When I debug the test I see that value
is null
.
Any idea where am I making a mistake?
PS1: I tried to test the post method using Postman and it works.
PS2: The Startup class is as Visual studio created it using Web Api template under Asp.NETCore web project