用于像 Postman 一样测试接口,无需像传统测试时,启动 Spirng 来调用 Service
private final String URL = "https://depp.wang";
@Test
void testGet() throws Exception {
String result = HttpClientHelper.get(URL);
logger.info("[result: {}]", result);
}
- application/x-www-form-urlencoded
@Test
@DisplayName("post application/x-www-form-urlencoded")
public void testPostForm() throws Exception {
Map<String, Object> form = new HashMap<>();
form.put("userId", USER_ID);
form.put("address", "3/151 Hartley Road, Smeaton Grange, New South Wales, Australia");
String url = URL + "/profile/v1/address/update";
String result = HttpClientHelper.postForm(url, form);
logger.info("[result: {}]", result);
}
- application/json
@Test
@DisplayName("post application/json")
public void testPostJson() throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.set("pid", "31502");
String url = URL + "/profile/v1/address/update";
String result = HttpClientHelper.postJSON(url, jsonObject.toString());
logger.info("[result: {}]", result);
}
本质上还是使用 HttpClient 来实现的
mvn clean package