基于spring boot的接口自动化工具(二)

文章接上篇:基于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;  
        }  
    }  
   
}

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值