Skip to content

Commit aac40d6

Browse files
committed
Raise when #authorize is used w/o block
1 parent 47f82c3 commit aac40d6

File tree

11 files changed

+87
-41
lines changed

11 files changed

+87
-41
lines changed

README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,24 +486,43 @@ browser.go_to("https://google.com")
486486

487487
#### authorize(\*\*options, &block)
488488

489-
If site uses authorization you can provide credentials using this method.
489+
If site or proxy uses authorization you can provide credentials using this method.
490490

491491
* options `Hash`
492492
* :type `Symbol` `:server` | `:proxy` site or proxy authorization
493493
* :user `String`
494494
* :password `String`
495-
* &block passes authenticated request, which you must subsequently allow or deny, if you don't
496-
care about unwanted requests call `continue`.
495+
* &block accepts authenticated request, which you must subsequently allow or deny, if you don't
496+
care about unwanted requests just call `request.continue`.
497497

498498
```ruby
499-
browser.network.authorize(user: "login", password: "pass") do |request|
500-
request.continue
501-
end
499+
browser.network.authorize(user: "login", password: "pass") { |req| req.continue }
502500
browser.go_to("http://example.com/authenticated")
503501
puts browser.network.status # => 200
504502
puts browser.body # => Welcome, authenticated client
505503
```
506504

505+
Since Chrome implements authorize using request interception you must continue or abort authorized requests. If you
506+
already have code that uses interception you can use `authorize` without block, but if not you are obliged to pass
507+
block, so this is version doesn't pass block and can work just fine:
508+
509+
```ruby
510+
browser = Ferrum::Browser.new
511+
browser.network.intercept
512+
browser.on(:request) do |request|
513+
if request.resource_type == "Image"
514+
request.abort
515+
else
516+
request.continue
517+
end
518+
end
519+
520+
browser.network.authorize(user: "login", password: "pass", type: :proxy)
521+
522+
browser.go_to("https://google.com")
523+
524+
```
525+
507526
You used to call `authorize` method without block, but since it's implemented using request interception there could be
508527
a collision with another part of your code that also uses request interception, so that authorize allows the request
509528
while your code denies but it's too late. The block is mandatory now.

lib/ferrum/browser/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def on(event, &block)
5858
end
5959
end
6060

61+
def subscribed?(event)
62+
[@interruptor, @subscriber].any? { |s| s.subscribed?(event) }
63+
end
64+
6165
def close
6266
@ws.close
6367
# Give a thread some time to handle a tail of messages

lib/ferrum/browser/subscriber.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def on(event, &block)
2121
true
2222
end
2323

24+
def subscribed?(event)
25+
@on.key?(event)
26+
end
27+
2428
def call(message)
2529
method, params = message.values_at("method", "params")
2630
total = @on[method].size

lib/ferrum/network.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,16 @@ def authorize(user:, password:, type: :server, &block)
8888
raise ArgumentError, ":type should be in #{AUTHORIZE_TYPE}"
8989
end
9090

91+
if !block_given? && !@page.subscribed?("Fetch.requestPaused")
92+
raise ArgumentError, "Block is missing, call `authorize(...) { |r| r.continue } or subscribe to `on(:request)` events before calling it"
93+
end
94+
9195
@authorized_ids ||= {}
9296
@authorized_ids[type] ||= []
9397

9498
intercept
9599

96-
if block_given?
97-
@page.on(:request, &block)
98-
else
99-
warn "[DEPRECATION] `authorize` without `&block` is deprecated. Please see details https://github.com/rubycdp/ferrum#authorizeoptions"
100-
101-
@page.on(:request) do |request|
102-
request.continue
103-
end
104-
end
100+
@page.on(:request, &block)
105101

106102
@page.on(:auth) do |request, index, total|
107103
if request.auth_challenge?(type)

lib/ferrum/node.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def find_position(x: nil, y: nil, position: :top)
142142

143143
get_position(points, x, y, position)
144144
rescue Ferrum::BrowserError => e
145-
return raise unless e.message&.include?('Could not compute content quads')
145+
return raise unless e.message&.include?("Could not compute content quads")
146146

147147
find_position_via_js
148148
end
@@ -151,8 +151,8 @@ def find_position(x: nil, y: nil, position: :top)
151151

152152
def find_position_via_js
153153
[
154-
evaluate('this.getBoundingClientRect().left + window.pageXOffset + (this.offsetWidth / 2)'), # x
155-
evaluate('this.getBoundingClientRect().top + window.pageYOffset + (this.offsetHeight / 2)') # y
154+
evaluate("this.getBoundingClientRect().left + window.pageXOffset + (this.offsetWidth / 2)"), # x
155+
evaluate("this.getBoundingClientRect().top + window.pageYOffset + (this.offsetHeight / 2)") # y
156156
]
157157
end
158158

lib/ferrum/page.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ def on(name, &block)
180180
end
181181
end
182182

183+
def subscribed?(event)
184+
@client.subscribed?(event)
185+
end
186+
183187
private
184188

185189
def subscribe

lib/ferrum/page/screenshot.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ def with_background_color(color)
195195
if color
196196
raise ArgumentError, "Accept Ferrum::RGBA class only" unless color.is_a?(RGBA)
197197

198-
command('Emulation.setDefaultBackgroundColorOverride', color: color.to_h)
198+
command("Emulation.setDefaultBackgroundColorOverride", color: color.to_h)
199199
end
200200

201201
yield
202202
ensure
203-
command('Emulation.setDefaultBackgroundColorOverride') if color
203+
command("Emulation.setDefaultBackgroundColorOverride") if color
204204
end
205205
end
206206
end

spec/browser_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ module Ferrum
9797
expect(browser.viewport_size).to eq([200, 400])
9898
end
9999

100-
context 'fullscreen' do
101-
shared_examples 'resize viewport by fullscreen' do
100+
context "fullscreen" do
101+
shared_examples "resize viewport by fullscreen" do
102102
it "allows the viewport to be resized by fullscreen" do
103103
expect(browser.viewport_size).to eq([1024, 768])
104104
browser.go_to(path)
@@ -107,12 +107,12 @@ module Ferrum
107107
end
108108
end
109109

110-
include_examples 'resize viewport by fullscreen' do
110+
include_examples "resize viewport by fullscreen" do
111111
let(:path) { "/ferrum/custom_html_size" }
112112
let(:viewport_size) { [1280, 1024] }
113113
end
114114

115-
include_examples 'resize viewport by fullscreen' do
115+
include_examples "resize viewport by fullscreen" do
116116
let(:path) { "/ferrum/custom_html_size_100%" }
117117
let(:viewport_size) { [1272, 1008] }
118118
end
@@ -564,7 +564,7 @@ module Ferrum
564564
it "does not run into content quads error" do
565565
browser.go_to("/ferrum/index")
566566

567-
allow_any_instance_of(Node).to receive(:get_content_quads).and_raise(Ferrum::BrowserError, 'message' => 'Could not compute content quads')
567+
allow_any_instance_of(Node).to receive(:get_content_quads).and_raise(Ferrum::BrowserError, "message" => "Could not compute content quads")
568568

569569
browser.at_xpath("//a[text() = 'JS redirect']").click
570570
expect(browser.body).to include("Hello world")

spec/network_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,25 @@ module Ferrum
201201
end
202202

203203
context "authentication support" do
204+
it "raises error when authorize is without block" do
205+
expect {
206+
browser.network.authorize(user: "login", password: "pass")
207+
}.to raise_exception(ArgumentError, "Block is missing, call `authorize(...) { |r| r.continue } or subscribe to `on(:request)` events before calling it")
208+
end
209+
210+
it "raises no error when authorize is with block" do
211+
expect {
212+
browser.network.authorize(user: "login", password: "pass") { |r| r.continue }
213+
}.not_to raise_error
214+
end
215+
216+
it "raises no error when authorize is without block but subscribed to events" do
217+
expect {
218+
browser.on(:request) { |r| r.continue }
219+
browser.network.authorize(user: "login", password: "pass")
220+
}.not_to raise_error
221+
end
222+
204223
it "denies without credentials" do
205224
browser.go_to("/ferrum/basic_auth")
206225

spec/rbga_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,43 @@
44

55
module Ferrum
66
describe RGBA do
7-
describe '#to_h' do
7+
describe "#to_h" do
88
it { expect(RGBA.new(0, 0, 0, 0.0).to_h).to eq({ r: 0, g: 0, b: 0, a: 0.0 }) }
99
it { expect(RGBA.new(255, 255, 255, 1.0).to_h).to eq({ r: 255, g: 255, b: 255, a: 1.0 }) }
1010
end
1111

12-
it 'raises ArgumentError for not Float value of alpha' do
12+
it "raises ArgumentError for not Float value of alpha" do
1313
expect {
1414
RGBA.new(0, 0, 0, 0)
1515
}.to raise_exception(
1616
ArgumentError,
17-
'Wrong alpha value 0 should be Float between 0.0 (fully transparent) and 1.0 (fully opaque)')
17+
"Wrong alpha value 0 should be Float between 0.0 (fully transparent) and 1.0 (fully opaque)")
1818
end
1919

20-
it 'raises ArgumentError wrong value of alpha' do
20+
it "raises ArgumentError wrong value of alpha" do
2121
expect {
2222
RGBA.new(0, 0, 0, 2.0)
2323
}.to raise_exception(
2424
ArgumentError,
25-
'Wrong alpha value 2.0 should be Float between 0.0 (fully transparent) and 1.0 (fully opaque)')
25+
"Wrong alpha value 2.0 should be Float between 0.0 (fully transparent) and 1.0 (fully opaque)")
2626
end
2727

28-
it 'raises ArgumentError for wrong value of red' do
28+
it "raises ArgumentError for wrong value of red" do
2929
expect {
3030
RGBA.new(-1, 0, 0, 0.0)
31-
}.to raise_exception(ArgumentError, 'Wrong value of -1 should be Integer from 0 to 255')
31+
}.to raise_exception(ArgumentError, "Wrong value of -1 should be Integer from 0 to 255")
3232
end
3333

34-
it 'raises ArgumentError for wrong value of green' do
34+
it "raises ArgumentError for wrong value of green" do
3535
expect {
3636
RGBA.new(0, 256, 0, 0.0)
37-
}.to raise_exception(ArgumentError, 'Wrong value of 256 should be Integer from 0 to 255')
37+
}.to raise_exception(ArgumentError, "Wrong value of 256 should be Integer from 0 to 255")
3838
end
3939

40-
it 'raises ArgumentError for wrong value of blue' do
40+
it "raises ArgumentError for wrong value of blue" do
4141
expect {
4242
RGBA.new(0, 0, 0.0, 0.0)
43-
}.to raise_exception(ArgumentError, 'Wrong value of 0.0 should be Integer from 0 to 255')
43+
}.to raise_exception(ArgumentError, "Wrong value of 0.0 should be Integer from 0 to 255")
4444
end
4545
end
4646
end

0 commit comments

Comments
 (0)