cucumberjs - 检查api调用的状态代码或标题

时间:2018-06-05 14:37:21

标签: javascript ecmascript-6 cucumberjs

我正在使用以下功能文件发送状态代码500以检查api调用中的错误。

Feature: ApiError
  Scenario: Error getting data
    Given I am logged in as "admin@xyz.com"
    And Api calls containing ".*" return "500"

此功能适用于以下功能:

const { When } = require('cucumber'); // eslint-disable-line import/no-extraneous-dependencies

When(
  /^Api calls containing "([^"]*)" return "([^"]*)"$/,
  async (endpoint, statusCode) => {
    function mock(endpointRegex, status) {
      const cachedFetch = window.fetch;
      window.fetch = (url, options) => {
        const isMatch = url.match('/api') && url.match(new RegExp(endpointRegex));
        return isMatch
          ? new Promise(resolve => resolve({ status }))
          : cachedFetch(url, options);
      };
    }

    return browser.execute(mock, endpoint, statusCode);
  },
);

现在,我正在尝试包括检查标头。例如,

Api calls containing ".*" return status "500"
Api calls containing ".*" return header "x-expires-at"

为此我尝试了When( /^Api calls containing "([^"]*)" return "(?:status|header) ([^"]*)"$/,

对于标题,我需要检查与headers.get(header)的状态相似。

我应该在当前代码中对状态和标题检查进行哪些更改?

0 个答案:

没有答案