Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add option to set an HTTP proxy

  • Loading branch information...
commit 25281b77dcd756458b106b9620adbab4bb31bdb8 1 parent b4faa3a
Niklas Baumstark niklasb authored halogenandtoast committed
12 lib/capybara/driver/webkit/browser.rb
@@ -99,6 +99,18 @@ def get_cookies
99 99 command("GetCookies").lines.map{ |line| line.strip }.select{ |line| !line.empty? }
100 100 end
101 101
  102 + def set_proxy(opts = {})
  103 + # remove proxy?
  104 + return command("SetProxy") if opts.empty?
  105 +
  106 + # set a HTTP proxy
  107 + command("SetProxy",
  108 + opts[:host] || "localhost",
  109 + opts[:port] || "0",
  110 + opts[:user] || "",
  111 + opts[:pass] || "")
  112 + end
  113 +
102 114 private
103 115
104 116 def start_server
86 spec/browser_spec.rb
@@ -2,6 +2,8 @@
2 2 require 'self_signed_ssl_cert'
3 3 require 'stringio'
4 4 require 'capybara/driver/webkit/browser'
  5 +require 'socket'
  6 +require 'base64'
5 7
6 8 describe Capybara::Driver::Webkit::Browser do
7 9
@@ -88,4 +90,88 @@
88 90 end
89 91 end
90 92
  93 + describe '#set_proxy' do
  94 + before do
  95 + @host = '127.0.0.1'
  96 + @user = 'user'
  97 + @pass = 'secret'
  98 + @url = "http://example.org/"
  99 +
  100 + serv = TCPServer.new(@host, 0)
  101 + @port = serv.addr[1]
  102 +
  103 + @proxy_requests = []
  104 + @proxy = Thread.new(serv, @proxy_requests) do |serv, proxy_requests|
  105 + while conn = serv.accept do
  106 + # read request
  107 + request = []
  108 + until (line = conn.readline.strip).empty?
  109 + request << line
  110 + end
  111 +
  112 + # send response
  113 + auth_header = request.find { |h| h =~ /Authorization:/i }
  114 + if auth_header || request[0].split(/\s+/)[1] =~ /^\//
  115 + html = "<html><body>D'oh!</body></html>"
  116 + conn.write "HTTP/1.1 200 OK\r\n"
  117 + conn.write "Content-Type:text/html\r\n"
  118 + conn.write "Content-Length: %i\r\n" % html.size
  119 + conn.write "\r\n"
  120 + conn.write html
  121 + conn.close
  122 + proxy_requests << request if auth_header
  123 + else
  124 + conn.write "HTTP/1.1 407 Proxy Auth Required\r\n"
  125 + conn.write "Proxy-Authenticate: Basic realm=\"Proxy\"\r\n"
  126 + conn.write "\r\n"
  127 + conn.close
  128 + proxy_requests << request
  129 + end
  130 + end
  131 + end
  132 +
  133 + browser.set_proxy(:host => @host,
  134 + :port => @port,
  135 + :user => @user,
  136 + :pass => @pass)
  137 + browser.visit @url
  138 + @proxy_requests.size.should == 2
  139 + @request = @proxy_requests[-1]
  140 + end
  141 +
  142 + after do
  143 + @proxy.kill
  144 + end
  145 +
  146 + it 'uses the HTTP proxy correctly' do
  147 + @request[0].should match /^GET\s+http:\/\/example.org\/\s+HTTP/i
  148 + @request.find { |header|
  149 + header =~ /^Host:\s+example.org$/i }.should_not be nil
  150 + end
  151 +
  152 + it 'sends correct proxy authentication' do
  153 + auth_header = @request.find { |header|
  154 + header =~ /^Proxy-Authorization:\s+/i }
  155 + auth_header.should_not be nil
  156 +
  157 + user, pass = Base64.decode64(auth_header.split(/\s+/)[-1]).split(":")
  158 + user.should == @user
  159 + pass.should == @pass
  160 + end
  161 +
  162 + it "uses the proxies' response" do
  163 + browser.body.should include "D'oh!"
  164 + end
  165 +
  166 + it 'uses original URL' do
  167 + browser.url.should == @url
  168 + end
  169 +
  170 + it 'is possible to disable proxy again' do
  171 + @proxy_requests.clear
  172 + browser.set_proxy
  173 + browser.visit "http://#{@host}:#{@port}/"
  174 + @proxy_requests.size.should == 0
  175 + end
  176 + end
91 177 end
24 src/SetProxy.cpp
... ... @@ -0,0 +1,24 @@
  1 +#include "SetProxy.h"
  2 +#include "WebPage.h"
  3 +#include <QNetworkAccessManager>
  4 +#include <QNetworkProxy>
  5 +
  6 +SetProxy::SetProxy(WebPage *page, QObject *parent)
  7 + : Command(page, parent)
  8 +{ }
  9 +
  10 +void SetProxy::start(QStringList &arguments)
  11 +{
  12 + // default to empty proxy
  13 + QNetworkProxy proxy;
  14 +
  15 + if (arguments.size() > 0)
  16 + proxy = QNetworkProxy(QNetworkProxy::HttpProxy,
  17 + arguments[0],
  18 + (quint16)(arguments[1].toInt()),
  19 + arguments[2],
  20 + arguments[3]);
  21 +
  22 + page()->networkAccessManager()->setProxy(proxy);
  23 + emit finished(new Response(true));
  24 +}
11 src/SetProxy.h
... ... @@ -0,0 +1,11 @@
  1 +#include "Command.h"
  2 +
  3 +class WebPage;
  4 +
  5 +class SetProxy : public Command {
  6 + Q_OBJECT;
  7 +
  8 + public:
  9 + SetProxy(WebPage *page, QObject *parent = 0);
  10 + virtual void start(QStringList &arguments);
  11 +};
2  src/find_command.h
@@ -20,3 +20,5 @@ CHECK_COMMAND(Headers)
20 20 CHECK_COMMAND(SetCookie)
21 21 CHECK_COMMAND(ClearCookies)
22 22 CHECK_COMMAND(GetCookies)
  23 +CHECK_COMMAND(Headers)
  24 +CHECK_COMMAND(SetProxy)
2  src/webkit_server.pro
@@ -30,6 +30,7 @@ HEADERS = \
30 30 GetCookies.h \
31 31 CommandParser.h \
32 32 CommandFactory.h \
  33 + SetProxy.h \
33 34
34 35 SOURCES = \
35 36 main.cpp \
@@ -61,6 +62,7 @@ SOURCES = \
61 62 GetCookies.cpp \
62 63 CommandParser.cpp \
63 64 CommandFactory.cpp \
  65 + SetProxy.cpp \
64 66
65 67 RESOURCES = webkit_server.qrc
66 68 QT += network webkit

0 comments on commit 25281b7

Please sign in to comment.
Something went wrong with that request. Please try again.