zoukankan      html  css  js  c++  java
  • groovy脚本test类打印响应头和请求头

    主要代码:

    Request request = builder.url("http://baidu.com).get().build();
    Response response = client.newCall(request).execute();

    Headers requestHeaders= response.networkResponse().request().headers();
    logger.info("{}", requestHeaders);
    Headers responseHeaders= response.networkResponse().headers();
    logger.info("{}", responseHeaders);

    详细代码:

    
    import java.util.concurrent.TimeUnit;
    import okhttp3.OkHttpClient
    import okhttp3.Request
    import okhttp3.Response
    import okhttp3.Cookie;
    import okhttp3.*;
    import org.junit.Assert
    import org.slf4j.Logger
    import org.slf4j.LoggerFactory
    
    
        public final Logger logger = TestUtils.LOGGER;
        public final OkHttpClient client;
        public final Request.Builder builder;
        public final Cookie.Builder cookie;
    
        Test() {
            client = new OkHttpClient().newBuilder()
                        .connectTimeout(6, TimeUnit.SECONDS)
                        .readTimeout(6, TimeUnit.SECONDS)
                        .writeTimeout(6, TimeUnit.SECONDS)
                        .followRedirects(false)
                        .build();
            builder = new Request.Builder();
    
            builder.addHeader("Cache-Control", "no-cache");
         builder.addHeader("Accept-Encoding", "gzip, deflate, br"); builder.addHeader("Connection", "keep-alive"); builder.addHeader("Host", "baidu.com"); builder.addHeader("X-API-Login-Type", "1"); builder.addHeader("User-Agent", "PostmanRuntime/7.26.8"); builder.addHeader("Cookie", "xx=xx;xx=xx"); logger.info("TestRunner init..."); } @TestCase void test() { TestUtils.transactionBegin("test_tran"); Request request = builder.url("http://baidu.com").get().build(); Response response = client.newCall(request).execute(); Headers requestHeaders= response.networkResponse().request().headers();
         logger.info("{}", requestHeaders);
         Headers responseHeaders= response.networkResponse().headers();
         logger.info("{}", responseHeaders);
    if(response.code() == 200){ TestUtils.transactionSuccess("test_tran"); logger.info("============= response successful, response code: {}, response body: {}",response.code(), response.body().string()); }else{ TestUtils.transactionFailure("test_tran"); logger.error("============= response false, response code: {}, response body: {}",response.code()); } response.close(); } }

    builder.headerbuilder.addHeader区别:

    header是替换,addHeader是追加

  • 相关阅读:
    Android Studio没有 Build Bundle(s)/Apk(s)选项或者Make Project没有反应
    typescript遍历Map
    Cocos Creator的项目优化问题
    Cocos Creator 常用优化技巧汇总
    CocosCreator客户端优化系列(四):CPU占用以及性能优化
    CocosCreator客户端优化系列(三):内存优化
    CocosCreator客户端优化系列(二):渲染优化
    CocosCreator客户端优化系列(一):加载优化(下)
    CocosCreator客户端优化系列(一):加载优化(上)
    cocos creator性能优化--Prefab加载优化
  • 原文地址:https://www.cnblogs.com/Tanwheey/p/14421893.html
Copyright © 2011-2022 走看看