文章接上篇:基于spring boot的接口自动化工具(一)
一、核心功能描述
1.将http接口协议、预期结果等信息放到csv文件中;
2.读取csv文件信息;
3.使用restTemplate发送请求,;
需要封装三个工具类:
RestTemplateUtil----发送请求工具类或者使用httpclient工具类;
CsvParseUtil----解析csv文件;
dbUtil----数据库连接工具类,用于需要将http请求结果与数据库结果进行比较的场景。
二、具体实现demo
1.csv文件 demo
ID,RequestMethod,URL,Headers,RequestBody,ExpectedStatusCode,ExpectedResponseBody
1,GET,https://api.example.com/users,Content-Type: application/json,,200,
2,POST,https://api.example.com/users,Content-Type: application/json; Authorization:
3,PUT,https://api.example.com/users/123,Content-Type: application/json,,200,{"message": "User updated"}
4,DELETE,https://api.example.com/users/456,Content-Type: application/json; Authorization: Bearer xyz789ghi012,,204,
2.读取csv文件工具类
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class HttpApiTest {
@Test
public void testReadHttpRequestInfoFromCsv() throws IOException {
String csvFilePath = "path/to/your/csvfile.csv"; // 替换为实际的CSV文件路径
List<Map<String, String>> httpRequestInfoList = HttpRequestInfoUtil.readHttpRequestInfoFromCsv(csvFilePath);
// 遍历列表,并使用HTTP请求信息进行测试
for (Map<String, String> httpRequestInfo : httpRequestInfoList) {
// 构建请求并发送...
// 断言响应...
}
}
}
3.http请求工具类-1
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClientException;
@Component
public class MyRestTemplateUtil {
private final RestTemplateBuilder restTemplateBuilder;
@Autowired
public MyRestTemplateUtil(RestTemplateBuilder restTemplateBuilder) {
this.restTemplateBuilder = restTemplateBuilder;
}
public <T> T getForObject(String url, Class<T> responseType, Map<String, String> headers) {
RestTemplate restTemplate = restTemplateBuilder.build();
HttpHeaders httpHeaders = new HttpHeaders();
if (headers != null) {
headers.forEach(httpHeaders::set);
}
HttpEntity<?> request = new HttpEntity<>(httpHeaders);
ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.GET, request, responseType);
return response.getBody();
}
public <T, R> R postForObject(String url, T requestBody, Class<R> responseType, Map<String, String> headers) {
RestTemplate restTemplate = restTemplateBuilder.build();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
if (headers != null) {
headers.forEach(httpHeaders::set);
}
HttpEntity<T> request = new HttpEntity<>(requestBody, httpHeaders);
ResponseEntity<R> response = restTemplate.exchange(url, HttpMethod.POST, request, responseType);
return response.getBody();
}
// 你可以根据需要添加其他方法
}
http请求工具类-get
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class HttpClientUtil {
private static final CloseableHttpClient httpClient = HttpClients.createDefault();
public static String get(String url) throws IOException {
HttpGet httpGet = new HttpGet(url);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity, StandardCharsets.UTF_8) : null;
}
}
public static String post(String url, String jsonPayload) throws IOException {
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonPayload, StandardCharsets.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
HttpEntity responseEntity = response.getEntity();
return responseEntity != null ? EntityUtils.toString(responseEntity, StandardCharsets.UTF_8) : null;
}
}
public static String put(String url, String jsonPayload) throws IOException {
HttpPut httpPut = new HttpPut(url);
StringEntity entity = new StringEntity(jsonPayload, StandardCharsets.UTF_8);
entity.setContentType("application/json");
httpPut.setEntity(entity);
try (CloseableHttpResponse response = httpClient.execute(httpPut)) {
HttpEntity responseEntity = response.getEntity();
return responseEntity != null ? EntityUtils.toString(responseEntity, StandardCharsets.UTF_8) : null;
}
}
public static String delete(String url) throws IOException {
HttpDelete httpDelete = new HttpDelete(url);
try (CloseableHttpResponse response = httpClient.execute(httpDelete)) {
HttpEntity responseEntity = response.getEntity();
return responseEntity != null ? EntityUtils.toString(responseEntity, StandardCharsets.UTF_8) : null;
}
}
}