Skip to content

Commit 3727388

Browse files
authored
Merge pull request #154 from rubycdp/fix-149-authorize
Fix #149 authorize method accepts block
2 parents 7d7db13 + aac40d6 commit 3727388

File tree

11 files changed

+106
-38
lines changed

11 files changed

+106
-38
lines changed

README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ browser.screenshot(path: "google.jpg") # => 30902
346346
# Save to Base64 the whole page not only viewport and reduce quality
347347
browser.screenshot(full: true, quality: 60) # "iVBORw0KGgoAAAANSUhEUgAABAAAAAMACAYAAAC6uhUNAAAAAXNSR0IArs4c6Q...
348348
# Save with specific background color
349-
browser.screenshot(background_color: Ferrum::RGBA.new(0, 0, 0, 0.0))
349+
browser.screenshot(background_color: Ferrum::RGBA.new(0, 0, 0, 0.0))
350350
```
351351

352352
#### pdf(\*\*options) : `String` | `Boolean`
@@ -484,22 +484,49 @@ end
484484
browser.go_to("https://google.com")
485485
```
486486

487-
#### authorize(\*\*options)
487+
#### 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 accepts authenticated request, which you must subsequently allow or deny, if you don't
496+
care about unwanted requests just call `request.continue`.
495497

496498
```ruby
497-
browser.network.authorize(user: "login", password: "pass")
499+
browser.network.authorize(user: "login", password: "pass") { |req| req.continue }
498500
browser.go_to("http://example.com/authenticated")
499501
puts browser.network.status # => 200
500502
puts browser.body # => Welcome, authenticated client
501503
```
502504

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+
526+
You used to call `authorize` method without block, but since it's implemented using request interception there could be
527+
a collision with another part of your code that also uses request interception, so that authorize allows the request
528+
while your code denies but it's too late. The block is mandatory now.
529+
503530

504531
### Mouse
505532

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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,21 @@ def intercept(pattern: "*", resource_type: nil)
8383
@page.command("Fetch.enable", handleAuthRequests: true, patterns: [pattern])
8484
end
8585

86-
def authorize(user:, password:, type: :server)
86+
def authorize(user:, password:, type: :server, &block)
8787
unless AUTHORIZE_TYPE.include?(type)
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-
@page.on(:request) do |request|
97-
request.continue
98-
end
100+
@page.on(:request, &block)
99101

100102
@page.on(:auth) do |request, index, total|
101103
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: 31 additions & 4 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

@@ -209,7 +228,9 @@ module Ferrum
209228
end
210229

211230
it "allows with given credentials" do
212-
browser.network.authorize(user: "login", password: "pass")
231+
browser.network.authorize(user: "login", password: "pass") do |request|
232+
request.continue
233+
end
213234

214235
browser.go_to("/ferrum/basic_auth")
215236

@@ -218,7 +239,9 @@ module Ferrum
218239
end
219240

220241
it "allows even overwriting headers" do
221-
browser.network.authorize(user: "login", password: "pass")
242+
browser.network.authorize(user: "login", password: "pass") do |request|
243+
request.continue
244+
end
222245
browser.headers.set("Cuprite" => "true")
223246

224247
browser.go_to("/ferrum/basic_auth")
@@ -228,7 +251,9 @@ module Ferrum
228251
end
229252

230253
it "denies with wrong credentials" do
231-
browser.network.authorize(user: "user", password: "pass!")
254+
browser.network.authorize(user: "user", password: "pass!") do |request|
255+
request.continue
256+
end
232257

233258
browser.go_to("/ferrum/basic_auth")
234259

@@ -237,7 +262,9 @@ module Ferrum
237262
end
238263

239264
it "allows on POST request" do
240-
browser.network.authorize(user: "login", password: "pass")
265+
browser.network.authorize(user: "login", password: "pass") do |request|
266+
request.continue
267+
end
241268

242269
browser.go_to("/ferrum/basic_auth")
243270
browser.at_css(%([type="submit"])).click

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)