Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
add option to set an HTTP proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasb authored and halogenandtoast committed Oct 14, 2011
1 parent b4faa3a commit 25281b7
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/capybara/driver/webkit/browser.rb
Expand Up @@ -99,6 +99,18 @@ def get_cookies
command("GetCookies").lines.map{ |line| line.strip }.select{ |line| !line.empty? } command("GetCookies").lines.map{ |line| line.strip }.select{ |line| !line.empty? }
end 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 private


def start_server def start_server
Expand Down
86 changes: 86 additions & 0 deletions spec/browser_spec.rb
Expand Up @@ -2,6 +2,8 @@
require 'self_signed_ssl_cert' require 'self_signed_ssl_cert'
require 'stringio' require 'stringio'
require 'capybara/driver/webkit/browser' require 'capybara/driver/webkit/browser'
require 'socket'
require 'base64'


describe Capybara::Driver::Webkit::Browser do describe Capybara::Driver::Webkit::Browser do


Expand Down Expand Up @@ -88,4 +90,88 @@
end end
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 end
24 changes: 24 additions & 0 deletions 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));
}
11 changes: 11 additions & 0 deletions 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);
};
2 changes: 2 additions & 0 deletions src/find_command.h
Expand Up @@ -20,3 +20,5 @@ CHECK_COMMAND(Headers)
CHECK_COMMAND(SetCookie) CHECK_COMMAND(SetCookie)
CHECK_COMMAND(ClearCookies) CHECK_COMMAND(ClearCookies)
CHECK_COMMAND(GetCookies) CHECK_COMMAND(GetCookies)
CHECK_COMMAND(Headers)
CHECK_COMMAND(SetProxy)
2 changes: 2 additions & 0 deletions src/webkit_server.pro
Expand Up @@ -30,6 +30,7 @@ HEADERS = \
GetCookies.h \ GetCookies.h \
CommandParser.h \ CommandParser.h \
CommandFactory.h \ CommandFactory.h \
SetProxy.h \


SOURCES = \ SOURCES = \
main.cpp \ main.cpp \
Expand Down Expand Up @@ -61,6 +62,7 @@ SOURCES = \
GetCookies.cpp \ GetCookies.cpp \
CommandParser.cpp \ CommandParser.cpp \
CommandFactory.cpp \ CommandFactory.cpp \
SetProxy.cpp \


RESOURCES = webkit_server.qrc RESOURCES = webkit_server.qrc
QT += network webkit QT += network webkit
Expand Down

0 comments on commit 25281b7

Please sign in to comment.