lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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());
    }
}