Skip to content

Commit

Permalink
Silence CDP error upon fetching body for 301 response
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Sep 13, 2021
1 parent 29c01fd commit a4f8c86
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def intercept_request(id, params, &block)
def intercept_response(id, params)
return devtools.fetch.continue_request(request_id: id) unless block_given?

body = devtools.fetch.get_response_body(request_id: id).dig('result', 'body')
body = fetch_response_body(id)
original = DevTools::Response.from(id, body, params)
mutable = DevTools::Response.from(id, body, params)
yield mutable
Expand All @@ -115,14 +115,21 @@ def intercept_response(id, params)
else
devtools.fetch.fulfill_request(
request_id: id,
body: Base64.strict_encode64(mutable.body),
body: (Base64.strict_encode64(mutable.body) if mutable.body),
response_code: mutable.code,
response_headers: mutable.headers.map do |k, v|
{name: k, value: v}
end
)
end
end

def fetch_response_body(id)
devtools.fetch.get_response_body(request_id: id).dig('result', 'body')
rescue Error::WebDriverError
# CDP fails to get body on certain responses (301) and raises:
# Can only get response body on requests captured after headers received.
end
end # HasNetworkInterception
end # DriverExtensions
end # WebDriver
Expand Down
2 changes: 1 addition & 1 deletion rb/lib/selenium/webdriver/devtools/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.from(id, encoded_body, params)
new(
id: id,
code: params['responseStatusCode'],
body: Base64.strict_decode64(encoded_body),
body: (Base64.strict_decode64(encoded_body) if encoded_body),
headers: params['responseHeaders'].each_with_object({}) do |header, hash|
hash[header['name']] = header['value']
end
Expand Down

0 comments on commit a4f8c86

Please sign in to comment.