[英]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-fetch
或requests
來利用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.