| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package space.anyi.httpClient;
- import org.junit.jupiter.api.Test;
- import java.io.IOException;
- import java.net.http.HttpResponse;
- import static org.junit.jupiter.api.Assertions.*;
- /**
- * GET 请求示例的测试类
- *
- * <p>测试依赖本地运行的 User Management API 服务(http://localhost:8080)。</p>
- */
- class GetExampleTest {
- private final GetExample getExample = new GetExample();
- @Test
- void getUsers() throws IOException, InterruptedException {
- // 查询所有用户:状态码应为 200
- HttpResponse<String> response = getExample.getUsers();
- assertEquals(200, response.statusCode());
- // 响应体是标准包装结构,应包含 code 字段
- assertTrue(response.body().contains("\"code\""));
- }
- @Test
- void getUserById_notExists() throws IOException, InterruptedException {
- // 使用一个不存在的 id(例如 999999999),服务应返回 404
- HttpResponse<String> response = getExample.getUserById(999999999L);
- assertEquals(404, response.statusCode());
- }
- @Test
- void buildRequestWithQuery() {
- // 验证带查询参数的请求对象可以正常构建,URI 拼接正确
- var request = getExample.buildRequestWithQuery("alice01");
- assertEquals("http://localhost:8080/api/users?account=alice01",
- request.uri().toString());
- // GET 方法的 HttpRequest 不应包含请求体
- assertEquals(null, request.bodyPublisher().orElse(null));
- }
- }
|