Skip to content

Commit 47f82c3

Browse files
committed
Fix authorize method that now accepts block
1 parent 7d7db13 commit 47f82c3

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

README.md

Lines changed: 11 additions & 3 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,30 @@ end
484484
browser.go_to("https://google.com")
485485
```
486486

487-
#### authorize(\*\*options)
487+
#### authorize(\*\*options, &block)
488488

489489
If site 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`.
495497

496498
```ruby
497-
browser.network.authorize(user: "login", password: "pass")
499+
browser.network.authorize(user: "login", password: "pass") do |request|
500+
request.continue
501+
end
498502
browser.go_to("http://example.com/authenticated")
499503
puts browser.network.status # => 200
500504
puts browser.body # => Welcome, authenticated client
501505
```
502506

507+
You used to call `authorize` method without block, but since it's implemented using request interception there could be
508+
a collision with another part of your code that also uses request interception, so that authorize allows the request
509+
while your code denies but it's too late. The block is mandatory now.
510+
503511

504512
### Mouse
505513

lib/ferrum/network.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ 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
@@ -93,8 +93,14 @@ def authorize(user:, password:, type: :server)
9393

9494
intercept
9595

96-
@page.on(:request) do |request|
97-
request.continue
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
98104
end
99105

100106
@page.on(:auth) do |request, index, total|

spec/network_spec.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ module Ferrum
209209
end
210210

211211
it "allows with given credentials" do
212-
browser.network.authorize(user: "login", password: "pass")
212+
browser.network.authorize(user: "login", password: "pass") do |request|
213+
request.continue
214+
end
213215

214216
browser.go_to("/ferrum/basic_auth")
215217

@@ -218,7 +220,9 @@ module Ferrum
218220
end
219221

220222
it "allows even overwriting headers" do
221-
browser.network.authorize(user: "login", password: "pass")
223+
browser.network.authorize(user: "login", password: "pass") do |request|
224+
request.continue
225+
end
222226
browser.headers.set("Cuprite" => "true")
223227

224228
browser.go_to("/ferrum/basic_auth")
@@ -228,7 +232,9 @@ module Ferrum
228232
end
229233

230234
it "denies with wrong credentials" do
231-
browser.network.authorize(user: "user", password: "pass!")
235+
browser.network.authorize(user: "user", password: "pass!") do |request|
236+
request.continue
237+
end
232238

233239
browser.go_to("/ferrum/basic_auth")
234240

@@ -237,7 +243,9 @@ module Ferrum
237243
end
238244

239245
it "allows on POST request" do
240-
browser.network.authorize(user: "login", password: "pass")
246+
browser.network.authorize(user: "login", password: "pass") do |request|
247+
request.continue
248+
end
241249

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

0 commit comments

Comments
 (0)