(ApiDataBean apiDataBean)
| 126 | } |
| 127 | |
| 128 | @Test(dataProvider = "apiDatas") |
| 129 | public void apiTest(ApiDataBean apiDataBean) throws Exception { |
| 130 | String apiParam = buildRequestParam(apiDataBean); |
| 131 | |
| 132 | // 封装请求方法 |
| 133 | HttpUriRequest method = parseHttpRequest(apiDataBean.getUrl(), |
| 134 | apiDataBean.getMethod(), apiParam); |
| 135 | HttpClient client = new SSLClient(); |
| 136 | client.getParams().setParameter( |
| 137 | CoreConnectionPNames.CONNECTION_TIMEOUT, 60000); // 请求超时 |
| 138 | client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000); // 读取超时 |
| 139 | |
| 140 | // 执行 |
| 141 | HttpResponse response = client.execute(method); |
| 142 | int reponseStatus = response.getStatusLine().getStatusCode(); |
| 143 | if (StringUtil.isNotEmpty(apiDataBean.getStatus())) { |
| 144 | Assert.assertEquals(reponseStatus, apiDataBean.getStatus(), |
| 145 | "返回状态码与预期不符合!"); |
| 146 | } else { |
| 147 | // 非2开头状态码为异常请求,抛异常后会进行重跑 |
| 148 | if (200 > reponseStatus || reponseStatus >= 300) { |
| 149 | throw new ErrorRespStatusException("返回状态码异常:" + reponseStatus); |
| 150 | } |
| 151 | } |
| 152 | HttpEntity respEntity = response.getEntity(); |
| 153 | String responseData; |
| 154 | Header respContenType = response.getFirstHeader("Content-Type"); |
| 155 | if (respContenType != null |
| 156 | && respContenType.getValue().contains("download")) { |
| 157 | String conDisposition = response.getFirstHeader( |
| 158 | "Content-disposition").getValue(); |
| 159 | String fileType = conDisposition.substring( |
| 160 | conDisposition.lastIndexOf("."), conDisposition.length()); |
| 161 | String filePath = "download/" + RandomUtil.getRandom(8, false) |
| 162 | + fileType; |
| 163 | InputStream is = response.getEntity().getContent(); |
| 164 | Assert.assertTrue(FileUtil.writeFile(is, filePath), "下载文件失败。"); |
| 165 | // 将下载文件的路径放到{"filePath":"xxxxx"}进行返回 |
| 166 | responseData = "{\"filePath\":\"" + filePath + "\"}"; |
| 167 | } else { |
| 168 | responseData = DecodeUtil.decodeUnicode(EntityUtils |
| 169 | .toString(respEntity)); |
| 170 | } |
| 171 | |
| 172 | // 输出返回数据log |
| 173 | ReportUtil.log("resp:" + responseData); |
| 174 | // 验证预期信息 |
| 175 | verifyResult(responseData, apiDataBean.getVerify(), |
| 176 | apiDataBean.isContains()); |
| 177 | |
| 178 | // 对返回结果进行提取保存。 |
| 179 | saveResult(responseData, apiDataBean.getSave()); |
| 180 | |
| 181 | } |
| 182 | |
| 183 | private String buildRequestParam(ApiDataBean apiDataBean) { |
| 184 | // 分析处理预参数 (函数生成的参数) |
nothing calls this directly
no test coverage detected