簡體   English   中英

FeathersJS:在服務測試中注入HTTP標頭

[英]FeathersJS: Injecting HTTP headers in Service Test

在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生成的測試文件中測試此服務時,我還沒有找到在調用before鈎子之前注入標頭的方法。 有問題的測試是:

測試/服務/ 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調用?

params將是您通過的任何東西。 只需將params.headers設置為您要測試的內容,例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM