Javascript 通过RequestHook添加响应头

Javascript 通过RequestHook添加响应头,javascript,testing,automated-tests,e2e-testing,testcafe,Javascript,Testing,Automated Tests,E2e Testing,Testcafe,如何在Testcafe中将标题添加到响应中?到目前为止,我已经尝试使用RequestHook类的onResponse函数。使用RequestLogger,我可以验证是否添加了标题,但它们实际上不会进入浏览器(在Chrome的网络选项卡中的响应标题列表中不可见) 以下是我正在使用的: 从“testcafe”导入{RequestLogger,RequestHook}; 类HeadersHook扩展了RequestHook{ 构造函数(requestFilterRules){ super(reques

如何在Testcafe中将标题添加到响应中?到目前为止,我已经尝试使用
RequestHook
类的
onResponse
函数。使用
RequestLogger
,我可以验证是否添加了标题,但它们实际上不会进入浏览器(在Chrome的网络选项卡中的响应标题列表中不可见)

以下是我正在使用的:

从“testcafe”导入{RequestLogger,RequestHook};
类HeadersHook扩展了RequestHook{
构造函数(requestFilterRules){
super(requestFilterRules,{includeBody:false,includeHeaders:true});
}
异步onRequest(事件){}
异步onResponse(事件){
event.headers['bar']='foo';
event.headers.foo='bar';
log('hook:',event.headers);
}
}
固定装置(“我的固定装置”);
测试('示例内部sdk使用',异步(t)=>{
const headerLogger=RequestLogger(process.env.TEST\u URL{
logResponseHeaders:没错
});
//这个顺序很重要:首先添加记录器会导致它丢失额外的标题。
//不知道为什么。
等待t.addRequestHooks(新的HeadersHook(process.env.TEST_URL));
等待t.addRequestHooks(头部记录器);
等待t.navigateTo(process.env.TEST\uURL);
console.log('logger:',headerLogger.requests.length,headerLogger.requests[0].response.headers);
t、 调试();
});
这将提供以下控制台输出,似乎表明已成功添加标头:

hook:  {
  'content-type': 'text/html;charset=utf-8',
  vary: 'origin,accept-encoding',
  'access-control-allow-credentials': 'true',
  'access-control-expose-headers': 'WWW-Authenticate,Server-Authorization',
  'cache-control': 'no-cache',
  'content-encoding': 'gzip',
  date: 'Fri, 05 Mar 2021 16:43:06 GMT',
  connection: 'keep-alive',
  'keep-alive': 'timeout=5',
  'transfer-encoding': 'chunked',
  bar: 'foo',
  foo: 'bar'
}
logger:  1 {
  'content-type': 'text/html;charset=utf-8',
  vary: 'origin,accept-encoding',
  'access-control-allow-credentials': 'true',
  'access-control-expose-headers': 'WWW-Authenticate,Server-Authorization',
  'cache-control': 'no-cache',
  'content-encoding': 'gzip',
  date: 'Fri, 05 Mar 2021 16:43:06 GMT',
  connection: 'keep-alive',
  'keep-alive': 'timeout=5',
  'transfer-encoding': 'chunked',
  bar: 'foo',
  foo: 'bar'
}
但在“浏览器网络”选项卡中,我只看到以下内容(缺少其他标题):


这种行为看起来像一个bug,因为修改钩子中的
对象不会影响真正的响应。我在GitHub上重新打开了作者的问题:

当前,向响应添加头的唯一方法是使用
RequestHook
类的私有
\onConfigureResponse
方法:

class HeadersHook扩展了RequestHook{
构造函数(requestFilterRules){
super(requestFilterRules,{includeBody:false,includeHeaders:true});
}
异步onRequest(){}
异步onResponse(){}
_onConfigureResponse(事件){
超级配置响应(事件);
event.setHeader('foo','bar');
event.setHeader('bar','foo');
}
}

请注意,此API是私有的,将来可能会更改。请参考上面的GitHub问题以跟踪其状态。

此行为看起来像一个bug,因为修改钩子中的
对象不会影响真正的响应。我在GitHub上重新打开了作者的问题:

当前,向响应添加头的唯一方法是使用
RequestHook
类的私有
\onConfigureResponse
方法:

class HeadersHook扩展了RequestHook{
构造函数(requestFilterRules){
super(requestFilterRules,{includeBody:false,includeHeaders:true});
}
异步onRequest(){}
异步onResponse(){}
_onConfigureResponse(事件){
超级配置响应(事件);
event.setHeader('foo','bar');
event.setHeader('bar','foo');
}
}
请注意,此API是私有的,将来可能会更改。请参阅上面的GitHub问题以跟踪其状态

access-control-allow-credentials: true
access-control-expose-headers: WWW-Authenticate,Server-Authorization
cache-control: no-cache
connection: keep-alive
content-encoding: gzip
content-type: text/html;charset=utf-8
date: Fri, 05 Mar 2021 16:45:51 GMT
keep-alive: timeout=5
transfer-encoding: chunked
vary: origin,accept-encoding