用的 okhttp3 框架,首先封装四种请求方法
import net.sf.json.JSONObject;
import okhttp3.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
class TestMethod {
private static String baseUrl = "http://192.168.3.149:8080";
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static OkHttpClient client = new OkHttpClient();
//post请求
static Object doPost(String url, String json, String cookie) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(baseUrl + url)
.addHeader("Cookie", cookie)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return JSONObject.fromObject(response.body().string());
}
}
//get请求
static Object doGet(String url, HashMap<String, Object> param, String cookie) throws IOException {
//拼接url字符串
if (param != null) {
Iterator<String> it = param.keySet().iterator();
StringBuffer sb = null;
while (it.hasNext()) {
String key = it.next();
Object value = param.get(key);
if (sb == null) {
sb = new StringBuffer();
sb.append("?");
} else {
sb.append("&");
}
sb.append(key);
sb.append("=");
sb.append(value);
}
url += sb.toString();
}
Request request = new Request.Builder()
.url(baseUrl + url)
.addHeader("Cookie", cookie)
.get()
.build();
try (Response response = client.newCall(request).execute()) {
return JSONObject.fromObject(response.body().string());
}
}
//put请求
static Object doPut(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(baseUrl + url)
.put(body)
.build();
try (Response response = client.newCall(request).execute()) {
return JSONObject.fromObject(response.body().string());
}
}
//delete请求
static Object doDelete(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(baseUrl + url)
.delete(body)
.build();
try (Response response = client.newCall(request).execute()) {
return JSONObject.fromObject(response.body().string());
}
}
}
接下来是做 cookie 获取,把 cookie 保存到本地,在需要传递 cookie 的方法里调用
import okhttp3.*;
import java.io.IOException;
class Cookie {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static OkHttpClient client = new OkHttpClient();
static String cookieManage() throws IOException {
String json = "{\"username\":\"huangshayang@supeq.com\",\"password\":\"123456\"}";
RequestBody body = RequestBody.create(JSON, json);
String cookieUrl = "http://192.168.3.149:8080/api/v1/login";
Request request = new Request.Builder()
.url(cookieUrl)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.header("Set-Cookie");
}
}
}
实际例子:登录请求和退出请求
1.登录请求不需要 cookie,所以不传
import net.sf.json.JSONObject;
import java.io.IOException;
public class LoginTest {
private static String loginUrl = "/api/v1/login";
//成功登录
public static JSONObject loginSuccess() throws IOException {
String json = "{\"username\":\"huangshayang@supeq.com\",\"password\":\"123456\"}";
return TestMethod.doPost(loginUrl, json, null);
}
}
2.退出请求需要 cookie
import net.sf.json.JSONObject;
import java.io.IOException;
public class LogoutTest {
private static String logoutUrl = "/api/v1/logout";
//成功退出
public static JSONObject logoutSuccess() throws IOException {
return TestMethod.doGet(logoutUrl, null, Cookie.cookieManage());
}
}
最后 junit 里做断言结果判断
import com.company.LoginTest;
import com.company.LogoutTest;
import net.sf.json.JSONObject;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class ApiTest {
@Test
public void testloginSuccess() throws IOException {
JSONObject result = LoginTest.loginSuccess();
assertEquals(200, Integer.parseInt(result.getString("status")));
assertEquals("Operation is successful.", result.getString("message"));
}
@Test
public void testlogoutSuccess() throws IOException {
JSONObject result = LogoutTest.logoutSuccess();
assertEquals(200, Integer.parseInt(result.getString("status")));
assertEquals("Operation is successful.", result.getString("message"));
}
}