Browse files

add option to set an HTTP proxy

  • Loading branch information...
1 parent b4faa3a commit 25281b77dcd756458b106b9620adbab4bb31bdb8 @niklasb niklasb committed with halogenandtoast Sep 24, 2011
Showing with 137 additions and 0 deletions.
  1. +12 −0 lib/capybara/driver/webkit/browser.rb
  2. +86 −0 spec/browser_spec.rb
  3. +24 −0 src/SetProxy.cpp
  4. +11 −0 src/SetProxy.h
  5. +2 −0 src/find_command.h
  6. +2 −0 src/webkit_server.pro
View
12 lib/capybara/driver/webkit/browser.rb
@@ -99,6 +99,18 @@ def get_cookies
command("GetCookies").lines.map{ |line| line.strip }.select{ |line| !line.empty? }
end
+ def set_proxy(opts = {})
+ # remove proxy?
+ return command("SetProxy") if opts.empty?
+
+ # set a HTTP proxy
+ command("SetProxy",
+ opts[:host] || "localhost",
+ opts[:port] || "0",
+ opts[:user] || "",
+ opts[:pass] || "")
+ end
+
private
def start_server
View
86 spec/browser_spec.rb
@@ -2,6 +2,8 @@
require 'self_signed_ssl_cert'
require 'stringio'
require 'capybara/driver/webkit/browser'
+require 'socket'
+require 'base64'
describe Capybara::Driver::Webkit::Browser do
@@ -88,4 +90,88 @@
end
end
+ describe '#set_proxy' do
+ before do
+ @host = '127.0.0.1'
+ @user = 'user'
+ @pass = 'secret'
+ @url = "http://example.org/"
+
+ serv = TCPServer.new(@host, 0)
+ @port = serv.addr[1]
+
+ @proxy_requests = []
+ @proxy = Thread.new(serv, @proxy_requests) do |serv, proxy_requests|
+ while conn = serv.accept do
+ # read request
+ request = []
+ until (line = conn.readline.strip).empty?
+ request << line
+ end
+
+ # send response
+ auth_header = request.find { |h| h =~ /Authorization:/i }
+ if auth_header || request[0].split(/\s+/)[1] =~ /^\//
+ html = "<html><body>D'oh!</body></html>"
+ conn.write "HTTP/1.1 200 OK\r\n"
+ conn.write "Content-Type:text/html\r\n"
+ conn.write "Content-Length: %i\r\n" % html.size
+ conn.write "\r\n"
+ conn.write html
+ conn.close
+ proxy_requests << request if auth_header
+ else
+ conn.write "HTTP/1.1 407 Proxy Auth Required\r\n"
+ conn.write "Proxy-Authenticate: Basic realm=\"Proxy\"\r\n"
+ conn.write "\r\n"
+ conn.close
+ proxy_requests << request
+ end
+ end
+ end
+
+ browser.set_proxy(:host => @host,
+ :port => @port,
+ :user => @user,
+ :pass => @pass)
+ browser.visit @url
+ @proxy_requests.size.should == 2
+ @request = @proxy_requests[-1]
+ end
+
+ after do
+ @proxy.kill
+ end
+
+ it 'uses the HTTP proxy correctly' do
+ @request[0].should match /^GET\s+http:\/\/example.org\/\s+HTTP/i
+ @request.find { |header|
+ header =~ /^Host:\s+example.org$/i }.should_not be nil
+ end
+
+ it 'sends correct proxy authentication' do
+ auth_header = @request.find { |header|
+ header =~ /^Proxy-Authorization:\s+/i }
+ auth_header.should_not be nil
+
+ user, pass = Base64.decode64(auth_header.split(/\s+/)[-1]).split(":")
+ user.should == @user
+ pass.should == @pass
+ end
+
+ it "uses the proxies' response" do
+ browser.body.should include "D'oh!"
+ end
+
+ it 'uses original URL' do
+ browser.url.should == @url
+ end
+
+ it 'is possible to disable proxy again' do
+ @proxy_requests.clear
+ browser.set_proxy
+ browser.visit "http://#{@host}:#{@port}/"
+ @proxy_requests.size.should == 0
+ end
+ end
end
View
24 src/SetProxy.cpp
@@ -0,0 +1,24 @@
+#include "SetProxy.h"
+#include "WebPage.h"
+#include <QNetworkAccessManager>
+#include <QNetworkProxy>
+
+SetProxy::SetProxy(WebPage *page, QObject *parent)
+ : Command(page, parent)
+{ }
+
+void SetProxy::start(QStringList &arguments)
+{
+ // default to empty proxy
+ QNetworkProxy proxy;
+
+ if (arguments.size() > 0)
+ proxy = QNetworkProxy(QNetworkProxy::HttpProxy,
+ arguments[0],
+ (quint16)(arguments[1].toInt()),
+ arguments[2],
+ arguments[3]);
+
+ page()->networkAccessManager()->setProxy(proxy);
+ emit finished(new Response(true));
+}
View
11 src/SetProxy.h
@@ -0,0 +1,11 @@
+#include "Command.h"
+
+class WebPage;
+
+class SetProxy : public Command {
+ Q_OBJECT;
+
+ public:
+ SetProxy(WebPage *page, QObject *parent = 0);
+ virtual void start(QStringList &arguments);
+};
View
2 src/find_command.h
@@ -20,3 +20,5 @@ CHECK_COMMAND(Headers)
CHECK_COMMAND(SetCookie)
CHECK_COMMAND(ClearCookies)
CHECK_COMMAND(GetCookies)
+CHECK_COMMAND(Headers)
+CHECK_COMMAND(SetProxy)
View
2 src/webkit_server.pro
@@ -30,6 +30,7 @@ HEADERS = \
GetCookies.h \
CommandParser.h \
CommandFactory.h \
+ SetProxy.h \
SOURCES = \
main.cpp \
@@ -61,6 +62,7 @@ SOURCES = \
GetCookies.cpp \
CommandParser.cpp \
CommandFactory.cpp \
+ SetProxy.cpp \
RESOURCES = webkit_server.qrc
QT += network webkit

0 comments on commit 25281b7

Please sign in to comment.