接着上面一篇,这篇来封装下PUT和Delete方法。虽然这两个方法很少用,这篇内容就算了解一下。PUT方法封装完成参考POST方法,Delete方法封装可以参考GET方法。由于弄明白了前面的Get和Post方法封装过程,现在就直接贴出封装方法和测试代码。
1.PUT和Deletet方法
package com.qa.restclient; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; 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.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class RestClient { //1. Get 请求方法 public CloseableHttpResponse get(String url) throws ClientProtocolException, IOException { //创建一个可关闭的HttpClient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); //创建一个HttpGet的请求对象 HttpGet httpget = new HttpGet(url); //执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收 CloseableHttpResponse httpResponse = httpclient.execute(httpget); return httpResponse; } //2. Get 请求方法(带请求头信息) public CloseableHttpResponse get(String url,HashMap<String,String> headermap) throws ClientProtocolException, IOException { //创建一个可关闭的HttpClient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); //创建一个HttpGet的请求对象 HttpGet httpget = new HttpGet(url); //加载请求头到httpget对象 for(Map.Entry<String, String> entry : headermap.entrySet()) { httpget.addHeader(entry.getKey(), entry.getValue()); } //执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收 CloseableHttpResponse httpResponse = httpclient.execute(httpget); return httpResponse; } //3. POST方法 public CloseableHttpResponse post(String url, String entityString, HashMap<String,String> headermap) throws ClientProtocolException, IOException { //创建一个可关闭的HttpClient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); //创建一个HttpPost的请求对象 HttpPost httppost = new HttpPost(url); //设置payload httppost.setEntity(new StringEntity(entityString)); //加载请求头到httppost对象 for(Map.Entry<String, String> entry : headermap.entrySet()) { httppost.addHeader(entry.getKey(), entry.getValue()); } //发送post请求 CloseableHttpResponse httpResponse = httpclient.execute(httppost); return httpResponse; } //4. Put方法 public CloseableHttpResponse put(String url, String entityString, HashMap<String,String> headerMap) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPut httpput = new HttpPut(url); httpput.setEntity(new StringEntity(entityString)); for(Map.Entry<String, String> entry : headerMap.entrySet()) { httpput.addHeader(entry.getKey(), entry.getValue()); } //发送put请求 CloseableHttpResponse httpResponse = httpclient.execute(httpput); return httpResponse; } //5. Delete方法 public CloseableHttpResponse delete(String url) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpDelete httpdel = new HttpDelete(url); //发送put请求 CloseableHttpResponse httpResponse = httpclient.execute(httpdel); return httpResponse; } }
2.测试代码
1)Put方法测试
package com.qa.tests; import java.io.IOException; import java.util.HashMap; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.util.EntityUtils; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.qa.base.TestBase; import com.qa.data.Users; import com.qa.restclient.RestClient; import com.qa.util.TestUtil; public class PutApiTest extends TestBase { TestBase testBase; String host; String url; RestClient restClient; CloseableHttpResponse closeableHttpResponse; @BeforeClass public void setUp() { testBase = new TestBase(); host = prop.getProperty("HOST"); url = host + "/api/users/2"; } @Test public void putTest() throws ClientProtocolException, IOException{ restClient = new RestClient(); HashMap<String,String> headermap = new HashMap<String,String>(); headermap.put("Content-Type", "application/json"); //这个在postman中可以查询到 //对象转换成Json字符串 Users user = new Users("Anthony","automation tester"); String userJsonString = JSON.toJSONString(user); //System.out.println(userJsonString); closeableHttpResponse = restClient.put(url, userJsonString, headermap); //验证状态码是不是200 int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); Assert.assertEquals(statusCode, RESPNSE_STATUS_CODE_200,"response status code is not 200"); //验证名称为Anthony的jon是不是 automation tester String responseString = EntityUtils.toString(closeableHttpResponse.getEntity()); JSONObject responseJson = JSON.parseObject(responseString); String jobString = TestUtil.getValueByJPath(responseJson, "job"); System.out.println(jobString); Assert.assertEquals(jobString, "automation tester","job is not same"); } }
2) Delete方法测试
package com.qa.tests; import java.io.IOException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.qa.base.TestBase; import com.qa.restclient.RestClient; public class DeleteApiTest extends TestBase { TestBase testBase; String host; String url; RestClient restClient; CloseableHttpResponse closeableHttpResponse; @BeforeClass public void setUp() { testBase = new TestBase(); host = prop.getProperty("HOST"); url = host + "/api/users/2"; //直接在这个网站可以找到delete的api } @Test public void deleteApiTest() throws ClientProtocolException, IOException { restClient = new RestClient(); closeableHttpResponse = restClient.delete(url); int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); System.out.println(statusCode); Assert.assertEquals(statusCode, 204,"status code is not 204"); } }注意到我们写过的TestNG测试用例没,我们发现每次写响应码断言和json内容判断,我们都需要多次重复写这些代码。我们既然看到了重复的代码,就要想办法去消除,抽取出来成公共方法来调用。下篇,我们实现这个优化和实现日志打印功能。