【问题标题】:Nock - how to log the status of each request?Nock - 如何记录每个请求的状态?
【发布时间】:2019-09-25 13:59:31
【问题描述】:

我希望能够查看拦截器拦截的每个请求,并查看它是否已响应或等待处理。我为每个拦截器使用scope.persist(true)。如何做到这一点?

【问题讨论】:

    标签: nock


    【解决方案1】:

    Scopes 在Interceptor 与请求匹配并且Interceptor 回复有效负载时发出事件。 https://github.com/nock/nock#events

    每个事件的回调都以 Interceptor 作为参数传递。

    我不完全确定“查看它是否已响应或待处理”的问题是什么,但这样的事情应该会让你继续前进:

    const scope = nock('http://example.test')
      .get('/')
      .reply(200)
    
    scope.on('request', (req, interceptor) => {
      console.log('interceptor matched request', interceptor.uri)
    });
    scope.on('replied', (req, interceptor) => {
      console.log('response replied with nocked payload', interceptor.uri)
    });
    

    【讨论】:

    • 当我实施上述建议时,如果我没有在scope = nock(baseUrl); 之后立即声明它,我会得到TypeError: scope.on is not a function,即如果我在scope.on(...); 之前执行scope.persist();scope.delay(delay);,那么我会看到上述错误。
    • 奇数。 delay 在拦截器上,而不是在作用域上。所以我会期待那里的错误。 github.com/nock/nock#delay-the-response
    猜你喜欢
    • 2021-01-22
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2021-03-27
    • 2020-03-30
    • 2013-11-19
    相关资源
    最近更新 更多