Skip to content

Commit

Permalink
feat: Supplement request headers with Network.requestWillBeSentExtraI…
Browse files Browse the repository at this point in the history
…nfo (#506)
  • Loading branch information
route authored Dec 20, 2024
1 parent 885d5d4 commit e4e275d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ jobs:
with:
chrome-version: stable

- name: Fix GA Chrome Permissions
run: |
sudo chown root:root /opt/hostedtoolcache/setup-chrome/chromium/stable/x64/chrome-sandbox
sudo chmod 4755 /opt/hostedtoolcache/setup-chrome/chromium/stable/x64/chrome-sandbox
- name: Run tests
run: |
mkdir -p /tmp/ferrum
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Added

`Ferrum::Network::Request#headers` are enhanced and supplemented with `Network.requestWillBeSentExtraInfo` [#506]

### Changed

### Fixed
Expand Down
12 changes: 10 additions & 2 deletions lib/ferrum/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,24 @@ def subscribe_request_will_be_sent

# We can build exchange in two places, here on the event or when request
# is interrupted. So we have to be careful when to create new one. We
# create new exchange only if there's no with such id or there's but
# create new exchange only if there's no with such id or there's, but
# it's filled with request which means this one is new but has response
# for a redirect. So we assign response from the params to previous
# exchange and build new exchange to assign this request to it.
exchange = select(request.id).last
exchange = build_exchange(request.id) unless exchange&.blank?
exchange = build_exchange(request.id) if exchange.nil? || !exchange.blank?
request.headers.merge!(Hash(exchange.request_extra_info&.dig("headers")))
exchange.request = request

@exchange = exchange if exchange.navigation_request?(@page.main_frame.id)
end

@page.on("Network.requestWillBeSentExtraInfo") do |params|
exchange = select(params["requestId"]).last
exchange ||= build_exchange(params["requestId"])
exchange.request_extra_info = params
exchange.request&.headers&.merge!(params["headers"])
end
end

def subscribe_response_received
Expand Down
4 changes: 4 additions & 0 deletions lib/ferrum/network/exchange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Exchange
# @return [Error, nil]
attr_accessor :error

# @api private
attr_accessor :request_extra_info

#
# Initializes the network exchange.
#
Expand All @@ -40,6 +43,7 @@ def initialize(page, id)
@page = page
@intercepted_request = nil
@request = @response = @error = nil
@request_extra_info = nil
end

#
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
end

config.after(:all) do
@browser.quit
@browser&.quit
end

config.before(:each) do
Expand Down

0 comments on commit e4e275d

Please sign in to comment.