Skip to content

Commit 502655a

Browse files
authored
feat: implement redirect? for Response (#392)
* feat: implement `redirect?` for Response * chore: fix linter * chore: fix round test
1 parent 6c989a8 commit 502655a

File tree

8 files changed

+43
-4
lines changed

8 files changed

+43
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
### Added
44
- `Ferrum::Network#cache(disable: true | false)` whether or not to use cache for every request
5+
- `Ferrum::Network::Exchange#redirect?` determines if the exchange is a redirect
56
- `Ferrum::Network::Exchange#xhr?` determines if the exchange is XHR
67
- `Ferrum::Network::Request#xhr?` determines if the request is XHR
78
- `Ferrum::Network::Response#loaded?` returns true if the response is fully loaded
9+
- `Ferrum::Network::Response#redirect?` returns true if the response is a redirect
810
- `Ferrum::Node#in_viewport?` checks if the element in viewport (optional argument `scope` as `Ferrum::Node`)
911
- `Ferrum::Node#scroll_into_view` - scrolls to element if needed (when it's not in the viewport)
1012
- `Ferrum::Cookies#each` - is now Enumerable and supports `each` method

lib/ferrum/network/exchange.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ def xhr?
109109
!!request&.xhr?
110110
end
111111

112+
#
113+
# Determines if the exchange is a redirect.
114+
#
115+
# @return [Boolean]
116+
#
117+
def redirect?
118+
response&.redirect?
119+
end
120+
112121
#
113122
# Returns request's URL.
114123
#

lib/ferrum/network/response.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,15 @@ def loaded?
146146
@loaded
147147
end
148148

149+
# Whether the response is a redirect.
149150
#
150-
# Comapres the respones ID to another response's ID.
151+
# @return [Boolean]
152+
def redirect?
153+
params.key?("redirectResponse")
154+
end
155+
156+
#
157+
# Compares the response's ID to another response's ID.
151158
#
152159
# @return [Boolean]
153160
# Indicates whether the response has the same ID as the other response

spec/network/exchange_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@
111111
end
112112
end
113113

114+
describe "#redirect?" do
115+
it "determines if exchange is a redirect" do
116+
page.go_to("/redirect_again")
117+
118+
expect(first_exchange.response.redirect?).to be
119+
end
120+
end
121+
114122
describe "#pending?" do
115123
it "determines if exchange is not fully loaded" do
116124
allow(page).to receive(:timeout) { 2 }

spec/network/response_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@
7373
end
7474
end
7575

76+
describe "#redirect?" do
77+
it "captures errors" do
78+
page.go_to("/redirect_again")
79+
80+
expect(page.body).to include("You landed")
81+
expect(first_exchange.response.redirect?).to be
82+
end
83+
end
84+
7685
describe "#body_size" do
7786
it "counts network traffic for each loaded resource" do
7887
page.go_to("/ferrum/with_js")

spec/page/screenshot_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def create_screenshot(**options)
145145
browser.screenshot(path: file, quality: 0) # ignored for png
146146
browser.screenshot(path: file2) # defaults to a quality of 75
147147
browser.screenshot(path: file3, quality: 100)
148-
expect(File.size(file)).to be > File.size(file2) # png by defult is bigger
148+
expect(File.size(file)).to be > File.size(file2) # png by default is bigger
149149
expect(File.size(file2)).to be < File.size(file3)
150150
ensure
151151
FileUtils.rm_f([file2, file3])
@@ -301,7 +301,7 @@ def create_screenshot(path:, **options)
301301
reader.pages.each do |page|
302302
bbox = page.attributes[:MediaBox]
303303
width = (bbox[2] - bbox[0]) / 72
304-
expect(width.round(2)).to eq(33.10)
304+
expect(width.round(1)).to eq(33.1)
305305
end
306306
end
307307

spec/support/global_helpers.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def traffic
2323
network.traffic
2424
end
2525

26+
def first_exchange
27+
traffic.first
28+
end
29+
2630
def last_exchange
2731
traffic.last
2832
end

spec/support/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def responsive?
121121
return false if @server_thread&.join(0)
122122

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

0 commit comments

Comments
 (0)