2

I am trying to write an integration test where I test the signup functionality of my application. This is the controller:

[Route("account")]
public class IdentityController : MainController
{
    // ...

    [HttpGet("signup")]
    public IActionResult SignUp()
    {
        return View();
    }

    [HttpPost("signup")]
    public async Task<IActionResult> SignUp(UserSignUpViewModel signUp)
    {
        if (!ModelState.IsValid)
        {

I am following an online training, and according to the video as well as other examples around, this is how it was supposed to be done to be able to test the form submission:

[Fact]
public async Task Identity_CreateUser_ShouldBeSuccessful()
{
    // Arrange
    var initialResponse = await _fixture.Client.GetAsync("/account/signup");
    initialResponse.EnsureSuccessStatusCode();

    var antiForgeryToken = _fixture.GetAntiForgeryToken(await initialResponse.Content.ReadAsStringAsync());

    var postRequest = new HttpRequestMessage(HttpMethod.Post, "/account/signup")
    {
        Content = new FormUrlEncodedContent(new Dictionary<string, string>
        {
            { "Name", "John Malone Doe" },
            // ...
        }
    }

    // Act
    var response = await _fixture.Client.SendAsync(postRequest);

    // ...

Rider will even auto-complete the path for me, But it will fail with a message saying:

System.InvalidOperationException: An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.

I tried passing the full address, "https://localhost:5001/account/signup", and also writing the code this way:

var postRequest = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    RequestUri = new Uri("https://localhost:5001/account/signup"),
    Content = new FormUrlEncodedContent(new Dictionary<string, string>
    {

None solved.

  • Are you sure there is a server listening at https localhost:5001 ? You may try to open this URL in browser to check – Renat Dec 23 '21 at 20:35
  • It might be because you are using the HTTPS protocol. Localhost usually runs on HTTP. Rather than `"https://localhost:5001/account/signup"`, try `"http://localhost:5001/account/signup"` (no 's' after HTTP). – Brendan R. Dec 23 '21 at 20:37
  • @Renat, yes I am. I started the servers and manually tested everything. –  Dec 23 '21 at 20:52
  • @BrendanR, You see, the `initialResponse` code works fine, and it has the same endpoint as it can be seen at the top of the question. Also, if I enter a non-existing endpoint for `HttpRequestMessage`, the test will fail saying it was not found. I believe it is something else, but let me try adjusting it here to use HTTP only. –  Dec 23 '21 at 20:59
  • @BrendanR., It persists! –  Dec 23 '21 at 21:07
  • This, `new Uri("https://localhost:5001/account/signup")` is a valid Uri and should work. I've tested using the BaseAddress and it works if the base Uri is `"https://localhost:5001"` and the RequestUri is `"/account/signup"`. Could it be something in your fixture/moq? Some links that may help.. https://stackoverflow.com/questions/43809746/an-invalid-request-uri-was-provided-the-request-uri-must-either-be-an-absolute .. and ... depending on how you setup the client you may be overriding the BaseAddress, https://stackoverflow.com/questions/23438416/why-is-httpclient-baseaddress-not-working – quaabaam Dec 23 '21 at 22:08

1 Answers1

0

It seems the URIs need a trailing slash to be valid

See: Create new URI from Base URI and Relative Path - slash makes a difference?

Rather than https://localhost:5001/account/signup, try https://localhost:5001/account/signup/.

(There is a slash at the end of the second)

I haven’t tested this though.

Brendan R.
  • 113
  • 1
  • 6