1
1
package io .github .json031 ;
2
2
3
3
import org .junit .jupiter .api .Assertions ;
4
+ import org .springframework .http .*;
4
5
import org .springframework .web .client .RestTemplate ;
5
6
6
7
import java .time .Duration ;
8
+ import java .util .Map ;
7
9
8
10
public class MCApiTests {
9
11
@@ -12,42 +14,88 @@ public class MCApiTests {
12
14
/**
13
15
* 通用测试方法,验证给定 API 是否能在 timeout 秒内响应
14
16
*
15
- * @param url 完整的 API 地址(包括 http/https)
17
+ * @param url 完整的 API 地址(包括 http/https)
18
+ * @param method 请求方式(GET / POST)
19
+ * @param params 请求参数(POST body 或 GET 查询参数)
20
+ * @param headers 请求头(可选)
16
21
* @param timeout 最大允许超时时间(秒)
17
- * @return durationMillis 响应时间(毫秒)
22
+ * @param verbose 是否打印响应
23
+ * @return 响应时间(毫秒)
18
24
*/
19
- public long assertApiRespondsWithinTimeout (String url , long timeout , boolean verbose ) {
20
- return this .assertApiRespondsWithinTimeoutMillis (url , timeout * 1000 , verbose );
25
+ public long assertApiRespondsWithinTimeout (String url ,
26
+ HttpMethod method ,
27
+ Map <String , Object > params ,
28
+ Map <String , String > headers ,
29
+ long timeout ,
30
+ boolean verbose ) {
31
+ return this .assertApiRespondsWithinTimeoutMillis (url , method , params , headers , timeout * 1000 , verbose );
21
32
}
22
33
23
34
/**
24
- * 通用测试方法,验证给定 API 是否能在 timeout 秒内响应
35
+ * 通用测试方法,验证给定 API 是否能在 timeoutMillis 内响应。
25
36
*
26
- * @param url 完整的 API 地址(包括 http/https)
27
- * @param timeoutMillis 最大允许超时时间(毫秒)
28
- * @return durationMillis 响应时间(毫秒)
37
+ * @param url 完整的 API 地址(包括 http/https)
38
+ * @param method 请求方式(GET / POST)
39
+ * @param params 请求参数(POST body 或 GET 查询参数)
40
+ * @param headers 请求头(可选)
41
+ * @param timeoutMillis 超时时间(毫秒)
42
+ * @param verbose 是否打印响应
43
+ * @return 响应时间(毫秒)
29
44
*/
30
- public long assertApiRespondsWithinTimeoutMillis (String url , long timeoutMillis , boolean verbose ) {
45
+ public long assertApiRespondsWithinTimeoutMillis (String url ,
46
+ HttpMethod method ,
47
+ Map <String , Object > params ,
48
+ Map <String , String > headers ,
49
+ long timeoutMillis ,
50
+ boolean verbose ) {
31
51
try {
32
- long start = System .nanoTime (); // 记录开始时间
52
+ HttpHeaders httpHeaders = new HttpHeaders ();
53
+ if (headers != null ) {
54
+ headers .forEach (httpHeaders ::set );
55
+ }
56
+
57
+ HttpEntity <?> entity ;
58
+ String finalUrl = url ;
59
+
60
+ if (method == HttpMethod .GET && params != null && !params .isEmpty ()) {
61
+ // 拼接 GET 参数
62
+ StringBuilder queryBuilder = new StringBuilder (url );
63
+ queryBuilder .append (url .contains ("?" ) ? "&" : "?" );
64
+ params .forEach ((key , value ) -> queryBuilder .append (key ).append ("=" ).append (value ).append ("&" ));
65
+ finalUrl = queryBuilder .substring (0 , queryBuilder .length () - 1 ); // 去掉最后一个 &
66
+ entity = new HttpEntity <>(httpHeaders );
67
+ } else {
68
+ // POST 请求体(可为 null)
69
+ entity = new HttpEntity <>(params , httpHeaders );
70
+ }
33
71
34
- String response = this . restTemplate . getForObject ( url , String . class );
72
+ long start = System . nanoTime ( );
35
73
36
- long end = System .nanoTime (); // 记录结束时间
74
+ ResponseEntity <String > response = restTemplate .exchange (
75
+ finalUrl ,
76
+ method ,
77
+ entity ,
78
+ String .class
79
+ );
80
+
81
+ long end = System .nanoTime ();
37
82
long durationMillis = (end - start ) / 1_000_000 ;
83
+
38
84
if (verbose ) {
39
- System .out .println ("API Response: " + response );
85
+ System .out .println ("API Response: " + response . getBody () );
40
86
System .out .println ("Response time: " + durationMillis + " ms" );
41
87
}
42
88
43
- if (durationMillis >= timeoutMillis ) {
44
- Assertions .fail ("API did not respond within " + timeoutMillis + " milliseconds , url: " +url );
89
+ if (durationMillis > timeoutMillis ) {
90
+ Assertions .fail ("API did not respond within " + timeoutMillis + " ms , url: " + finalUrl );
45
91
}
46
92
47
93
return durationMillis ;
94
+
48
95
} catch (Exception e ) {
49
- Assertions .fail ("Connection refused when trying to access : " + url );
96
+ Assertions .fail ("API call failed for: " + url + " with error : " + e . getMessage () );
50
97
}
98
+
51
99
return -1 ;
52
100
}
53
101
}
0 commit comments