接口测试请求方法封装

用的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"));
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值