C
C#8mo ago
Gipper

✅ Please help me read this simple API endpoint documentation

So the image I included is a sample of one of the rows in a table that is supposed to be the documentation of the endpoints of an API I have to write Unit and Integration tests for. I do not have access to the code (as it probably doesn't even exist or at least not fully) and I don't know much about how the application will end up in terms of codebase or in terms of final software produced. This documentation is in reference to the API endpoints of the backend of a web app that will be developed in ASP.NET (at least the backend part will be in ASP). What I need help with is in understanding better what an API endpoint is and what it does in this context. Also how that connects with the info on this table. Finally, I also need to know how I can use all that information once I understand it and understand how it all is interrelated to make these unit and integration tests (I'm doing integration right now, cause no code yet).
No description
19 Replies
Gipper
GipperOP8mo ago
As a clarification, I can switch back to doing unit tests if you think even with having no code I can make them with just this info, that just wasn't what I was planning but whatever it's completely fine either way.
Angius
Angius8mo ago
An API endpoint is the code that handles a given HTTP call In this case, a GET call to https://some-website.com/User/[email protected] Which is supposed to return that user's data Their ID, username, and state Something like
// UserController.cs
[Route("[controller]")]
class UserController : ControllerBase
{
[HttpGet("{email}")]
public async Task<IActionResult> GetUserByEmail(string email)
{
var user = await _context.Users
.Where(u => u.Email == email)
.Select(u => new UserDataDto {
Id = u.Id,
Username = u.Username,
State = u.State
})
.FirstOrDefaultAsync();
return user is null ? NotFound() : Ok(user);
}
}
// UserController.cs
[Route("[controller]")]
class UserController : ControllerBase
{
[HttpGet("{email}")]
public async Task<IActionResult> GetUserByEmail(string email)
{
var user = await _context.Users
.Where(u => u.Email == email)
.Select(u => new UserDataDto {
Id = u.Id,
Username = u.Username,
State = u.State
})
.FirstOrDefaultAsync();
return user is null ? NotFound() : Ok(user);
}
}
Angius
Angius8mo ago
No description
Angius
Angius8mo ago
Unit tests without having the code are not really possible Integration tests... are more possible
Gipper
GipperOP8mo ago
Ok, so integration then. I ran this exact same info by chatGPT and asked for integration tests and this is what it said:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.Http;
using System.Threading.Tasks;

namespace YourTestProjectNamespace
{
[TestClass]
public class UserIntegrationTests
{
private readonly HttpClient _httpClient;
private const string BaseUrl = "http://your-api-base-url"; // Update this with your actual API base URL

public UserIntegrationTests()
{
_httpClient = new HttpClient();
}

[TestMethod]
public async Task GetUserById_ReturnsCorrectUser()
{
// Arrange
var userId = "1";
var expectedUser = new { id = "1", username = "Tom", state = "0" };

// Act
var response = await _httpClient.GetAsync($"{BaseUrl}/User/{userId}");
response.EnsureSuccessStatusCode(); // Throw an exception if HTTP status code is not success

var actualUser = await response.Content.ReadAsAsync<dynamic>();

// Assert
Assert.AreEqual(expectedUser.id, actualUser.id);
Assert.AreEqual(expectedUser.username, actualUser.username);
Assert.AreEqual(expectedUser.state, actualUser.state);
}
}
}
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.Http;
using System.Threading.Tasks;

namespace YourTestProjectNamespace
{
[TestClass]
public class UserIntegrationTests
{
private readonly HttpClient _httpClient;
private const string BaseUrl = "http://your-api-base-url"; // Update this with your actual API base URL

public UserIntegrationTests()
{
_httpClient = new HttpClient();
}

[TestMethod]
public async Task GetUserById_ReturnsCorrectUser()
{
// Arrange
var userId = "1";
var expectedUser = new { id = "1", username = "Tom", state = "0" };

// Act
var response = await _httpClient.GetAsync($"{BaseUrl}/User/{userId}");
response.EnsureSuccessStatusCode(); // Throw an exception if HTTP status code is not success

var actualUser = await response.Content.ReadAsAsync<dynamic>();

// Assert
Assert.AreEqual(expectedUser.id, actualUser.id);
Assert.AreEqual(expectedUser.username, actualUser.username);
Assert.AreEqual(expectedUser.state, actualUser.state);
}
}
}
What do you think? If I understand all that is happening can I use it? Is it good code? Can I emulate it for all the other endpoints to the best of my ability?
Angius
Angius8mo ago
This code tries to get the user by ID and not email, and it used dynamic So, no, it immediately fails at doing what it's supposed to do I highly recommend not using LLMs if you're just learning
Gipper
GipperOP8mo ago
Ok, so I tried to adapt what gpt gave me to this, what do you think?
[TestFixture]
public class TestesIntegracaoUser
{
private readonly HttpClient _httpClient;
private const string URLBase = "http://User";

public TestesIntegracaoUser()
{
_httpClient = new HttpClient();
}
[OneTimeTearDown]
public void disposeHTTPClient()
{
_httpClient.Dispose();
}

[TestCase("[email protected]")]
public async Task GetUserByEmail_ReturnsCorrectUser(string email)
{
// Arrange
var userEmail = email;
var expectedUser = new User { email = userEmail };

// Act
var response = await _httpClient.GetAsync($"{URLBase}/User/{userEmail}");
response.EnsureSuccessStatusCode();

var UserReal = await response.Content.ReadAsStringAsync();

// Assert
Assert.AreEqual(expectedUser.id, "1");
Assert.AreEqual(expectedUser.username, "Tom");
Assert.AreEqual(expectedUser.id, "0");
}

}
public class User
{
public string id;
public string username;
public string state;
public string email;
}
[TestFixture]
public class TestesIntegracaoUser
{
private readonly HttpClient _httpClient;
private const string URLBase = "http://User";

public TestesIntegracaoUser()
{
_httpClient = new HttpClient();
}
[OneTimeTearDown]
public void disposeHTTPClient()
{
_httpClient.Dispose();
}

[TestCase("[email protected]")]
public async Task GetUserByEmail_ReturnsCorrectUser(string email)
{
// Arrange
var userEmail = email;
var expectedUser = new User { email = userEmail };

// Act
var response = await _httpClient.GetAsync($"{URLBase}/User/{userEmail}");
response.EnsureSuccessStatusCode();

var UserReal = await response.Content.ReadAsStringAsync();

// Assert
Assert.AreEqual(expectedUser.id, "1");
Assert.AreEqual(expectedUser.username, "Tom");
Assert.AreEqual(expectedUser.id, "0");
}

}
public class User
{
public string id;
public string username;
public string state;
public string email;
}
Angius
Angius8mo ago
You don't use UserReal anywhere So what are you checking here? Not the response from the API that's for sure
Gipper
GipperOP8mo ago
yeah, sorry I had misunderstood:
[TestFixture]
public class TestesIntegracaoUser
{
private readonly HttpClient _httpClient;
private const string URLBase = "http://User";

public TestesIntegracaoUser()
{
_httpClient = new HttpClient();
}
[OneTimeTearDown]
public void disposeHTTPClient()
{
_httpClient.Dispose();
}

[TestCase("[email protected]")]
public async Task GetUserByEmail_ReturnsCorrectUser(string email)
{
// Arrange
var userEmail = email;
var expectedUser = new User { id = "1", username = "Tom", state = "0" };

// Act
var response = await _httpClient.GetAsync($"{URLBase}/User/{userEmail}");
response.EnsureSuccessStatusCode();

var UserReal = await response.Content.ReadAsAsync();

// Assert
Assert.AreEqual(expectedUser.id, UserReal.id);
Assert.AreEqual(expectedUser.username, UserReal.username);
Assert.AreEqual(expectedUser.id, UsrReal.state);
}

}
public class User
{
public string id;
public string username;
public string state;
public string email;
}
[TestFixture]
public class TestesIntegracaoUser
{
private readonly HttpClient _httpClient;
private const string URLBase = "http://User";

public TestesIntegracaoUser()
{
_httpClient = new HttpClient();
}
[OneTimeTearDown]
public void disposeHTTPClient()
{
_httpClient.Dispose();
}

[TestCase("[email protected]")]
public async Task GetUserByEmail_ReturnsCorrectUser(string email)
{
// Arrange
var userEmail = email;
var expectedUser = new User { id = "1", username = "Tom", state = "0" };

// Act
var response = await _httpClient.GetAsync($"{URLBase}/User/{userEmail}");
response.EnsureSuccessStatusCode();

var UserReal = await response.Content.ReadAsAsync();

// Assert
Assert.AreEqual(expectedUser.id, UserReal.id);
Assert.AreEqual(expectedUser.username, UserReal.username);
Assert.AreEqual(expectedUser.id, UsrReal.state);
}

}
public class User
{
public string id;
public string username;
public string state;
public string email;
}
Now the problem is that the ReadAsync() method can't read anything. But perhaps that is unavoidable if there is no real code or app to read anyting from??? I don't know, is it?
Angius
Angius8mo ago
What is ReadAsAsync()? It's a hallucination of the LLM It's not a method that exists
Gipper
GipperOP8mo ago
It's not anything, it's a placeholder. I just left it as that...it's not meant to work because I assume that I would need to have the code or some form of the app to make that call and have it contain something. This is essentially just meant to be a skeleton of the test which will be finalized once I have more to go on and to test
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius8mo ago
No, you don't need anything from the project You know what the API returns
Gipper
GipperOP8mo ago
because NUnit was complaining
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Gipper
GipperOP8mo ago
Ooooh, I didn't understand that's what WebClientFactory was for I do have that file in my proj
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Gipper
GipperOP8mo ago
yeah, mispell
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server