如何检查zapier测试用例中的响应状态码

How to check response's status code in zapier's test cases

我正在编写一些代码来测试 Zapier 的 CLI 中的操作。我想在这里再添加一个条件,例如 response.status == 200 or 201; 来检查 API 响应代码是 200 还是 201。

我该怎么做?当我记录响应时,它给了我整个 JSON 对象 API 回来了。

describe("contact create", () => {
  it("should create a contact", done => {
    const bundle = {
      inputData: {
        firstName: "Test",
        lastName: "Contact",
        email: "Contact@test.com",
        mobileNumber: "+12125551234",
        type: "contact"
      }
    };
    appTester(App.creates.contact.operation.perform, bundle)
      .then(response => {

        // Need one more condition whether response status is 200 or 201.

        response.should.not.be.an.Array();
        response.should.have.property('id');
        done();
      })
      .catch(done);
  });
});

appTester returns perform 方法的结果,它不是 API 响应。这是传回 Zapier 的数据。

最好的办法是在 perform:

中添加这样一行
// after your `z.request`
if (!(response.status === 200 || response.status === 201)) {
  throw new Error('need a 200/201 response')
}

这将确保您得到您想要的准确答复。但是,更有可能的是,您可以添加一个 response.throwForStatus() 以确保它不是错误代码,并且不必担心它是否正好是 200/201。