Skip to content

Commit

Permalink
fix: Don't modify ws_url
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Feb 26, 2024
1 parent 7df5fae commit a981af2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

### Fixed

- `:ws_url` option is now used without modifications WYSIWYG.

### Removed


Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 10 additions & 13 deletions lib/ferrum/browser/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -185,19 +183,18 @@ 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"
"https"
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"]
Expand All @@ -206,7 +203,7 @@ def parse_json_version(url)
@protocol_version = response["Protocol-Version"]

response
rescue StandardError
rescue JSON::ParserError
# nop
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/support/global_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions spec/unit/process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit a981af2

Please sign in to comment.