mirror of
https://github.com/thoughtbot/capybara-webkit
synced 2023-03-27 23:22:28 -04:00
add option to set an HTTP proxy
This commit is contained in:
parent
b4faa3ab6b
commit
25281b77dc
6 changed files with 137 additions and 0 deletions
|
@ -99,6 +99,18 @@ class Capybara::Driver::Webkit
|
|||
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
|
||||
|
|
|
@ -2,6 +2,8 @@ require 'spec_helper'
|
|||
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 @@ describe Capybara::Driver::Webkit::Browser do
|
|||
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
|
||||
|
|
24
src/SetProxy.cpp
Normal file
24
src/SetProxy.cpp
Normal file
|
@ -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
src/SetProxy.h
Normal file
11
src/SetProxy.h
Normal file
|
@ -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);
|
||||
};
|
|
@ -20,3 +20,5 @@ CHECK_COMMAND(Headers)
|
|||
CHECK_COMMAND(SetCookie)
|
||||
CHECK_COMMAND(ClearCookies)
|
||||
CHECK_COMMAND(GetCookies)
|
||||
CHECK_COMMAND(Headers)
|
||||
CHECK_COMMAND(SetProxy)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue