在feathersJS服务中,我有一个正在运行的before
钩子,它需要一个特定的HTTP头存在:
SRC /服务/ 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
生成的测试文件中测试此服务时,我还没有找到一种在调用前挂钩之前注入头的方法。有问题的测试是:
测试/服务/ 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调用?
params
将是你通过的任何东西。只需将params.headers
设置为您要测试的内容,例如
const getParams = {
...params,
headers: { 'header-wanted': 'something' }
};
const resp = await service.get('id', getParams);