【问题标题】:How to mock the same request of different origins URLs (for TestCafe)如何模拟不同来源 URL 的相同请求(用于 TestCafe)
【发布时间】:2021-01-14 10:17:20
【问题描述】:

我需要模拟由 2 个不同来源(url1、url2)发起的相同请求。 但是唯一的一个请求(来自第一个来源 url1)被模拟了。 如何处理多个来源? 提前致谢。

const urlRegExp = new RegExp('/events');
const mock = RequestMock()
    .onRequestTo(urlRegExp)
    .respond({eenter code hererror: 'this request has been mocked'}, 500, {
        'access-control-allow-origin': 'url1.com',
        'access-control-allow-credentials': true
    })
    .onRequestTo(urlRegExp)
    .respond({error: 'this request has been mocked'}, 500, {
        'access-control-allow-origin': 'url2.com',
        'access-control-allow-credentials': true
    });

【问题讨论】:

    标签: testing automation automated-tests e2e-testing testcafe


    【解决方案1】:

    您需要修改您的onRequestTo 调用。请查看 TestCafe 文档的Filter With Predicate section

    const mock = RequestMock()
        .onRequestTo(request => {
            return request.url === 'http://example.com' &&
                   request.method === 'post' &&
                   request.isAjax &&
                   request.body.toString() === 'foo=bar';
        })
        .respond(/*...*/);
    

    您可以设置您的请求模拟以通过请求标头(例如主机、引用者、来源等)过滤请求。因此您需要为不同的请求编写不同的过滤器。

    【讨论】:

      猜你喜欢
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      相关资源
      最近更新 更多