@@ -62,11 +62,15 @@ def self.directory_remover(path)
6262 def initialize ( options )
6363 @pid = @xvfb = @user_data_dir = nil
6464
65+ if options . ws_url
66+ response = parse_json_version ( options . ws_url )
67+ self . ws_url = response &.[]( "webSocketDebuggerUrl" ) || options . ws_url
68+ return
69+ end
70+
6571 if options . url
66- url = URI . join ( options . url , "/json/version" )
67- response = JSON . parse ( ::Net ::HTTP . get ( url ) )
68- self . ws_url = response [ "webSocketDebuggerUrl" ]
69- parse_browser_versions
72+ response = parse_json_version ( options . url )
73+ self . ws_url = response &.[]( "webSocketDebuggerUrl" )
7074 return
7175 end
7276
@@ -100,7 +104,7 @@ def start
100104 ObjectSpace . define_finalizer ( self , self . class . process_killer ( @pid ) )
101105
102106 parse_ws_url ( read_io , @process_timeout )
103- parse_browser_versions
107+ parse_json_version ( ws_url )
104108 ensure
105109 close_io ( read_io , write_io )
106110 end
@@ -174,25 +178,37 @@ def ws_url=(url)
174178 @port = @ws_url . port
175179 end
176180
177- def parse_browser_versions
178- return unless ws_url . is_a? ( Addressable ::URI )
181+ def close_io ( *ios )
182+ ios . each do |io |
183+ io . close unless io . closed?
184+ rescue IOError
185+ raise unless RUBY_ENGINE == "jruby"
186+ end
187+ end
188+
189+ def parse_json_version ( url )
190+ url = URI . join ( url , "/json/version" )
191+
192+ if %w[ wss ws ] . include? ( url . scheme )
193+ url . scheme = case url . scheme
194+ when "ws"
195+ "http"
196+ when "wss"
197+ "https"
198+ end
199+ end
179200
180- version_url = URI . parse ( ws_url . merge ( scheme : "http" , path : "/json/version" ) )
181- response = JSON . parse ( ::Net ::HTTP . get ( version_url ) )
201+ response = JSON . parse ( ::Net ::HTTP . get ( URI ( url . to_s ) ) )
182202
183203 @v8_version = response [ "V8-Version" ]
184204 @browser_version = response [ "Browser" ]
185205 @webkit_version = response [ "WebKit-Version" ]
186206 @default_user_agent = response [ "User-Agent" ]
187207 @protocol_version = response [ "Protocol-Version" ]
188- end
189208
190- def close_io ( *ios )
191- ios . each do |io |
192- io . close unless io . closed?
193- rescue IOError
194- raise unless RUBY_ENGINE == "jruby"
195- end
209+ response
210+ rescue StandardError
211+ # nop
196212 end
197213 end
198214 end
0 commit comments