Skip to content

Commit

Permalink
feat: implement redirect? for Response (#392)
Browse files Browse the repository at this point in the history
* feat: implement `redirect?` for Response

* chore: fix linter

* chore: fix round test
  • Loading branch information
route authored Aug 22, 2023
1 parent 6c989a8 commit 502655a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

### Added
- `Ferrum::Network#cache(disable: true | false)` whether or not to use cache for every request
- `Ferrum::Network::Exchange#redirect?` determines if the exchange is a redirect
- `Ferrum::Network::Exchange#xhr?` determines if the exchange is XHR
- `Ferrum::Network::Request#xhr?` determines if the request is XHR
- `Ferrum::Network::Response#loaded?` returns true if the response is fully loaded
- `Ferrum::Network::Response#redirect?` returns true if the response is a redirect
- `Ferrum::Node#in_viewport?` checks if the element in viewport (optional argument `scope` as `Ferrum::Node`)
- `Ferrum::Node#scroll_into_view` - scrolls to element if needed (when it's not in the viewport)
- `Ferrum::Cookies#each` - is now Enumerable and supports `each` method
Expand Down
9 changes: 9 additions & 0 deletions lib/ferrum/network/exchange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ def xhr?
!!request&.xhr?
end

#
# Determines if the exchange is a redirect.
#
# @return [Boolean]
#
def redirect?
response&.redirect?
end

#
# Returns request's URL.
#
Expand Down
9 changes: 8 additions & 1 deletion lib/ferrum/network/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,15 @@ def loaded?
@loaded
end

# Whether the response is a redirect.
#
# Comapres the respones ID to another response's ID.
# @return [Boolean]
def redirect?
params.key?("redirectResponse")
end

#
# Compares the response's ID to another response's ID.
#
# @return [Boolean]
# Indicates whether the response has the same ID as the other response
Expand Down
8 changes: 8 additions & 0 deletions spec/network/exchange_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@
end
end

describe "#redirect?" do
it "determines if exchange is a redirect" do
page.go_to("/redirect_again")

expect(first_exchange.response.redirect?).to be
end
end

describe "#pending?" do
it "determines if exchange is not fully loaded" do
allow(page).to receive(:timeout) { 2 }
Expand Down
9 changes: 9 additions & 0 deletions spec/network/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
end
end

describe "#redirect?" do
it "captures errors" do
page.go_to("/redirect_again")

expect(page.body).to include("You landed")
expect(first_exchange.response.redirect?).to be
end
end

describe "#body_size" do
it "counts network traffic for each loaded resource" do
page.go_to("/ferrum/with_js")
Expand Down
4 changes: 2 additions & 2 deletions spec/page/screenshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def create_screenshot(**options)
browser.screenshot(path: file, quality: 0) # ignored for png
browser.screenshot(path: file2) # defaults to a quality of 75
browser.screenshot(path: file3, quality: 100)
expect(File.size(file)).to be > File.size(file2) # png by defult is bigger
expect(File.size(file)).to be > File.size(file2) # png by default is bigger
expect(File.size(file2)).to be < File.size(file3)
ensure
FileUtils.rm_f([file2, file3])
Expand Down Expand Up @@ -301,7 +301,7 @@ def create_screenshot(path:, **options)
reader.pages.each do |page|
bbox = page.attributes[:MediaBox]
width = (bbox[2] - bbox[0]) / 72
expect(width.round(2)).to eq(33.10)
expect(width.round(1)).to eq(33.1)
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/support/global_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def traffic
network.traffic
end

def first_exchange
traffic.first
end

def last_exchange
traffic.last
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def responsive?
return false if @server_thread&.join(0)

res = Net::HTTP.start(host, port, read_timeout: 2, max_retries: 0) { |h| h.get("/__identify__") }
return res.body == app.object_id.to_s if res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPRedirection)
res.body == app.object_id.to_s if res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPRedirection)
rescue SystemCallError, Net::ReadTimeout
false
end
Expand Down

0 comments on commit 502655a

Please sign in to comment.