Xunit integration testing for Post gives bad request

OmarBouattour

New member
Joined
Mar 16, 2022
Messages
3
Programming Experience
Beginner
Hello I'm learning integration testing and I want to test a 'POST' method from my controller using xunit and WebApplicationFactory But I'm getting this exception

System.Net.Http.HttpRequestException : Response status code does not indicate success: 400 (Bad Request).

This is my Test:

C#:
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace integrationtest.IntegrationTests;
public class ControllerTest  : IClassFixture<TestingWebAppFactory<Program>>
{
    private readonly HttpClient _client;
    public ControllerTest(TestingWebAppFactory<Program> factory)
        => _client = factory.CreateClient();
   
    [Fact]
    public async Task AddInternshipMemberTest()
    {
        var postRequest = new HttpRequestMessage(HttpMethod.Post, "/api/InternshipMember/AddInternshipMember");
       
        var formModel = new Dictionary<string, string>
        {
           { "var1", "1"},
           { "var2", "0"},
           { "var3", "135"},
           { "var4", "1"},
           { "var5", "1"},
           { "var6", "1"},
           { "var7", "3" }
        };
        postRequest.Content = new FormUrlEncodedContent(formModel);
        postRequest.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
        var response = await _client.SendAsync(postRequest);
        response.EnsureSuccessStatusCode();
        var responseString = await response.Content.ReadAsStringAsync();
        Assert.Contains("1", responseString);
    }
}

Can anyone help why i'm getting that error?
 
Last edited by a moderator:
You are encoding the body as form URL data, but setting the content header to say that the data is encoded as JSON. Why?
 
Perhaps use the JSON content encoder instead? That way you are truly passing in JSON data instead of telling the API that you are passing a JSON string, but you are actually passing a form URL encoded string.

The real answer is get the documentation for the API you are calling. Determine how it is expecting to get its input. Then write your code so that it conforms to that APIs requirements.
 
Perhaps use the JSON content encoder instead? That way you are truly passing in JSON data instead of telling the API that you are passing a JSON string, but you are actually passing a form URL encoded string.

The real answer is get the documentation for the API you are calling. Determine how it is expecting to get its input. Then write your code so that it conforms to that APIs requirements.
Can you please help me with the code, couldnt find what is JSON content encoder, this is the first time i'm using integration testing
 
this is the first time i'm using integration testing
The issue here is orthogonal to integration testing. You would have the same kind of problem trying to call any other REST API not within your own application.
 
Back
Top Bottom