3
3
import com .fasterxml .jackson .databind .ObjectMapper ;
4
4
import io .github .json031 .JavaBean .RequestUnitTestsResult ;
5
5
import org .junit .jupiter .api .Assertions ;
6
+ import org .springframework .http .HttpHeaders ;
7
+ import org .springframework .http .MediaType ;
8
+ import org .springframework .http .ResponseEntity ;
6
9
7
10
import java .net .URL ;
8
11
@@ -13,17 +16,22 @@ public class DataUnitTests {
13
16
14
17
/**
15
18
* 验证是否为有效json格式数据。
16
- * @param json 数据
19
+ * @param response api请求结果
17
20
* @return 是否为有效json格式数据。
18
21
*/
19
- public static Boolean isValidJSON (String json ) {
20
- // 用 Jackson 尝试解析 JSON
21
- ObjectMapper mapper = new ObjectMapper ();
22
- try {
23
- mapper .readTree (json ); // 验证数据是否为合法JSON格式数据
24
- return true ;
25
- } catch (Exception e ) {
26
- Assertions .fail ("Invalid JSON data: " + json );
22
+ public static <T > Boolean isValidJSON (ResponseEntity <T > response ) {
23
+ if (DataUnitTests .isJSONContentType (response )) {
24
+ String json = (String ) response .getBody ();
25
+ // 用 Jackson 尝试解析 JSON
26
+ ObjectMapper mapper = new ObjectMapper ();
27
+ try {
28
+ mapper .readTree (json ); // 验证数据是否为合法JSON格式数据
29
+ return true ;
30
+ } catch (Exception e ) {
31
+ Assertions .fail ("Invalid JSON data: " + json );
32
+ return false ;
33
+ }
34
+ } else {
27
35
return false ;
28
36
}
29
37
}
@@ -69,4 +77,53 @@ public static Boolean isValidUrl(String url) {
69
77
}
70
78
}
71
79
80
+ /**
81
+ * 获取getMediaType
82
+ * @param result api请求结果
83
+ * @return MediaType
84
+ */
85
+ public static MediaType getMediaType (RequestUnitTestsResult result ) {
86
+ return DataUnitTests .getMediaType (result .response );
87
+ }
88
+ /**
89
+ * 获取getMediaType
90
+ * @param response api请求结果
91
+ * @return MediaType
92
+ */
93
+ public static <T > MediaType getMediaType (ResponseEntity <T > response ) {
94
+ HttpHeaders responseHeaders = response .getHeaders ();
95
+ MediaType contentType = responseHeaders .getContentType ();
96
+ return contentType ;
97
+ }
98
+ /**
99
+ * ContentType是否为application/json
100
+ * @param response api请求结果
101
+ * @return Boolean 是否为application/json
102
+ */
103
+ public static <T > Boolean isJSONContentType (ResponseEntity <T > response ) {
104
+ MediaType contentType = DataUnitTests .getMediaType (response );
105
+ return DataUnitTests .isJSONContentType (contentType );
106
+ }
107
+ /**
108
+ * ContentType是否为application/json
109
+ * @param result api请求结果
110
+ * @return Boolean 是否为application/json
111
+ */
112
+ public static Boolean isJSONContentType (RequestUnitTestsResult result ) {
113
+ MediaType contentType = DataUnitTests .getMediaType (result .response );
114
+ return DataUnitTests .isJSONContentType (contentType );
115
+ }
116
+ /**
117
+ * ContentType是否为application/json
118
+ * @param contentType api请求结果response.headers.contentType
119
+ * @return Boolean 是否为application/json
120
+ */
121
+ public static Boolean isJSONContentType (MediaType contentType ) {
122
+ if (contentType != null ) {
123
+ if (MediaType .APPLICATION_JSON .includes (contentType )) {
124
+ return true ;
125
+ }
126
+ }
127
+ return false ;
128
+ }
72
129
}
0 commit comments