package com.rongyichuang;
|
|
import org.junit.jupiter.api.Test;
|
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.MediaType;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.test.context.TestPropertySource;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@TestPropertySource(properties = {
|
"server.servlet.context-path="
|
})
|
public class GraphQLEndpointTest {
|
|
@LocalServerPort
|
private int port;
|
|
private final TestRestTemplate restTemplate = new TestRestTemplate();
|
|
@Test
|
public void testGraphQLEndpoint() {
|
// 测试简单的GraphQL查询
|
Map<String, String> requestBody = new HashMap<>();
|
requestBody.put("query", "{ hello }");
|
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
HttpEntity<Map<String, String>> entity = new HttpEntity<>(requestBody, headers);
|
|
String url = "http://localhost:" + port + "/graphql";
|
System.out.println("测试GraphQL端点: " + url);
|
|
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
|
|
System.out.println("响应状态码: " + response.getStatusCode());
|
System.out.println("响应体: " + response.getBody());
|
}
|
}
|