FeathersJS:在服务测试中注入HTTP标头

时间:2019-03-19 22:04:53

标签: javascript feathersjs

在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-fetchrequests利用HTTP调用?

1 个答案:

答案 0 :(得分:2)

params将成为您通过的任何内容。只需将params.headers设置为您要测试的内容,例如

const getParams =  {
   ...params,
   headers: { 'header-wanted': 'something' }
};
const resp = await service.get('id', getParams);