在feathersJS服务中,我运行了一个before
钩子,该钩子期望存在某个HTTP标头:
src / services / service_name / service_name.hooks.js
const validationHook = () => (context, next) => {
if (!context.params.headers.hasOwnProperty('header-wanted'))
throw new errors.BadRequest();
next(null, context);
};
module.exports = {
before: {
all: [cronValidationHook()],
...
..
.
但是,在从feathers-cli
生成的测试文件中测试此服务时,我还没有找到在调用before钩子之前注入标头的方法。有问题的测试是:
test / services / service_name.test.js
describe('get', () => {
it('should run "id" endpoint', async () => {
const service = app.service('v1/cron');
const resp = await service.get('id', params);
// Assertions exist after this call
});
});
是否有一种方法不需要通过node-fetch
或requests
利用HTTP调用?
答案 0 :(得分:2)
params
将成为您通过的任何内容。只需将params.headers
设置为您要测试的内容,例如
const getParams = {
...params,
headers: { 'header-wanted': 'something' }
};
const resp = await service.get('id', getParams);