• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • http回調測試自動化實踐

    發表于:2017-06-08來源:xieyingchunuestc作者:xieyingchunuestc點擊數: 標簽:http
    背景:視頻云點播支持用戶設置回調地址,在上傳或者轉碼結束后向用戶的回調地址以http協議發送回調信息,例如上傳完成后的視頻信息,視頻轉碼信息。 之前的測試方法:回調
      背景:視頻云點播支持用戶設置回調地址,在上傳或者轉碼結束后向用戶的回調地址以http協議發送回調信息,例如上傳完成后的視頻信息,視頻轉碼信息。
     
      之前的測試方法:回調地址設置為測試服務器地址,自動化腳本跑的同時去測試服務器查看回調信息。
     
      缺點:隨著業務擴展,點播依賴服務和NTS集群擴展迅速,每次回歸量增大,回調測試無法自動化會耗費大量時間,同時,人眼觀察回調,當測試代碼發送大量請求,回調信息與發送的請求難以一一對應,可能請求后發但是回調先至。
     
      問題的解決方式:
     
      1、在運行測試代碼的機器上找到可用端口
     
      2、起HttpServer并監聽端口。
     
      3、將起的HttpServer地址設置成點播回調地址
     
      4、HttpServer解析收到的回調信息,并與點播業務發送的請求信息進行斷言
     
      5、關閉HttpServer
     
      下面從代碼層面逐一實現上面四個步驟
     
      一、找到可用端口:設置從指定端口開始向后遍歷,查看端口是否可用。
     
      public static int check(int port) {
     
      int _port;
     
      try {
     
      ServerSocket sock = new ServerSocket(port);
     
      sock.close();
     
      _port = port;
     
      System.out.println("Port : " + port + " is Ok");
     
      } catch (IOException e) {
     
      System.out.println("Port : " + port + " is occupied ,Try to port : " + (port + 1));
     
      _port = port + 1;
     
      check(_port);
     
      }
     
      return _port;
     
      }
     
      再用String addrip = InetAddress.getLocalHost()。getHostAddress();  得到本機IP,這樣就可以獲得起HttpServer服務需要的地址。
     
      二、起HttpServer并監聽端口
     
      public static String callbackHttpServer(int minPort) throws IOException {
     
      int port;
     
      String addrip = InetAddress.getLocalHost()。getHostAddress();
     
      port = check(minPort);
     
      String ipAddress = addrip + ":" + port;
     
      System.out.println("address is " + ipAddress);
     
      InetSocketAddress addr = new InetSocketAddress(port);
     
      server = HttpServer.create(addr, 0);
     
      MyHandler myHandler=new MyHandler();
     
      server.createContext("/", myHandler);
     
      server.setExecutor(Executors.newCachedThreadPool());
     
      server.start();
     
      System.out.println("Server is listening on port : " + port);
     
      return ipAddress;
     
      }
     
      三、解析HttpServer收到的消息
     
      public void handle(HttpExchange exchange) throws IOException {
     
      String requestMethod = exchange.getRequestMethod();
     
      if (requestMethod.equalsIgnoreCase("POST")) {
     
      Headers responseHeaders = exchange.getResponseHeaders();
     
      responseHeaders.set("Content-Type", "text/plain");
     
      exchange.sendResponseHeaders(200, 0);            //給請求發送方返回200的響應碼,否則對方一直收不到響應。
     
      InputStream requestBody = exchange.getRequestBody();          //收到的回調信息
     
      Headers requestHeaders = exchange.getRequestHeaders();
     
      Set<String> keySet = requestHeaders.keySet();
     
      Iterator<String> iter = keySet.iterator();           //如需post請求的header信息,則可解析出來
     
      String body = slurp(requestBody, 1024);          //將InputStream 類型轉換為String類型
     
      JsonObject messageBody = new JsonParser()。parse(body)。getAsJsonObject();
     
      reparam=messageBody;
     
      this.setReparam(reparam);
     
      System.out.println("request body is >>>>>:" + this.getReparam());    //打印出回調請求體的內容
     
      requestBody.close();
     
      }
     
      }
     
      四、關閉HttpServer:
     
      收到回調信息或者監聽超時還未收到回調信息,則關閉HttpServer服務。
     
      public JsonObject getCallbackBody()throws IOException, InterruptedException
     
      {
     
      int i=0;
     
      while (true) {
     
      if(MyHandler.getReparam()!=null||i++>30)
     
      break;
     
      else
     
      TimeUnit.SECONDS.sleep(5);
     
      }
     
      String type=MyHandler.judgeCallbackType(MyHandler.getReparam());
     
      System.out.println("type is "+type);
     
      JsonObject param=MyHandler.getReparam();
     
      System.out.println("jsonObject is : "+param.toString());
     
      MyHttpServer.stopHttpServer();
     
      System.out.println("listen service is closed ");
     
      return param;
     
      }
     
      }
     
      總結:如上即實現了http回調信息的解析及自動化,在應用中,需要保證起HttpServer的地址業務服務器可以telnet通,否則,業務服務器發送過去的post請求會被拒絕。

    原文轉自:http://qa.blog.163.com/blog/static/190147002201732491154667/

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>