From a981af27d611f3869d20500d7b8ba72484943136 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Mon, 26 Feb 2024 10:47:11 +0300 Subject: [PATCH] fix: Don't modify ws_url --- CHANGELOG.md | 2 ++ README.md | 2 +- lib/ferrum/browser/process.rb | 23 ++++++++++------------- spec/browser_spec.rb | 3 +-- spec/support/global_helpers.rb | 8 ++++++++ spec/unit/process_spec.rb | 2 ++ 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b862db3b..96256d47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ ### Fixed +- `:ws_url` option is now used without modifications WYSIWYG. + ### Removed diff --git a/README.md b/README.md index 350df06e..9af47332 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ Ferrum::Browser.new(options) * `:url` (String) - URL for a running instance of Chrome. If this is set, a browser process will not be spawned. * `:ws_url` (String) - Websocket url for a running instance of Chrome. If this is set, a - browser process will not be spawned. + browser process will not be spawned. It's higher priority than `:url`, setting both doesn't make sense. * `:process_timeout` (Integer) - How long to wait for the Chrome process to respond on startup. * `:ws_max_receive_size` (Integer) - How big messages to accept from Chrome diff --git a/lib/ferrum/browser/process.rb b/lib/ferrum/browser/process.rb index 2a754443..163cccfe 100644 --- a/lib/ferrum/browser/process.rb +++ b/lib/ferrum/browser/process.rb @@ -62,13 +62,11 @@ def self.directory_remover(path) def initialize(options) @pid = @xvfb = @user_data_dir = nil - if options.ws_url - response = parse_json_version(options.ws_url) - self.ws_url = options.ws_url - return - elsif options.url - response = parse_json_version(options.url) - self.ws_url = response&.[]("webSocketDebuggerUrl") + if options.ws_url || options.url + # `:ws_url` option is higher priority than `:url`, parse versions + # and use it as a ws_url, otherwise use what has been parsed. + response = parse_json_version(options.ws_url || options.url) + self.ws_url = options.ws_url || response&.[]("webSocketDebuggerUrl") return end @@ -185,11 +183,10 @@ def close_io(*ios) end def parse_json_version(url) - uri = Addressable::URI.parse(url) - uri.path = "/json/version" + url = URI.join(url, "/json/version") - if %w[wss ws].include?(uri.scheme) - uri.scheme = case uri.scheme + if %w[wss ws].include?(url.scheme) + url.scheme = case url.scheme when "ws" "http" when "wss" @@ -197,7 +194,7 @@ def parse_json_version(url) end end - response = JSON.parse(::Net::HTTP.get(URI(uri.to_s))) + response = JSON.parse(::Net::HTTP.get(URI(url.to_s))) @v8_version = response["V8-Version"] @browser_version = response["Browser"] @@ -206,7 +203,7 @@ def parse_json_version(url) @protocol_version = response["Protocol-Version"] response - rescue StandardError + rescue JSON::ParserError # nop end end diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index da3b1f88..6856f5c3 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -135,8 +135,7 @@ it "supports :ws_url argument" do with_external_browser do |url, process| - uri = Addressable::URI.parse(url) - browser = Ferrum::Browser.new(ws_url: "ws://#{uri.host}:#{uri.port}") + browser = Ferrum::Browser.new(ws_url: web_socket_debugger_url(url)) expect(process.v8_version).not_to be_nil expect(process.browser_version).not_to be_nil expect(process.webkit_version).not_to be_nil diff --git a/spec/support/global_helpers.rb b/spec/support/global_helpers.rb index 59266934..08f3853d 100644 --- a/spec/support/global_helpers.rb +++ b/spec/support/global_helpers.rb @@ -58,4 +58,12 @@ def with_external_browser(host: "127.0.0.1", port: 32_001) process.stop end end + + def web_socket_debugger_url(url) + uri = Addressable::URI.parse(url) + url = uri.join("/json/version").to_s + JSON.parse(Net::HTTP.get(URI(url)))["webSocketDebuggerUrl"] + rescue JSON::ParserError + # nop + end end diff --git a/spec/unit/process_spec.rb b/spec/unit/process_spec.rb index 519a2ee7..5ad6c435 100644 --- a/spec/unit/process_spec.rb +++ b/spec/unit/process_spec.rb @@ -10,6 +10,7 @@ allow(Ferrum::Client).to receive(:new).and_return(double.as_null_object) allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_ws_url) + allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_json_version) subject.send(:start) @@ -30,6 +31,7 @@ allow(Process).to receive(:spawn).with({ "LD_PRELOAD" => "some.so" }, any_args).and_return(123_456_789) allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_ws_url) + allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_json_version) subject.send(:start) subject.quit