Nock 不匹配中间件内的 HTTPS 调用?
Nock isn't matching HTTPS call inside middleware?
我有一个中间件可以通过请求与第三方服务进行身份验证。我用 superagent 做这个请求。显然我想在我的测试中模拟这个,因为它会大大降低它们的速度并且还依赖于第三方服务器。
使用nock时,似乎根本找不到请求。我什至尝试使用记录器,它只接收我本地端点的实际请求。 (虽然它使用了一个陌生的IP和端口?)。
我中间件里面的请求;
export default async (req, res, next) => {
const user = await superagent
.get(`https://example.com/session/`)
.query({ session })
.set('Api-Key', '1234');
}
我的诺克实例;
nock('https://example.com/session/')
.persist()
.get('/session/')
.reply(200, {
success: true,
username: 'testuser',
})
.log(console.log);
这里有 2 个问题。
首先,你定义了两次/session/
:
nock('https://example.com/session/')
.get('/session/')
选择:
nock('https://example.com').get('/session/')
nock('https://example.com/session/').get('/')
第二个问题,您在调用中添加了一个查询字符串 (.query({ session })
),但您没有使用 .query(true)
.
将其告诉 Nock
最后,你应该有这样的东西:
nock('https://example.com/session/')
.persist()
.get('/') // rewrote here
.query(true) // added here
.reply(200, {
success: true,
username: 'testuser',
})
.log(console.log);
我有一个中间件可以通过请求与第三方服务进行身份验证。我用 superagent 做这个请求。显然我想在我的测试中模拟这个,因为它会大大降低它们的速度并且还依赖于第三方服务器。
使用nock时,似乎根本找不到请求。我什至尝试使用记录器,它只接收我本地端点的实际请求。 (虽然它使用了一个陌生的IP和端口?)。
我中间件里面的请求;
export default async (req, res, next) => {
const user = await superagent
.get(`https://example.com/session/`)
.query({ session })
.set('Api-Key', '1234');
}
我的诺克实例;
nock('https://example.com/session/')
.persist()
.get('/session/')
.reply(200, {
success: true,
username: 'testuser',
})
.log(console.log);
这里有 2 个问题。
首先,你定义了两次/session/
:
nock('https://example.com/session/')
.get('/session/')
选择:
nock('https://example.com').get('/session/')
nock('https://example.com/session/').get('/')
第二个问题,您在调用中添加了一个查询字符串 (.query({ session })
),但您没有使用 .query(true)
.
最后,你应该有这样的东西:
nock('https://example.com/session/')
.persist()
.get('/') // rewrote here
.query(true) // added here
.reply(200, {
success: true,
username: 'testuser',
})
.log(console.log);