Search code examples
javascriptfeathersjs

FeathersJS: Injecting HTTP headers in Service Test


In a feathersJS service, I have a before hook being ran that expects a certain HTTP header to exist:

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()],
...
..
.

When testing this service in a generated test file from feathers-cli, however, I haven't found a way to inject headers prior to the before hook being called. The test in question is:

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
   });
});

Is there a way to do this that does not require utilizing an HTTP call via node-fetch or requests?


Solution

  • params will be whatever you pass. Just set params.headers to what you would like to test, e.g.

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