Nock 拦截请求但 returns 空对象
Nock intercepts request but returns empty object
我正在使用 mocha as the testing framework and I'm trying to mock up a DELETE
request that uses fetch 来对抗 return HTTP 状态代码 204
.
的端点
这里是测试代码:
it('should logout user', (done) => {
nock(<domain>)
.log(console.log)
.delete(path)
.reply(204, {
status: 204,
message: 'This is a mocked response',
});
api.logout(token)
.then((response) => {
console.log('IS DONE?--->', nock.isDone());
console.log('RESPONSE--->', response);
done();
})
.catch((error) => {
console.log('ERROR--->', error);
});
});
这 return 的输出如下:
matching <domain> to DELETE <domain>/<path>: true
(the above line being generated by the .log method in nock)
IS DONE?---> true
RESPONSE---> {}
如您所见,请求已按 log()
和 isDone()
nock 方法正确拦截,但是 response
对象 returned 是空的对象,因此无法断言 returned HTTP 状态代码(在本例中 204
)
知道我在这里可能遗漏了什么吗?为什么 reply()
方法 return 是一个空对象?
更新
这是 logout
方法的代码,remove
方法是使用 DELETE
HTTP 方法对 fetch
请求的包装器。
logout(token) {
return remove(
this.host,
END_POINTS.DELETE_TOKEN,
{
pathParams: { token },
},
{
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
);
}
我认为 204 不应该有响应 body, so you may need to change it to 200. The server of course could return the response, but I think the fetch will not handle it. Other package like request 将处理具有 204 状态的正文,但此请求包仅用于 server-side。
也不确定你的包装器做了什么,但我认为你需要使用 response.json() 承诺来获得响应。而且 mocha 还可以自动处理 promises,你可以 return 它们。请参阅下面的完整示例:
const nock = require('nock')
const fetch = require('isomorphic-fetch');
const request = require('request')
const domain = "http://domain.com";
const path = '/some-path';
const token = 'some-token';
const api = {
logout: (token) => {
return fetch(domain + path, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
});
}
}
describe('something', () => {
it('should logout user with 204 response using request package', (done) => {
nock(domain)
.log(console.log)
.delete(path)
.reply(204, {
status: 204,
message: 'This is a mocked response',
});
request.delete(domain + path, function(err, res) {
console.log(res.body);
done(err);
})
});
it('should logout user', () => {
nock(domain)
.log(console.log)
.delete(path)
.reply(200, {
status: 200,
message: 'This is a mocked response',
});
return api.logout(token)
.then((response) => {
console.log('IS DONE?--->', nock.isDone());
return response.json();
})
.then(function(body) {
console.log('BODY', body);
})
.catch((error) => {
console.log('ERROR--->', error);
});
});
});
这将输出:
something
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true
{"status":204,"message":"This is a mocked response"}
✓ should logout user with 204 response
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true
IS DONE?---> true
BODY { status: 200, message: 'This is a mocked response' }
✓ should logout user
使用了以下部门:
"dependencies": {
"isomorphic-fetch": "^2.2.1",
"mocha": "^3.2.0",
"nock": "^9.0.6",
"request": "^2.79.0"
}
希望对您有所帮助。
我正在使用 mocha as the testing framework and I'm trying to mock up a DELETE
request that uses fetch 来对抗 return HTTP 状态代码 204
.
这里是测试代码:
it('should logout user', (done) => {
nock(<domain>)
.log(console.log)
.delete(path)
.reply(204, {
status: 204,
message: 'This is a mocked response',
});
api.logout(token)
.then((response) => {
console.log('IS DONE?--->', nock.isDone());
console.log('RESPONSE--->', response);
done();
})
.catch((error) => {
console.log('ERROR--->', error);
});
});
这 return 的输出如下:
matching <domain> to DELETE <domain>/<path>: true
(the above line being generated by the .log method in nock)
IS DONE?---> true
RESPONSE---> {}
如您所见,请求已按 log()
和 isDone()
nock 方法正确拦截,但是 response
对象 returned 是空的对象,因此无法断言 returned HTTP 状态代码(在本例中 204
)
知道我在这里可能遗漏了什么吗?为什么 reply()
方法 return 是一个空对象?
更新
这是 logout
方法的代码,remove
方法是使用 DELETE
HTTP 方法对 fetch
请求的包装器。
logout(token) {
return remove(
this.host,
END_POINTS.DELETE_TOKEN,
{
pathParams: { token },
},
{
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
);
}
我认为 204 不应该有响应 body, so you may need to change it to 200. The server of course could return the response, but I think the fetch will not handle it. Other package like request 将处理具有 204 状态的正文,但此请求包仅用于 server-side。
也不确定你的包装器做了什么,但我认为你需要使用 response.json() 承诺来获得响应。而且 mocha 还可以自动处理 promises,你可以 return 它们。请参阅下面的完整示例:
const nock = require('nock')
const fetch = require('isomorphic-fetch');
const request = require('request')
const domain = "http://domain.com";
const path = '/some-path';
const token = 'some-token';
const api = {
logout: (token) => {
return fetch(domain + path, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
});
}
}
describe('something', () => {
it('should logout user with 204 response using request package', (done) => {
nock(domain)
.log(console.log)
.delete(path)
.reply(204, {
status: 204,
message: 'This is a mocked response',
});
request.delete(domain + path, function(err, res) {
console.log(res.body);
done(err);
})
});
it('should logout user', () => {
nock(domain)
.log(console.log)
.delete(path)
.reply(200, {
status: 200,
message: 'This is a mocked response',
});
return api.logout(token)
.then((response) => {
console.log('IS DONE?--->', nock.isDone());
return response.json();
})
.then(function(body) {
console.log('BODY', body);
})
.catch((error) => {
console.log('ERROR--->', error);
});
});
});
这将输出:
something
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true
{"status":204,"message":"This is a mocked response"}
✓ should logout user with 204 response
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true
IS DONE?---> true
BODY { status: 200, message: 'This is a mocked response' }
✓ should logout user
使用了以下部门:
"dependencies": {
"isomorphic-fetch": "^2.2.1",
"mocha": "^3.2.0",
"nock": "^9.0.6",
"request": "^2.79.0"
}
希望对您有所帮助。