From ff51b158ac3a3c17889d73753f945a76b4f26319 Mon Sep 17 00:00:00 2001 From: addicted Date: Sun, 18 Jan 2015 19:50:28 +0200 Subject: [PATCH 1/9] support --- lib/capybara/webkit/browser.rb | 5 +++-- src/SetProxy.cpp | 13 +++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/capybara/webkit/browser.rb b/lib/capybara/webkit/browser.rb index 9f75908c..a64dce18 100644 --- a/lib/capybara/webkit/browser.rb +++ b/lib/capybara/webkit/browser.rb @@ -246,7 +246,7 @@ def get_cookies def set_proxy(options = {}) options = default_proxy_options.merge(options) - command("SetProxy", options[:host], options[:port], options[:user], options[:pass]) + command("SetProxy", options[:host], options[:port], options[:user], options[:pass], options[:type]) end def clear_proxy @@ -308,7 +308,8 @@ def default_proxy_options :host => "localhost", :port => "0", :user => "", - :pass => "" + :pass => "", + :type => "http" } end end diff --git a/src/SetProxy.cpp b/src/SetProxy.cpp index 118d8a99..38edba4c 100644 --- a/src/SetProxy.cpp +++ b/src/SetProxy.cpp @@ -11,12 +11,13 @@ void SetProxy::start() // default to empty proxy QNetworkProxy proxy; - if (arguments().size() > 0) - proxy = QNetworkProxy(QNetworkProxy::HttpProxy, - arguments()[0], - (quint16)(arguments()[1].toInt()), - arguments()[2], - arguments()[3]); + if (arguments().size() > 0) { + proxy.setType(arguments()[4] == 'socks' ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy); + proxy.setHostName(arguments()[0]); + proxy.setPort((quint16)(arguments()[1].toInt())); + proxy.setUser(arguments()[2]); + proxy.setPassword(arguments()[3]); + } manager()->networkAccessManager()->setProxy(proxy); finish(true); From 852bae8e19267cb886ff1de3f855921d2fa434c6 Mon Sep 17 00:00:00 2001 From: addicted Date: Sun, 18 Jan 2015 20:05:48 +0200 Subject: [PATCH 2/9] proxy set --- src/SetProxy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SetProxy.cpp b/src/SetProxy.cpp index 38edba4c..8fb1b8ad 100644 --- a/src/SetProxy.cpp +++ b/src/SetProxy.cpp @@ -12,7 +12,7 @@ void SetProxy::start() QNetworkProxy proxy; if (arguments().size() > 0) { - proxy.setType(arguments()[4] == 'socks' ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy); + proxy.setType((arguments()[4] == "socks") ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy); proxy.setHostName(arguments()[0]); proxy.setPort((quint16)(arguments()[1].toInt())); proxy.setUser(arguments()[2]); From 573674da489fd642198f56176bba4379cb94bb3a Mon Sep 17 00:00:00 2001 From: addicted Date: Sun, 18 Jan 2015 20:37:13 +0200 Subject: [PATCH 3/9] changed argument to socks5 --- src/SetProxy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SetProxy.cpp b/src/SetProxy.cpp index 8fb1b8ad..a40743d8 100644 --- a/src/SetProxy.cpp +++ b/src/SetProxy.cpp @@ -12,7 +12,7 @@ void SetProxy::start() QNetworkProxy proxy; if (arguments().size() > 0) { - proxy.setType((arguments()[4] == "socks") ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy); + proxy.setType((arguments()[4] == "socks5") ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy); proxy.setHostName(arguments()[0]); proxy.setPort((quint16)(arguments()[1].toInt())); proxy.setUser(arguments()[2]); From 0dd392f9d8a34f4bdd3ef03bec5b6cf6af904c90 Mon Sep 17 00:00:00 2001 From: addicted Date: Wed, 20 May 2015 19:20:14 +0300 Subject: [PATCH 4/9] browser#set_proxy tests copied --- Gemfile.lock | 4 + spec/browser_spec.rb | 240 ++++++++++++++++++++++++++++++------------- 2 files changed, 171 insertions(+), 73 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 1537c743..6c106a87 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -25,6 +25,7 @@ GEM ffi (1.9.3-java) ffi (1.9.3-x86-mingw32) json (1.8.1) + json (1.8.1-java) launchy (2.4.2) addressable (~> 2.3) launchy (2.4.2-java) @@ -37,6 +38,9 @@ GEM multi_json (1.8.4) nokogiri (1.6.4) mini_portile (~> 0.6.0) + nokogiri (1.6.4-java) + nokogiri (1.6.4-x86-mingw32) + mini_portile (~> 0.6.0) rack (1.5.2) rack-protection (1.3.2) rack diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index abf4d222..09a32dcf 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -158,93 +158,187 @@ end describe '#set_proxy' do - before do - @host = '127.0.0.1' - @user = 'user' - @pass = 'secret' - @url = "http://example.org/" - - @server = TCPServer.new(@host, 0) - @port = @server.addr[1] - - @proxy_requests = [] - @proxy = Thread.new(@server, @proxy_requests) do |serv, proxy_requests| - while conn = serv.accept do - # read request - request = [] - until (line = conn.readline.strip).empty? - request << line - end + context 'type: http' do + before do + @host = '127.0.0.1' + @user = 'user' + @pass = 'secret' + @url = "http://example.org/" + + @server = TCPServer.new(@host, 0) + @port = @server.addr[1] + + @proxy_requests = [] + @proxy = Thread.new(@server, @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 = "D'oh!" - 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 + # send response + auth_header = request.find { |h| h =~ /Authorization:/i } + if auth_header || request[0].split(/\s+/)[1] =~ /^\// + html = "D'oh!" + 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 eq 2 + @request = @proxy_requests[-1] end - browser.set_proxy(:host => @host, - :port => @port, - :user => @user, - :pass => @pass) - browser.visit @url - @proxy_requests.size.should eq 2 - @request = @proxy_requests[-1] - end + after do + @proxy.kill + @server.close + end - after do - @proxy.kill - @server.close - 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 '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 - 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 eq @user + pass.should eq @pass + end - user, pass = Base64.decode64(auth_header.split(/\s+/)[-1]).split(":") - user.should eq @user - pass.should eq @pass - end + it "uses the proxies' response" do + browser.body.should include "D'oh!" + end - it "uses the proxies' response" do - browser.body.should include "D'oh!" - end + it 'uses original URL' do + browser.current_url.should eq @url + end - it 'uses original URL' do - browser.current_url.should eq @url - end + it 'uses URLs changed by javascript' do + browser.execute_script "window.history.pushState('', '', '/blah')" + browser.current_url.should eq 'http://example.org/blah' + end - it 'uses URLs changed by javascript' do - browser.execute_script "window.history.pushState('', '', '/blah')" - browser.current_url.should eq 'http://example.org/blah' + it 'is possible to disable proxy again' do + @proxy_requests.clear + browser.clear_proxy + browser.visit "http://#{@host}:#{@port}/" + @proxy_requests.size.should eq 0 + end end + context 'type: :sock5' do + before do + @host = '127.0.0.1' + @user = 'user' + @pass = 'secret' + @url = "http://example.org/" + + @server = TCPServer.new(@host, 0) + @port = @server.addr[1] + + @proxy_requests = [] + @proxy = Thread.new(@server, @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 = "D'oh!" + 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, + :type => 'socks5') + browser.visit @url + @proxy_requests.size.should eq 2 + @request = @proxy_requests[-1] + end - it 'is possible to disable proxy again' do - @proxy_requests.clear - browser.clear_proxy - browser.visit "http://#{@host}:#{@port}/" - @proxy_requests.size.should eq 0 + after do + @proxy.kill + @server.close + end + + it 'uses the Socks 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 + p @request + 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 eq @user + pass.should eq @pass + end + + it "uses the proxies' response" do + browser.body.should include "D'oh!" + end + + it 'uses original URL' do + browser.current_url.should eq @url + end + + it 'uses URLs changed by javascript' do + browser.execute_script "window.history.pushState('', '', '/blah')" + browser.current_url.should eq 'http://example.org/blah' + end + + it 'is possible to disable proxy again' do + @proxy_requests.clear + browser.clear_proxy + browser.visit "http://#{@host}:#{@port}/" + @proxy_requests.size.should eq 0 + end end end From b2f2d7a1bf0f960c9f86c6a02368f96e17435a6d Mon Sep 17 00:00:00 2001 From: addicted Date: Wed, 20 May 2015 19:34:09 +0300 Subject: [PATCH 5/9] browser#set_proxy tests copied --- spec/browser_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index 09a32dcf..1f2b1153 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -266,7 +266,6 @@ 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] =~ /^\// @@ -293,6 +292,7 @@ :user => @user, :pass => @pass, :type => 'socks5') + browser.allow_url @url browser.visit @url @proxy_requests.size.should eq 2 @request = @proxy_requests[-1] @@ -304,6 +304,7 @@ end it 'uses the Socks proxy correctly' do + p @request @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 From f938ed1bb628bf7577d91f709eaecb038aeee8ea Mon Sep 17 00:00:00 2001 From: addicted Date: Thu, 21 May 2015 02:43:04 +0300 Subject: [PATCH 6/9] commented socks5 tests --- spec/browser_spec.rb | 49 ++-- spec/browser_spec.rb___jb_bak___ | 378 +++++++++++++++++++++++++++++++ 2 files changed, 406 insertions(+), 21 deletions(-) create mode 100644 spec/browser_spec.rb___jb_bak___ diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index 1f2b1153..52f3341f 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -255,35 +255,42 @@ @pass = 'secret' @url = "http://example.org/" - @server = TCPServer.new(@host, 0) + @server = TCPServer.new(@host, 2000) @port = @server.addr[1] - @proxy_requests = [] @proxy = Thread.new(@server, @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 = "D'oh!" 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 + conn.close + # # read request + # request = [] + # until (line = conn.readline.strip).empty? + # request << line + # end + # p request + # # send response + # auth_header = request.find { |h| h =~ /Authorization:/i } + # if auth_header || request[0].split(/\s+/)[1] =~ /^\// + # html = "D'oh!" + # 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 @@ -291,7 +298,8 @@ :port => @port, :user => @user, :pass => @pass, - :type => 'socks5') + :type => :socks5 + ) browser.allow_url @url browser.visit @url @proxy_requests.size.should eq 2 @@ -304,7 +312,6 @@ end it 'uses the Socks proxy correctly' do - p @request @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 diff --git a/spec/browser_spec.rb___jb_bak___ b/spec/browser_spec.rb___jb_bak___ new file mode 100644 index 00000000..5b6c7cc2 --- /dev/null +++ b/spec/browser_spec.rb___jb_bak___ @@ -0,0 +1,378 @@ +require 'spec_helper' +require 'self_signed_ssl_cert' +require 'stringio' +require 'capybara/webkit/driver' +require 'socket' +require 'base64' + +describe Capybara::Webkit::Browser do + + let(:connection) { Capybara::Webkit::Connection.new } + let(:browser) { Capybara::Webkit::Browser.new(connection) } + let(:browser_ignore_ssl_err) do + Capybara::Webkit::Browser.new(Capybara::Webkit::Connection.new).tap do |browser| + browser.ignore_ssl_errors + end + end + let(:browser_skip_images) do + Capybara::Webkit::Browser.new(Capybara::Webkit::Connection.new).tap do |browser| + browser.set_skip_image_loading(true) + end + end + + context 'handling of SSL validation errors' do + before do + # set up minimal HTTPS server + @host = "127.0.0.1" + @server = TCPServer.new(@host, 0) + @port = @server.addr[1] + + # set up SSL layer + ssl_serv = OpenSSL::SSL::SSLServer.new(@server, $openssl_self_signed_ctx) + + @server_thread = Thread.new(ssl_serv) do |serv| + while conn = serv.accept do + # read request + request = [] + until (line = conn.readline.strip).empty? + request << line + end + + # write response + html = "D'oh!" + 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 + end + end + end + + after do + @server_thread.kill + @server.close + end + + it "doesn't accept a self-signed certificate by default" do + lambda { browser.visit "https://#{@host}:#{@port}/" }.should raise_error + end + + it 'accepts a self-signed certificate if configured to do so' do + browser_ignore_ssl_err.visit "https://#{@host}:#{@port}/" + end + + it "doesn't accept a self-signed certificate in a new window by default" do + browser.execute_script("window.open('about:blank')") + browser.window_focus(browser.get_window_handles.last) + lambda { browser.visit "https://#{@host}:#{@port}/" }.should raise_error + end + + it 'accepts a self-signed certificate in a new window if configured to do so' do + browser_ignore_ssl_err.execute_script("window.open('about:blank')") + browser_ignore_ssl_err.window_focus(browser_ignore_ssl_err.get_window_handles.last) + browser_ignore_ssl_err.visit "https://#{@host}:#{@port}/" + end + end + + context "skip image loading" do + before(:each) do + # set up minimal HTTP server + @host = "127.0.0.1" + @server = TCPServer.new(@host, 0) + @port = @server.addr[1] + @received_requests = [] + + @server_thread = Thread.new do + while conn = @server.accept + Thread.new(conn) do |thread_conn| + # read request + request = [] + until (line = thread_conn.readline.strip).empty? + request << line + end + + @received_requests << request.join("\n") + + # write response + html = <<-HTML + + + + + + + + + HTML + thread_conn.write "HTTP/1.1 200 OK\r\n" + thread_conn.write "Content-Type:text/html\r\n" + thread_conn.write "Content-Length: %i\r\n" % html.size + thread_conn.write "\r\n" + thread_conn.write html + thread_conn.write("\r\n\r\n") + thread_conn.close + end + end + end + end + + after(:each) do + @server_thread.kill + @server.close + end + + it "should load images in image tags by default" do + browser.visit("http://#{@host}:#{@port}/") + @received_requests.find {|r| r =~ %r{/path/to/image} }.should_not be_nil + end + + it "should load images in css by default" do + browser.visit("http://#{@host}:#{@port}/") + @received_requests.find {|r| r =~ %r{/path/to/image} }.should_not be_nil + end + + it "should not load images in image tags when skip_image_loading is true" do + browser_skip_images.visit("http://#{@host}:#{@port}/") + @received_requests.find {|r| r =~ %r{/path/to/image} }.should be_nil + end + + it "should not load images in css when skip_image_loading is true" do + browser_skip_images.visit("http://#{@host}:#{@port}/") + @received_requests.find {|r| r =~ %r{/path/to/bgimage} }.should be_nil + end + end + + describe "forking", skip_on_windows: true, skip_on_jruby: true do + it "only shuts down the server from the main process" do + browser.reset! + pid = fork {} + Process.wait(pid) + expect { browser.reset! }.not_to raise_error + end + end + + describe '#set_proxy' do + context 'type: http' do + before do + @host = '127.0.0.1' + @user = 'user' + @pass = 'secret' + @url = "http://example.org/" + + @server = TCPServer.new(@host, 0) + @port = @server.addr[1] + + @proxy_requests = [] + @proxy = Thread.new(@server, @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 = "D'oh!" + 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 eq 2 + @request = @proxy_requests[-1] + end + + after do + @proxy.kill + @server.close + 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 eq @user + pass.should eq @pass + end + + it "uses the proxies' response" do + browser.body.should include "D'oh!" + end + + it 'uses original URL' do + browser.current_url.should eq @url + end + + it 'uses URLs changed by javascript' do + browser.execute_script "window.history.pushState('', '', '/blah')" + browser.current_url.should eq 'http://example.org/blah' + end + + it 'is possible to disable proxy again' do + @proxy_requests.clear + browser.clear_proxy + browser.visit "http://#{@host}:#{@port}/" + @proxy_requests.size.should eq 0 + end + end + context 'type: :sock5' do + # before do + # @host = '127.0.0.1' + # @user = 'user' + # @pass = 'secret' + # @url = "http://example.org/" + # + # @server = TCPServer.new(@host, 2000) + # @port = @server.addr[1] + # @proxy_requests = [] + # @proxy = Thread.new(@server, @proxy_requests) do |serv, proxy_requests| + # while conn = serv.accept do + # html = "D'oh!" + # 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 + # # # read request + # # request = [] + # # until (line = conn.readline.strip).empty? + # # request << line + # # end + # # p request + # # # send response + # # auth_header = request.find { |h| h =~ /Authorization:/i } + # # if auth_header || request[0].split(/\s+/)[1] =~ /^\// + # # html = "D'oh!" + # # 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, + # :type => :socks5 + # ) + # browser.allow_url @url + # browser.visit @url + # @proxy_requests.size.should eq 2 + # @request = @proxy_requests[-1] + # end + # + # after do + # @proxy.kill + # @server.close + # end + # + # it 'uses the Socks 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 + # p @request + # 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 eq @user + # pass.should eq @pass + # end + # + # it "uses the proxies' response" do + # browser.body.should include "D'oh!" + # end + # + # it 'uses original URL' do + # browser.current_url.should eq @url + # end + # + # it 'uses URLs changed by javascript' do + # browser.execute_script "window.history.pushState('', '', '/blah')" + # browser.current_url.should eq 'http://example.org/blah' + # end + # + # it 'is possible to disable proxy again' do + # @proxy_requests.clear + # browser.clear_proxy + # browser.visit "http://#{@host}:#{@port}/" + # @proxy_requests.size.should eq 0 + # end + end + end + + it "doesn't try to read an empty response" do + connection = double("connection") + connection.stub(:puts) + connection.stub(:print) + connection.stub(:gets).and_return("ok\n", "0\n") + connection.stub(:read).and_raise(StandardError.new("tried to read empty response")) + + browser = Capybara::Webkit::Browser.new(connection) + + expect { browser.visit("/") }.not_to raise_error + end + + describe '#command' do + context 'non-ok response' do + it 'raises an error of given class' do + error_json = '{"class": "ClickFailed"}' + + connection.should_receive(:gets).ordered.and_return 'error' + connection.should_receive(:gets).ordered.and_return error_json.bytesize + connection.stub read: error_json + + expect { browser.command 'blah', 'meh' }.to raise_error(Capybara::Webkit::ClickFailed) + end + end + end +end From 343a99d593aeea1c6e8891f5947b90cdc0983544 Mon Sep 17 00:00:00 2001 From: Oleg Date: Tue, 24 May 2016 19:53:46 +0300 Subject: [PATCH 7/9] removed extra lines from browser spec --- spec/browser_spec.rb | 201 ------------------------------------------- 1 file changed, 201 deletions(-) diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index 57f37bc0..8f8dd08f 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -19,207 +19,6 @@ expect { browser.reset! }.not_to raise_error end end -<<<<<<< HEAD - it 'test' do - require 'socket' - include Socket::Constants - socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) - socket.listen( 1 ) - end - describe '#set_proxy' do - context 'type: http' do - before do - @host = '127.0.0.1' - @user = 'user' - @pass = 'secret' - @url = "http://example.org/" - - @server = TCPServer.new(@host, 0) - @port = @server.addr[1] - - @proxy_requests = [] - @proxy = Thread.new(@server, @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 = "D'oh!" - 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 eq 2 - @request = @proxy_requests[-1] - end - - after do - @proxy.kill - @server.close - 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 eq @user - pass.should eq @pass - end - - it "uses the proxies' response" do - browser.body.should include "D'oh!" - end - - it 'uses original URL' do - browser.current_url.should eq @url - end - - it 'uses URLs changed by javascript' do - browser.execute_script "window.history.pushState('', '', '/blah')" - browser.current_url.should eq 'http://example.org/blah' - end - - it 'is possible to disable proxy again' do - @proxy_requests.clear - browser.clear_proxy - browser.visit "http://#{@host}:#{@port}/" - @proxy_requests.size.should eq 0 - end - end - context 'type: :sock5' do - before do - @host = '127.0.0.1' - @user = 'user' - @pass = 'secret' - @url = "http://example.org/" - - @server = TCPServer.new(@host, 2000) - @port = @server.addr[1] - @proxy_requests = [] - @proxy = Thread.new(@server, @proxy_requests) do |serv, proxy_requests| - while conn = serv.accept do - html = "D'oh!" - 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 - # # read request - # request = [] - # until (line = conn.readline.strip).empty? - # request << line - # end - # p request - # # send response - # auth_header = request.find { |h| h =~ /Authorization:/i } - # if auth_header || request[0].split(/\s+/)[1] =~ /^\// - # html = "D'oh!" - # 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, - :type => :socks5 - ) - browser.allow_url @url - browser.visit @url - @proxy_requests.size.should eq 2 - @request = @proxy_requests[-1] - end - - after do - @proxy.kill - @server.close - end - - it 'uses the Socks 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 - p @request - 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 eq @user - pass.should eq @pass - end - - it "uses the proxies' response" do - browser.body.should include "D'oh!" - end - - it 'uses original URL' do - browser.current_url.should eq @url - end - - it 'uses URLs changed by javascript' do - browser.execute_script "window.history.pushState('', '', '/blah')" - browser.current_url.should eq 'http://example.org/blah' - end - - it 'is possible to disable proxy again' do - @proxy_requests.clear - browser.clear_proxy - browser.visit "http://#{@host}:#{@port}/" - @proxy_requests.size.should eq 0 - end - end - end -======= ->>>>>>> 3ef6732baa2a3ed7a1b7ba5d6a464485779cebd4 it "doesn't try to read an empty response" do connection = double("connection") From 61979c23e816c65fca38766778918743d038575b Mon Sep 17 00:00:00 2001 From: Oleg Date: Tue, 24 May 2016 19:59:02 +0300 Subject: [PATCH 8/9] fixed hound offences --- lib/capybara/webkit/browser.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/capybara/webkit/browser.rb b/lib/capybara/webkit/browser.rb index 074f7f1b..a7c69efe 100644 --- a/lib/capybara/webkit/browser.rb +++ b/lib/capybara/webkit/browser.rb @@ -258,7 +258,8 @@ def get_cookies def set_proxy(options = {}) options = default_proxy_options.merge(options) - command("SetProxy", options[:host], options[:port], options[:user], options[:pass], options[:type]) + command "SetProxy", options[:host], options[:port], options[:user], + options[:pass], options[:type] end def clear_proxy @@ -321,11 +322,11 @@ def read_response def default_proxy_options { - :host => "localhost", - :port => "0", - :user => "", - :pass => "", - :type => "http" + host: "localhost", + port: "0", + user: "", + pass: "", + type: "http" } end end From d33b8039cb5205ee534f7d217ae86cf2fbd412ef Mon Sep 17 00:00:00 2001 From: Oleg Date: Tue, 24 May 2016 20:02:15 +0300 Subject: [PATCH 9/9] fixed hound offences for hash --- lib/capybara/webkit/browser.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/capybara/webkit/browser.rb b/lib/capybara/webkit/browser.rb index a7c69efe..07909ae5 100644 --- a/lib/capybara/webkit/browser.rb +++ b/lib/capybara/webkit/browser.rb @@ -322,11 +322,11 @@ def read_response def default_proxy_options { - host: "localhost", - port: "0", - user: "", - pass: "", - type: "http" + host: "localhost", + port: "0", + user: "", + pass: "", + type: "http", } end end