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

问题描述 投票:1回答:1

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

javascript feathersjs
1个回答
2
投票

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

const getParams =  {
   ...params,
   headers: { 'header-wanted': 'something' }
};
const resp = await service.get('id', getParams);
© www.soinside.com 2019 - 2024. All rights reserved.