0

I am using BrowserMob-Proxy inside a Selenium test suite. I would like to change the Referer for a few tests. I have added requestInterceptor from the 2.0 docs into our MyProxy class and while it does not generate an error the Referer is not changed.

For now, I am trying to get the requestInterceptor to work in the MyProxy class where the proxy is created. In the end, I would like to be able to specify the Referer in each test.

If anyone has suggestions on getting the requestInterceptor to work please let me know. Here is the MyProxy class. Please let me know if other code examples would be helpful to troubleshoot this.

import org.openqa.selenium.Proxy;

import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.proxy.ProxyServer;
import net.lightbody.bmp.proxy.http.BrowserMobHttpRequest;
import net.lightbody.bmp.proxy.http.RequestInterceptor;

public class MyProxy {
    private ProxyServer proxy;
    private boolean initialized;

    public Har endCapture() throws Exception {
        Thread.sleep(15000);
        return this.proxy.getHar();
    }

    public Proxy getSeleniumProxy() {
        return this.proxy.seleniumProxy();
    }

    public boolean isInitialized() throws Exception {
        return this.initialized;
    }

    public void start() throws Exception {
        int proxyPort = Integer.parseInt(System.getProperty("proxyPort"));
        this.proxy = new ProxyServer(proxyPort);
        this.proxy.start();
        this.proxy.setCaptureHeaders(true);
        this.proxy.setCaptureContent(true);

        this.proxy.addRequestInterceptor(new RequestInterceptor() {
            @Override
            public void process(BrowserMobHttpRequest request, Har har) {
                request.getMethod().removeHeaders("Referer");
                request.getMethod().addHeader("Referer", "http://www.google.com");
            }
        });

        this.initialized = true;
    }

    public void startCapture() throws Exception{
        this.proxy.newHar("MyHar");
    }

    public void stop() throws Exception {
        this.proxy.stop();
        this.initialized = false;
    }
}
4

2 回答 2

1

我认为这里的关键是如何测试新添加的标题,这很难手动完成。

我选择了一个测试站点:http ://headers.cloxy.net/request.php ,它只记录所有请求标头的名称和值。首次设置代理后,我安排了在页面请求完成后编写的屏幕截图。

我能够确定:

 @Override
 public void process(BrowserMobHttpRequest req, Har har) {
     req.getMethod().removeHeaders("Referer");
     req.getMethod().addHeader("Referer", "http://www.google.xyz");

     // Some extras
     req.getMethod().addHeader("Foo_" + System.currentTimeMillis(), "Bar_" + new java.util.Date());
     req.getMethod().setHeader("Lorem_" + System.currentTimeMillis(), "Ipsum_" + new java.util.Date());
 }

...成功在 BrowserMob 2.0.0 和 2.1 beta 5中添加了所有指定的标头。我已经为 Firefox (45)、Chrome (49) 和 PhantomJS 中的每个版本确认了这一点。

在此处输入图像描述

所以,简而言之:

  • OP 的标题添加语法非常好。
  • setHeader也可以按预期工作。
  • BMP 版本号不影响这一点(但在发布时一定要升级到 2.1)
  • 浏览器不影响这个
于 2016-04-08T23:23:34.243 回答
0

查看此问题,看看它是否描述了您的问题。

建议迁移到 BrowserMobProxy 的最新版本,即 2.1.0-beta-5。

于 2016-04-06T01:06:34.753 回答