簡體   English   中英

如何使用Protobuf編碼Cypress存根響應?

[英]How I can encode with Protobuf a Cypress stub response?

我有一些固定的工具,可以將服務器編碼為帶有protobuf的消息(我正在使用protobufjs )。 我想對固定裝置進行解碼,以輕松地對其進行操作,並讓賽普拉斯在將響應發送給客戶端之前對存根主體進行編碼,我該怎么做?

[更新]現在可以作為賽普拉斯插件使用


那是我的解決方案:

  • cypress/plugins/protobufjs/index.js文件(其中導入了protobuf定義)
const path = require("path");
const protobufjs = require("protobufjs");
const definition = path.join(__dirname, "../../../public/escrow/ui.proto");
const proto = protobufjs.loadSync(definition);
module.exports = {
  Status: proto.lookupType("escrow.Status"),
};
  • cypress/plugins/index.js文件(其中的編碼發生在自定義的Cypress任務中)
const { StringDecoder } = require("string_decoder");
const Messages = require("./protobufjs");

module.exports = on => {
  on("task", {
    protobufEncode: ({ data, encoderName }) => {
      const decoder = new StringDecoder("utf8");
      const bufferValue = Messages[encoderName].encode(data).finish();
      return decoder.end(Buffer.from(bufferValue));
    }
  });
};
  • 在您的測試中
cy.fixture("YOUR_FIXTURE.json").then(async json => {
  cy.task("protobufEncode", { encoderName: "Status", data: json }).then(result => {
    cy.route({
      headers: {
        "content-type": "application/octet-stream"
      },
      method: "GET",
      response: result,
      status: 200,
      url: `**/YOUR_URL`
    }).as("route_status_one_usb_key");
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM