如何修改http headers
How to modify http headers
我正在自动化 UI 测试。 API 受 oAuth 保护。所以要点击 URL 让我们说 http://mydom/api/welcome
浏览器必须已将授权密钥设置为 header,并且值必须是有效令牌。
手动测试,我用Requestly (Chrome Extension)设置header。
(任何人都可以指出请求在内部做什么,以便来自 chrome 的每个请求都获得 header 值是请求设置的值吗?)
有没有办法以编程方式设置 header 值?
PS:我使用 curl 调用 oath url 以获取有效令牌并将令牌值设置为环境变量。我也用邮递员来得到同样的东西。但是所有这些都不会影响从浏览器调用时。
你不能很容易地做到这一点,因为 Chrome 不支持它(参见 https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/141)。
就个人而言,我会使用诸如请求 (https://github.com/request/requesthttps://github.com/request/request) 和基本断言之类的东西来进行此测试,而不是通过带有量角器的浏览器。
您不能直接在 selenium 中操作 http headers。你需要使用代理来做这样的事情。 BrowserMobProxy is a popular proxy in selenium world. You can use the node wrapper browsermob-node to do this in protractor test. I added a sample section on how to modify headers here
我使用 supertest and supertest-as-promised 通过量角器进行 API 测试。因此,我在 OAuth 端点上执行 GET,将令牌存储在变量中,然后在其余 API 调用中执行 request.get(url).set('Authorization', auth.tokenType + ' ' + auth.token);
。
因此,您可能能够做到这一点的一种方法是使用诸如 supertest 之类的东西来获取令牌,然后 browser.manage().addCookie()
在浏览器会话中设置它。
@Sam 在此请求作者。
目前不能,您不能以编程方式设置 header 值。它现在必须是静态的。考虑到出现了多少用例,这是实施此功能的非常长期的计划。但这是您可以做的。
你可以分叉 Requestly github repo. The code where actual header modification happens is inside Background.js 。方法名称是 BG.Methods.modifyHeaders
。请继续编写您自己的逻辑以生成 header 值并修改它。
来自 Chrome Docs
的注释
The following headers are currently not provided to the onBeforeSendHeaders event. This list is not guaranteed to be complete nor stable.
- 授权
- Cache-Control
- 连接
- Content-Length主机
- If-Modified-Since
- If-None-Match
- If-Range
- Partial-Data 编译指示
- Proxy-Authorization
- Proxy-Connection
- Transfer-Encoding
可能这个答案对您没有用,因为Authorization
无法修改。但我仍然将这个答案添加为一种可行的方法。
我正在自动化 UI 测试。 API 受 oAuth 保护。所以要点击 URL 让我们说 http://mydom/api/welcome 浏览器必须已将授权密钥设置为 header,并且值必须是有效令牌。
手动测试,我用Requestly (Chrome Extension)设置header。 (任何人都可以指出请求在内部做什么,以便来自 chrome 的每个请求都获得 header 值是请求设置的值吗?)
有没有办法以编程方式设置 header 值?
PS:我使用 curl 调用 oath url 以获取有效令牌并将令牌值设置为环境变量。我也用邮递员来得到同样的东西。但是所有这些都不会影响从浏览器调用时。
你不能很容易地做到这一点,因为 Chrome 不支持它(参见 https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/141)。
就个人而言,我会使用诸如请求 (https://github.com/request/requesthttps://github.com/request/request) 和基本断言之类的东西来进行此测试,而不是通过带有量角器的浏览器。
您不能直接在 selenium 中操作 http headers。你需要使用代理来做这样的事情。 BrowserMobProxy is a popular proxy in selenium world. You can use the node wrapper browsermob-node to do this in protractor test. I added a sample section on how to modify headers here
我使用 supertest and supertest-as-promised 通过量角器进行 API 测试。因此,我在 OAuth 端点上执行 GET,将令牌存储在变量中,然后在其余 API 调用中执行 request.get(url).set('Authorization', auth.tokenType + ' ' + auth.token);
。
因此,您可能能够做到这一点的一种方法是使用诸如 supertest 之类的东西来获取令牌,然后 browser.manage().addCookie()
在浏览器会话中设置它。
@Sam 在此请求作者。
目前不能,您不能以编程方式设置 header 值。它现在必须是静态的。考虑到出现了多少用例,这是实施此功能的非常长期的计划。但这是您可以做的。
你可以分叉 Requestly github repo. The code where actual header modification happens is inside Background.js 。方法名称是 BG.Methods.modifyHeaders
。请继续编写您自己的逻辑以生成 header 值并修改它。
来自 Chrome Docs
的注释The following headers are currently not provided to the onBeforeSendHeaders event. This list is not guaranteed to be complete nor stable.
- 授权
- Cache-Control
- 连接
- Content-Length主机
- If-Modified-Since
- If-None-Match
- If-Range
- Partial-Data 编译指示
- Proxy-Authorization
- Proxy-Connection
- Transfer-Encoding
可能这个答案对您没有用,因为Authorization
无法修改。但我仍然将这个答案添加为一种可行的方法。