1)网站host地址:https://reqres.in/
2)用户展示请求方式是: Get
3)接口的url 是: /api/users
4)接口的响应状态码是200,还可以看到响应body的JSON内容。
1.封装回去请求地址的方法
package com.automation.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class TestBase {
public Properties props ;
public TestBase() {
try {
props = new Properties();
FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\java\\com\\qa\\config\\config.properties");
props.load(fis);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir")); // 这个方法就是找到 项目的绝对路径的位置。
}
}
2封装一个获取请求头的方法
package com.qa.restclient;
import java.io.IOException;
import java.util.HashMap;
import org.apache.http.Header;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.json.JSONString;
public class RestClient {
public void get(String url) throws Exception {
//创建一个可关闭的HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个HttpGet的请求对象
HttpGet get = new HttpGet(url);
//执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
CloseableHttpResponse httpResponse = httpClient.execute(get);
//获取响应的代码
int statusCode = httpResponse.getStatusLine().getStatusCode();
System.out.println("获取响应的代码"+statusCode);
//把响应的内容存在字符串中
String responseString = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
System.out.println(responseString);
//创建Json对象,把上面你的字符串序列化为Json对象
JSONObject jsonObject = new JSONObject(responseString);
System.out.println("respon json from API-->" + jsonObject);
//获取响应头信息,返回是一个数组
Header[] headerArray = httpResponse.getAllHeaders();
//创建一个hashmap对象,通过postman可以看到请求响应头信息都是Key和value得形式,所以我们想起了HashMap
HashMap<String, String> hm = new HashMap<String, String>();
//增强for循环遍历headerArray数组,依次把元素添加到hashmap集合
for(Header header : headerArray) {
hm.put(header.getName(), header.getValue());
}
//打印hashmap
System.out.println("response headers -->"+ hm);
}
public static void main(String[] args) {
RestClient client = new RestClient();
try {
client.get("https://reqres.in/api/users");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在通过testng进行测试
package com.qa.tests;
import javax.management.MXBean;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.automation.test.TestBase;
import com.qa.restclient.RestClient;
public class GetApiTest extends TestBase {
private String url;
private RestClient restclient;
private GetApiTest apiTest;
@BeforeClass
public void setUp() {
apiTest = new GetApiTest();
url = (String) apiTest.props.get("HOST");
System.out.println(url);
}
@Test
public void test() throws Exception {
restclient = new RestClient();
restclient.get(url + "/api/users");
}
}