protobuf 接口测试

你是否遇到过 protobuf 接口难易测试?可以按照以下代码去实现(部分工具代码未贴出可以自己实现):


import com.google.protobuf.ByteString;
import com.google.protobuf.GeneratedMessageV3;
import com.googlecode.protobuf.format.JsonFormat;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.cglib.reflect.FastClass;
import org.springframework.cglib.reflect.FastMethod;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class PbUtils {
    public static void potoPost(HttpPost httpPost, String token, InputStreamEntity inputStreamEntity, Class pbClass) throws IOException {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpPost.addHeader("Content-Type", "application/x-protobuf");
        httpPost.addHeader("Accept", "application/x-protobuf");
        httpPost.addHeader("UserAgent", "Android/localDevelop version/7.1.0");
        if (StringUtils.isNotEmpty(token)) {
            httpPost.addHeader("userToken", token);
        }
        if (pbClass == null) {
            System.out.println("pbClass不能为空");
            return;
        }
        httpPost.setEntity(inputStreamEntity);
        HttpResponse response = httpclient.execute(httpPost);

        JSONObject resultData = new JSONObject();
        try {
            HttpEntity entity = response.getEntity();
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            entity.writeTo(buf);

            ResultResponse.Result result = ResultResponse.Result.parseFrom(buf.toByteArray());

            resultData.put("code", result.getCode());
            resultData.put("msg", result.getMsg());
            System.out.println("result1 : " + ItvJsonUtil.writeValue(resultData) + "\n\n");
            try {
                FastClass fastClass = BaseController.getFastClass(pbClass);
                ByteString dataValue = result.getData().getValue();
                FastMethod parseFrom = fastClass.getMethod("parseFrom", new Class[]{ByteString.class});
                GeneratedMessageV3 invoke = (GeneratedMessageV3) parseFrom.invoke(null, new Object[]{dataValue});
                //reponse 中 data的值
                JSONObject jsonObject = JSONObject.fromObject(new JsonFormat().printToString(invoke));

                //System.out.println("jsonObject : " + ItvJsonUtil.writeValue(jsonObject));

                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(jsonObject.opt("image")).append("  ").
                        append(jsonObject.opt("playFrequency")).append("  ").
                        append(jsonObject.opt("requestFrequency")).
                        append("\r\n");

                resultData.put("data", jsonObject);
                System.out.println("\n\n解析结果result2:{" + ItvJsonUtil.writeValue(resultData) + "}\n\n");

            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    }
}
使用方法如下:

@Test
    public void tpverify(){
        try {
            String url = serviceUrl() + "f/tp/verify";

            HttpPost httpPost = new HttpPost(url);
            GirlTpVerify.Request.Builder builder = GirlTpVerify.Request.newBuilder();
            builder.setContent("xxx是");

            InputStream inputStream = new ByteArrayInputStream(builder.build().toByteArray());
            InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream, builder.build().toByteArray().length);
            PbUtils.potoPost(httpPost, token, inputStreamEntity, GirlTpVerify.Response.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林中山野

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值