【问题标题】:How to check response's status code in zapier's test cases如何在 zapier 的测试用例中检查响应状态码
【发布时间】:2020-01-04 07:45:24
【问题描述】:

我正在编写一些代码来测试 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);
  });
});

【问题讨论】:

    标签: zapier zapier-cli


    【解决方案1】:

    appTester 返回 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。

    【讨论】:

    • 当我在/creates/contact.js 文件中使用此代码时,它可以工作,但在测试用例文件/test/contact.js 中,响应是一个API 内容对象,它没有任何状态或标题。 appTester() 函数有什么问题吗?
    • 啊,我把你的问题搞混了。我已经更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2023-03-14
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多