From 11ccf79e159f6a13ad87b7f3c58d68447fe75495 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Mon, 29 Jul 2024 15:44:28 +0300 Subject: [PATCH] fix: Network.requestWillBeSent should handle params in a type-safe way --- CHANGELOG.md | 1 + lib/ferrum/network.rb | 20 +++++++++----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d2c9a5d..0b44d47f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ ### Fixed - `:ws_url` option is now used without modifications WYSIWYG. +- `Network.requestWillBeSent` callback didn't handle params in a type-safe way ### Removed diff --git a/lib/ferrum/network.rb b/lib/ferrum/network.rb index 2ede0d41..6d8c2c31 100644 --- a/lib/ferrum/network.rb +++ b/lib/ferrum/network.rb @@ -368,26 +368,24 @@ def subscribe_request_will_be_sent @page.on("Network.requestWillBeSent") do |params| request = Network::Request.new(params) - # 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 - # 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? - # On redirects Chrome doesn't change `requestId` and there's no # `Network.responseReceived` event for such request. If there's already # exchange object with this id then we got redirected and params has # `redirectResponse` key which contains the response. - if params["redirectResponse"] - previous_exchange = select(request.id)[-2] + if params["redirectResponse"] && (previous_exchange = select(request.id).last) response = Network::Response.new(@page, params) response.loaded = true previous_exchange.response = response end + # 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 + # 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.request = request @exchange = exchange if exchange.navigation_request?(@page.main_frame.id)