【发布时间】:2019-09-25 13:59:31
【问题描述】:
我希望能够查看拦截器拦截的每个请求,并查看它是否已响应或等待处理。我为每个拦截器使用scope.persist(true)。如何做到这一点?
【问题讨论】:
标签: nock
我希望能够查看拦截器拦截的每个请求,并查看它是否已响应或等待处理。我为每个拦截器使用scope.persist(true)。如何做到这一点?
【问题讨论】:
标签: nock
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