【发布时间】:2019-06-25 02:37:33
【问题描述】:
我有验证response.connection.remoteAddress 不是本地IP 的应用程序代码,但是当使用nock 时response.connection 只是一个没有remoteAddress 属性的EventEmitter 实例。如何修改 nock 创建的响应?
更新: 我最终取而代之的是嘲笑 isPrivate():
it('GET returns a 403 status code when example.com resolved to internal ip', function(done){
const scope = nock('https://example.com')
.get('/some/fetched/url')
.replyWithFile(200, FILE_CONTENT);
spyOn(ip, 'isPrivate').and.callFake(() => {
return true;
});
// this handler fetches a url, we want to throw an error if it
// resolves to an internal IP
fetch(`${env.testHost}/some-url-under-test`).then(function(response){
response.text().then(function(body){
scope.done();
expect(response.status).toBe(403);
expect(body).toEqual('Forbidden');
done();
});
});
});
【问题讨论】:
-
鉴于它是一个没有远程地址可参考的模拟请求,预期值是多少?
-
在我的测试中,我想将其设置为本地 IP 并验证我的处理程序是否返回错误。