Skip to content

Commit b5a5ae8

Browse files
authored
Fix: Do not expect response for initiatorType Ping requests (#417)
* Add Request#response_expected? method * Add Exchange#response_expected? method * Exchange#finished? returns true if no response expected * Add HTML test for <a> with ping attribute
1 parent ec912a1 commit b5a5ae8

File tree

8 files changed

+95
-8
lines changed

8 files changed

+95
-8
lines changed

lib/ferrum/network/exchange.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def blocked?
7979
# @return [Boolean]
8080
#
8181
def finished?
82-
blocked? || response&.loaded? || !error.nil?
82+
blocked? || response&.loaded? || !error.nil? || !response_expected?
8383
end
8484

8585
#
@@ -118,6 +118,17 @@ def redirect?
118118
response&.redirect?
119119
end
120120

121+
#
122+
# Determines if the exchange expects a response
123+
#
124+
# @return [Boolean]
125+
#
126+
def response_expected?
127+
return true if request.nil?
128+
129+
!!request.response_expected?
130+
end
131+
121132
#
122133
# Returns request's URL.
123134
#

lib/ferrum/network/request.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ def time
8080
@time ||= Time.strptime(@params["wallTime"].to_s, "%s")
8181
end
8282

83+
#
84+
# Determines if a response is expected.
85+
#
86+
# @return [Boolean]
87+
#
88+
def response_expected?
89+
!type?("ping")
90+
end
91+
8392
#
8493
# Converts the request to a Hash.
8594
#

spec/network/exchange_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@
109109

110110
expect(last_exchange.finished?).to be true
111111
end
112+
113+
it "returns true if an error occurred" do
114+
exchange = Ferrum::Network::Exchange.new(page, "1")
115+
116+
expect(exchange.finished?).to be false
117+
exchange.error = Ferrum::Network::Error.new
118+
expect(exchange.finished?).to be true
119+
end
120+
121+
it "returns true for ping requests" do
122+
exchange = Ferrum::Network::Exchange.new(page, "1")
123+
expect(exchange.finished?).to be false
124+
125+
exchange.request = Ferrum::Network::Request.new({"type" => "Ping"})
126+
expect(exchange.finished?).to be true
127+
end
112128
end
113129

114130
describe "#redirect?" do
@@ -119,6 +135,16 @@
119135
end
120136
end
121137

138+
describe "#response_expected?" do
139+
it "determines if exchange expects a response" do
140+
exchange = Ferrum::Network::Exchange.new(page, "1")
141+
expect(exchange.response_expected?).to be true
142+
143+
exchange.request = Ferrum::Network::Request.new({"type" => "Ping"})
144+
expect(exchange.response_expected?).to be false
145+
end
146+
end
147+
122148
describe "#pending?" do
123149
it "determines if exchange is not fully loaded" do
124150
allow(page).to receive(:timeout) { 2 }

spec/network/request_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# frozen_string_literal: true
22

33
describe Ferrum::Network::Request do
4-
skip
4+
describe "#response_expected?" do
5+
it "returns true for document requests" do
6+
request = Ferrum::Network::Request.new({"type" => "Document"})
7+
8+
expect(request.response_expected?).to be(true)
9+
end
10+
11+
it "returns false for ping requests" do
12+
request = Ferrum::Network::Request.new({"type" => "Ping"})
13+
14+
expect(request.response_expected?).to be(false)
15+
end
16+
end
517
end

spec/network_spec.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,23 @@
4747
expect(page.body).to include("test_cookie")
4848
end
4949

50-
it "#idle?" do
51-
page.go_to("/ferrum/with_slow_ajax_connection")
52-
expect(page.at_xpath("//h1[text() = 'Slow AJAX']")).to be
50+
describe "#idle?" do
51+
it "waits for network to be idle" do
52+
page.go_to("/ferrum/with_slow_ajax_connection")
53+
expect(page.at_xpath("//h1[text() = 'Slow AJAX']")).to be
54+
55+
expect(network.idle?).to be_falsey
56+
network.wait_for_idle
57+
expect(network.idle?).to be_truthy
58+
end
5359

54-
expect(network.idle?).to be_falsey
55-
network.wait_for_idle
56-
expect(network.idle?).to be_truthy
60+
it "does not wait for responses to PING requests" do
61+
page.go_to("/ferrum/link_with_ping")
62+
page.at_css("a").click
63+
64+
network.wait_for_idle
65+
expect(network.idle?).to be_truthy
66+
end
5767
end
5868

5969
it "#total_connections" do

spec/node_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
expect(browser.current_url).to eq(base_url("/"))
1111
end
1212

13+
it "fires a ping request for anchor elements" do
14+
browser.go_to("/ferrum/link_with_ping")
15+
16+
expect(browser.network.traffic.length).to eq(1)
17+
browser.at_css("a").click
18+
19+
# 1 for first load, 1 for load of new url, 1 for ping = 3 total
20+
expect(browser.network.traffic.length).to eq(3)
21+
end
22+
1323
it "does not run into content quads error" do
1424
browser.go_to("/ferrum/index")
1525

spec/support/application.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ def authorized?(login, password)
326326
params["remaining_path"]
327327
end
328328

329+
post "/ferrum/ping" do
330+
# Sleeping to simulate a server that does not send a response to PING requests
331+
sleep 5
332+
halt 204
333+
end
334+
329335
protected
330336

331337
def render_view(view)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>
2+
<a href="/host" ping="/ferrum/ping">Link with ping</a>
3+
</p>

0 commit comments

Comments
 (0)