dommy-rack 0.15.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +17 -3
- data/lib/dommy/rack/cookie_jar.rb +11 -188
- data/lib/dommy/rack/event_source_transport.rb +10 -4
- data/lib/dommy/rack/file_upload.rb +4 -4
- data/lib/dommy/rack/history.rb +12 -0
- data/lib/dommy/rack/navigation.rb +14 -6
- data/lib/dommy/rack/request_builder.rb +2 -2
- data/lib/dommy/rack/resources.rb +6 -0
- data/lib/dommy/rack/response.rb +27 -7
- data/lib/dommy/rack/session.rb +195 -25
- data/lib/dommy/rack/session_runtime.rb +77 -15
- data/lib/dommy/rack/version.rb +1 -1
- data/lib/dommy/rack/web_socket_transport.rb +11 -2
- data/sig/dommy/rack.rbs +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f8af313f77527147bc933b85aabef1ee539c86cc1ba8e9c49827b44bb0544bed
|
|
4
|
+
data.tar.gz: b8d89f41f1191a71c9d3869f04b2ce6fd871fbc70ccca180cd3b4571f6d06d8c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8885edb04121bfa10030fd45ca29fef75708d38a06836e4b7b7a84a198c9ed2eea155cf9d590355e97e6a6f7fca567e982c1586e4d5f29b7f44d687f074f124c
|
|
7
|
+
data.tar.gz: d3bf3386a970af499c6a83ef663d7bc1cf3c8001f483e9f2bede6b23b679bb8c928ccf23ba5e70dae98dbd103e7f0403de33efbe6057307f9150bbda6546f257
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.17.0 — 2026-10-20
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- `Session#navigate`, `Navigation#navigate` and `Navigation#run` take `method_override:` (false by default), saying the method is a form's `_method` field's, so a 301 or 302 is followed with a GET.
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- A form whose `_method` field overrides its method (Rails' `PUT`, `PATCH`, `DELETE`) follows a 301 or 302 with a GET, as the POST a browser sends is; the redirect target got the overridden method and answered 404. A 307 or 308 keeps the method and the params.
|
|
12
|
+
|
|
13
|
+
## 0.16.0 — 2026-10-10
|
|
14
|
+
|
|
15
|
+
### Breaking changes
|
|
16
|
+
|
|
17
|
+
- **`click_link` and `click_button` click the element as the page sees it, and return that element, with or without JavaScript**, as Capybara's and `click(selector)` do; without JavaScript they returned the navigation's Response. Read it from `last_response`, or use the request API (`visit`, `get` / `post`, `submit_form`, `click_link_element`), which returns a Response. With `javascript: true` the click is the page's to handle: Turbo, a client-side router or any listener can take the submission or the navigation over, and an un-prevented click submits or follows through the page; a Turbo Stream response then renders on a later frame, after `settle`. dommy-examples' signup browser specs build 19 pages' scripts instead of 35, because Turbo now keeps the page across its form submissions.
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
|
|
21
|
+
- `Session#resize_viewport(width, height)` resizes the browser window: the current page gets a `resize` event and re-evaluated `@media`, and later pages open at that size (`viewport_size` reads it back).
|
|
22
|
+
- `Session#virtual_time`, `Session#next_timer_delay` (the virtual ms to the page's next timer, nil when none), `Session#completion_pending?` (a response or message another thread handed back awaits the next turn of the loop), `Session#fetch_in_flight?` (a fetch is still running on the `network_executor`) and `Session#open_connections?` (a WebSocket or EventSource of the page or one of its frames is open), for a host waiting on the virtual clock.
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
|
|
26
|
+
- `Session#advance_time` moves the clock of the page and of every frame together, and `Session#next_timer_delay` is the soonest timer among them. A frame that navigated away or was removed is left off the clock: its timers stay unfired, and its fetches in flight and completions keep no one waiting.
|
|
27
|
+
- `Session#settle` settles the page's frames too: the ready work of every realm, going round again while one hands another work that is ready at once (a frame's `postMessage` to the page). An animation that requests frame after frame runs its next frame and lets `settle` return.
|
|
28
|
+
- Unloading a document closes the WebSockets and EventSources it opened, without waiting for their readers: the page's and its frames' when the page navigates away, a frame's when it navigates or its iframe is removed. A connection the app ended or dropped reports `closed?` too.
|
|
29
|
+
- A form the page submits (a click on a submit button with JavaScript, `form.submit()`, `requestSubmit()`) is recorded in the trace as `submit_form` records one.
|
|
30
|
+
- Navigating to a `text/plain`, CSS, JavaScript or JSON response shows it as a text document, a `<pre>` holding the body, so `session.text` is the body and `on_document_loaded` fires; other non-HTML responses still keep the previous document.
|
|
31
|
+
|
|
32
|
+
### Fixed
|
|
33
|
+
|
|
34
|
+
- A response's document is decoded in the charset its Content-Type names (a byte-order mark first, UTF-8 otherwise), so a Shift_JIS or ISO-8859-1 page no longer reads as U+FFFD.
|
|
35
|
+
|
|
3
36
|
## 0.15.0 — 2026-10-04
|
|
4
37
|
|
|
5
38
|
### Added
|
data/README.md
CHANGED
|
@@ -121,9 +121,23 @@ session.select("Tokyo", from: "City") # <select>
|
|
|
121
121
|
session.unselect("Tokyo", from: "City")
|
|
122
122
|
session.attach_file("Avatar", "/path/to/file.png")
|
|
123
123
|
|
|
124
|
-
session.click_button("Save") # submits the owning form
|
|
125
|
-
session.
|
|
126
|
-
|
|
124
|
+
session.click_button("Save") # submits the owning form; returns the button
|
|
125
|
+
session.last_response.status # what the click led to
|
|
126
|
+
session.submit_form(session.at_css("form")) # => the Response
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The methods that operate on the page — `fill_in`, `choose`, `check`,
|
|
130
|
+
`click_button`, `click_link`, `click` — return the element they acted on, as
|
|
131
|
+
Capybara's do; what an action led to is the page itself and
|
|
132
|
+
`session.last_response`. The request API — `visit`, `get` / `post` and the
|
|
133
|
+
other verbs, `submit_form`, `fetch` — returns the `Response`.
|
|
134
|
+
|
|
135
|
+
In a session with `javascript: true`, `click_button` and `click_link` click the
|
|
136
|
+
element as a browser would: the page's listeners see the click and the
|
|
137
|
+
`submit`, so Turbo or a client-side router can take the submission or the
|
|
138
|
+
navigation over, and an un-prevented one submits or follows through the page.
|
|
139
|
+
What the page does afterwards on a later frame (a Turbo Stream rendering, say)
|
|
140
|
+
shows after `session.settle`.
|
|
127
141
|
|
|
128
142
|
Locators are Capybara-style and depend on the element type:
|
|
129
143
|
fields match by `id`, `name`, label text, placeholder, or `aria-label`;
|
|
@@ -1,199 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "time"
|
|
4
|
-
|
|
5
3
|
module Dommy
|
|
6
4
|
module Rack
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
def initialize
|
|
17
|
-
@entries = []
|
|
18
|
-
# The jar is shared between the page thread (document.cookie) and network
|
|
19
|
-
# worker threads (request Cookie headers + Set-Cookie storage), so every
|
|
20
|
-
# touch of @entries is guarded. The lock is held only around the array
|
|
21
|
-
# access (no I/O, no reentrancy into the jar), so it never serializes the
|
|
22
|
-
# blocking HTTP itself.
|
|
23
|
-
@mutex = Mutex.new
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# Parse a single Set-Cookie header value and store the result.
|
|
5
|
+
# The session's cookie store: Dommy::CookieJar (RFC 6265bis storage and
|
|
6
|
+
# retrieval), shared by the requests the session sends to the app and by
|
|
7
|
+
# its pages' `document.cookie` / `cookieStore`, under the names this gem
|
|
8
|
+
# has always used. Thread-safe (network workers store and read too).
|
|
9
|
+
class CookieJar < Dommy::CookieJar
|
|
10
|
+
CookieEntry = Dommy::CookieJar::Cookie
|
|
11
|
+
|
|
12
|
+
# Store a response's Set-Cookie header value, received from `request_url`.
|
|
27
13
|
def store_from_header(set_cookie_string, request_url)
|
|
28
|
-
|
|
29
|
-
entry = parse_set_cookie(set_cookie_string, uri)
|
|
30
|
-
return unless entry
|
|
31
|
-
|
|
32
|
-
@mutex.synchronize do
|
|
33
|
-
if expired?(entry)
|
|
34
|
-
remove(entry.name, entry.domain, entry.path)
|
|
35
|
-
else
|
|
36
|
-
store_entry(entry)
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# Manually store a cookie. domain defaults to host-only on request_host.
|
|
42
|
-
def set!(name, value, domain: nil, path: "/", expires: nil, secure: false, http_only: false)
|
|
43
|
-
entry = CookieEntry.new(
|
|
44
|
-
name: name.to_s,
|
|
45
|
-
value: value.to_s,
|
|
46
|
-
domain: (domain || "").sub(/\A\./, "").downcase,
|
|
47
|
-
path: path || "/",
|
|
48
|
-
expires: expires,
|
|
49
|
-
secure: secure,
|
|
50
|
-
http_only: http_only,
|
|
51
|
-
host_only: domain.nil?
|
|
52
|
-
)
|
|
53
|
-
@mutex.synchronize { store_entry(entry) }
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
# First non-expired cookie value matching the name.
|
|
57
|
-
def get(name)
|
|
58
|
-
@mutex.synchronize { @entries.find { |e| e.name == name.to_s && !expired?(e) }&.value }
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def clear
|
|
62
|
-
@mutex.synchronize { @entries = [] }
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def all
|
|
66
|
-
@mutex.synchronize { @entries.reject { |e| expired?(e) } }
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
# Every non-expired cookie as a plain Hash (name/value/domain/path/expires/
|
|
70
|
-
# secure/http_only/host_only) — enough to round-trip the jar to disk and
|
|
71
|
-
# back via #import! without losing host-only scoping. `expires` is a Time or
|
|
72
|
-
# nil; the caller serializes it.
|
|
73
|
-
def export
|
|
74
|
-
all.map(&:to_h)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
# Restore a cookie from an #export Hash, preserving host_only exactly (unlike
|
|
78
|
-
# #set!, which infers it). Skips an already-expired entry. Symbol- or
|
|
79
|
-
# string-keyed Hashes both work, so a JSON round-trip is fine.
|
|
80
|
-
def import!(attrs)
|
|
81
|
-
h = attrs.transform_keys(&:to_sym)
|
|
82
|
-
entry = CookieEntry.new(
|
|
83
|
-
name: h[:name].to_s, value: h[:value].to_s,
|
|
84
|
-
domain: h[:domain].to_s, path: (h[:path] || "/"),
|
|
85
|
-
expires: h[:expires], secure: !!h[:secure],
|
|
86
|
-
http_only: !!h[:http_only], host_only: !!h[:host_only]
|
|
87
|
-
)
|
|
88
|
-
@mutex.synchronize { store_entry(entry) unless expired?(entry) }
|
|
14
|
+
store(set_cookie_string, request_url, http: true)
|
|
89
15
|
nil
|
|
90
16
|
end
|
|
91
17
|
|
|
92
|
-
#
|
|
93
|
-
def cookies_for(request_url)
|
|
94
|
-
uri = Dommy::URL.new(request_url)
|
|
95
|
-
secure_request = uri.protocol == "https:"
|
|
96
|
-
host = uri.hostname.downcase
|
|
97
|
-
path = uri.pathname.empty? ? "/" : uri.pathname
|
|
98
|
-
|
|
99
|
-
matches = @mutex.synchronize do
|
|
100
|
-
@entries.reject { |e| expired?(e) }.select do |e|
|
|
101
|
-
domain_match?(e, host) &&
|
|
102
|
-
path_match?(e.path, path) &&
|
|
103
|
-
(!e.secure || secure_request)
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
# More specific (longer) paths first, per RFC 6265 (on the snapshot copy,
|
|
108
|
-
# outside the lock).
|
|
109
|
-
matches.sort_by! { |e| -e.path.length }
|
|
110
|
-
matches.map { |e| "#{e.name}=#{e.value}" }.join("; ")
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
private
|
|
114
|
-
|
|
115
|
-
def store_entry(entry)
|
|
116
|
-
remove(entry.name, entry.domain, entry.path)
|
|
117
|
-
@entries << entry
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def remove(name, domain, path)
|
|
121
|
-
@entries.reject! { |e| e.name == name && e.domain == domain && e.path == path }
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
def parse_set_cookie(string, request_uri)
|
|
125
|
-
segments = string.split(";").map(&:strip)
|
|
126
|
-
name_value = segments.shift.to_s
|
|
127
|
-
return nil unless name_value.include?("=")
|
|
128
|
-
|
|
129
|
-
name, value = name_value.split("=", 2)
|
|
130
|
-
name = name.to_s.strip
|
|
131
|
-
return nil if name.empty?
|
|
132
|
-
|
|
133
|
-
attrs = parse_attributes(segments)
|
|
134
|
-
request_host = request_uri.hostname.downcase
|
|
135
|
-
|
|
136
|
-
domain = attrs["domain"]
|
|
137
|
-
host_only = domain.nil? || domain.empty?
|
|
138
|
-
domain = host_only ? request_host : domain.sub(/\A\./, "").downcase
|
|
139
|
-
|
|
140
|
-
CookieEntry.new(
|
|
141
|
-
name: name,
|
|
142
|
-
value: value.to_s.strip,
|
|
143
|
-
domain: domain,
|
|
144
|
-
path: attrs["path"] || default_path(request_uri),
|
|
145
|
-
expires: resolve_expiry(attrs),
|
|
146
|
-
secure: attrs.key?("secure"),
|
|
147
|
-
http_only: attrs.key?("httponly"),
|
|
148
|
-
host_only: host_only
|
|
149
|
-
)
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
def parse_attributes(segments)
|
|
153
|
-
segments.each_with_object({}) do |segment, acc|
|
|
154
|
-
key, val = segment.split("=", 2)
|
|
155
|
-
acc[key.to_s.strip.downcase] = val&.strip
|
|
156
|
-
end
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
# Max-Age takes precedence over Expires.
|
|
160
|
-
def resolve_expiry(attrs)
|
|
161
|
-
if attrs["max-age"]
|
|
162
|
-
seconds = attrs["max-age"].to_i
|
|
163
|
-
Time.now + seconds
|
|
164
|
-
elsif attrs["expires"]
|
|
165
|
-
Time.parse(attrs["expires"]) rescue nil
|
|
166
|
-
end
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
# RFC 6265 default-path: directory portion of the request path.
|
|
170
|
-
def default_path(uri)
|
|
171
|
-
path = uri.pathname
|
|
172
|
-
return "/" if path.empty? || !path.start_with?("/")
|
|
173
|
-
|
|
174
|
-
idx = path.rindex("/")
|
|
175
|
-
idx.nil? || idx.zero? ? "/" : path[0...idx]
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
def expired?(entry)
|
|
179
|
-
entry.expires && entry.expires <= Time.now
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
def domain_match?(entry, host)
|
|
183
|
-
if entry.host_only
|
|
184
|
-
host == entry.domain
|
|
185
|
-
else
|
|
186
|
-
host == entry.domain || host.end_with?(".#{entry.domain}")
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
# RFC 6265 path-match.
|
|
191
|
-
def path_match?(cookie_path, request_path)
|
|
192
|
-
return true if cookie_path == request_path
|
|
193
|
-
return false unless request_path.start_with?(cookie_path)
|
|
194
|
-
|
|
195
|
-
cookie_path.end_with?("/") || request_path[cookie_path.length] == "/"
|
|
196
|
-
end
|
|
18
|
+
# The Cookie request header value for `request_url`, or "".
|
|
19
|
+
def cookies_for(request_url) = cookie_string(request_url, http: true)
|
|
197
20
|
end
|
|
198
21
|
end
|
|
199
22
|
end
|
|
@@ -46,6 +46,10 @@ module Dommy
|
|
|
46
46
|
@reader = Thread.new { run }
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
+
# Whether the stream is over, so no event can arrive on it: the page
|
|
50
|
+
# closed it, or the reader stopped (the response ended or failed).
|
|
51
|
+
def closed? = @closed || @reader_done
|
|
52
|
+
|
|
49
53
|
# Called from the page thread when the EventSource is closed: stop the
|
|
50
54
|
# reader and release the response body.
|
|
51
55
|
def close
|
|
@@ -54,11 +58,12 @@ module Dommy
|
|
|
54
58
|
nil
|
|
55
59
|
end
|
|
56
60
|
|
|
57
|
-
# Hard teardown
|
|
58
|
-
#
|
|
59
|
-
|
|
61
|
+
# Hard teardown: drop the stream; the reader exits on the closed body
|
|
62
|
+
# or at the next chunk. With `wait` it is also joined (briefly), for a
|
|
63
|
+
# session going away for good; a page navigating away does not wait.
|
|
64
|
+
def dispose(wait: true)
|
|
60
65
|
close
|
|
61
|
-
@reader&.join(1)
|
|
66
|
+
@reader&.join(1) if wait
|
|
62
67
|
rescue IOError
|
|
63
68
|
nil
|
|
64
69
|
end
|
|
@@ -82,6 +87,7 @@ module Dommy
|
|
|
82
87
|
post { @es.__internal_transport_error__ } unless @closed
|
|
83
88
|
ensure
|
|
84
89
|
close_body
|
|
90
|
+
@reader_done = true
|
|
85
91
|
end
|
|
86
92
|
|
|
87
93
|
def env
|
|
@@ -18,7 +18,7 @@ module Dommy
|
|
|
18
18
|
def multipart?(pairs)
|
|
19
19
|
return false unless pairs
|
|
20
20
|
|
|
21
|
-
pairs.any? { |(_name, value)| value.
|
|
21
|
+
pairs.any? { |(_name, value)| value.is_a?(Dommy::Blob) }
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
# Returns [body (ASCII-8BIT String), content_type with boundary].
|
|
@@ -32,8 +32,8 @@ module Dommy
|
|
|
32
32
|
|
|
33
33
|
def part(boundary, name, value)
|
|
34
34
|
head = +"--#{boundary}\r\n"
|
|
35
|
-
if value.
|
|
36
|
-
filename = value.
|
|
35
|
+
if value.is_a?(Dommy::Blob)
|
|
36
|
+
filename = value.is_a?(Dommy::File) ? value.name.to_s : ""
|
|
37
37
|
content_type = file_content_type(value)
|
|
38
38
|
head << %(Content-Disposition: form-data; name="#{escape(name)}"; filename="#{escape(filename)}"\r\n)
|
|
39
39
|
head << "Content-Type: #{content_type}\r\n\r\n"
|
|
@@ -45,7 +45,7 @@ module Dommy
|
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
def file_content_type(value)
|
|
48
|
-
type = value.
|
|
48
|
+
type = value.type.to_s
|
|
49
49
|
type.empty? ? "application/octet-stream" : type
|
|
50
50
|
end
|
|
51
51
|
|
data/lib/dommy/rack/history.rb
CHANGED
|
@@ -42,6 +42,18 @@ module Dommy
|
|
|
42
42
|
current_entry
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
+
# Move the cursor by `delta` entries and return that Entry, or nil (cursor
|
|
46
|
+
# unchanged) when the target is out of range.
|
|
47
|
+
def go(delta)
|
|
48
|
+
target = @index + delta.to_i
|
|
49
|
+
return nil if delta.to_i.zero? || target.negative? || target >= @stack.size
|
|
50
|
+
|
|
51
|
+
@index = target
|
|
52
|
+
current_entry
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def length = @stack.size
|
|
56
|
+
|
|
45
57
|
# replaceState: the current entry's URL changes in place.
|
|
46
58
|
def replace_current_url(url)
|
|
47
59
|
current_entry&.url = url
|
|
@@ -48,7 +48,9 @@ module Dommy
|
|
|
48
48
|
|
|
49
49
|
# Perform a navigation, following redirects per session policy, then
|
|
50
50
|
# apply the final response to the session (updating document + history).
|
|
51
|
-
|
|
51
|
+
# `method_override:` marks a method a form's `_method` field gave, which a
|
|
52
|
+
# redirect treats as the POST a browser sends (see #run).
|
|
53
|
+
def navigate(method:, url:, params: nil, body: nil, enctype: nil, headers: {}, replace: false, method_override: false)
|
|
52
54
|
return navigate_about(url.to_s) if url.to_s.start_with?("about:")
|
|
53
55
|
|
|
54
56
|
verb = method.to_s.upcase
|
|
@@ -64,7 +66,8 @@ module Dommy
|
|
|
64
66
|
end
|
|
65
67
|
check_same_origin!(target)
|
|
66
68
|
|
|
67
|
-
response, final_url = run(method: verb, url: target, params: params, body: body, enctype: enctype, headers: headers
|
|
69
|
+
response, final_url = run(method: verb, url: target, params: params, body: body, enctype: enctype, headers: headers,
|
|
70
|
+
method_override: method_override)
|
|
68
71
|
# replace: a location.replace() / reload() / redirect updates the current
|
|
69
72
|
# history entry in place rather than pushing a new one.
|
|
70
73
|
@session.apply_navigation_response(response, final_url, replace: replace)
|
|
@@ -100,7 +103,12 @@ module Dommy
|
|
|
100
103
|
|
|
101
104
|
# Run the request/redirect loop. Returns [response, final_url].
|
|
102
105
|
# Public so Session#fetch can reuse it without applying navigation state.
|
|
103
|
-
|
|
106
|
+
#
|
|
107
|
+
# A `method_override` request carries the method a form's `_method` field
|
|
108
|
+
# gave (the session resolves it for the app), but what a browser sends is
|
|
109
|
+
# a POST, which a 301/302 turns into a GET; a real PUT keeps its method
|
|
110
|
+
# (Fetch), and a 307/308 keeps either one, with its params.
|
|
111
|
+
def run(method:, url:, params: nil, body: nil, enctype: nil, headers: {}, follow: true, exchange: nil, method_override: false)
|
|
104
112
|
verb = method
|
|
105
113
|
target = url
|
|
106
114
|
# Carry the fragment across redirects: a redirect Location without its
|
|
@@ -139,7 +147,7 @@ module Dommy
|
|
|
139
147
|
params = nil
|
|
140
148
|
body = nil
|
|
141
149
|
end
|
|
142
|
-
verb = redirect_method(response.status, verb)
|
|
150
|
+
verb = redirect_method(response.status, verb, method_override)
|
|
143
151
|
end
|
|
144
152
|
end
|
|
145
153
|
|
|
@@ -218,10 +226,10 @@ module Dommy
|
|
|
218
226
|
!response.location_header.to_s.empty?
|
|
219
227
|
end
|
|
220
228
|
|
|
221
|
-
def redirect_method(status, original)
|
|
229
|
+
def redirect_method(status, original, method_override)
|
|
222
230
|
case status
|
|
223
231
|
when 303 then "GET"
|
|
224
|
-
when 301, 302 then original == "POST" ? "GET" : original
|
|
232
|
+
when 301, 302 then original == "POST" || method_override ? "GET" : original
|
|
225
233
|
else original # 307, 308 keep the method
|
|
226
234
|
end
|
|
227
235
|
end
|
|
@@ -130,9 +130,9 @@ module Dommy
|
|
|
130
130
|
# File/Blob values can appear in a GET form; reduce them to their
|
|
131
131
|
# filename so build_query never serializes raw bytes into a query.
|
|
132
132
|
def scalar(value)
|
|
133
|
-
return value.to_s unless value.
|
|
133
|
+
return value.to_s unless value.is_a?(Dommy::Blob)
|
|
134
134
|
|
|
135
|
-
value.
|
|
135
|
+
value.is_a?(Dommy::File) ? value.name.to_s : ""
|
|
136
136
|
end
|
|
137
137
|
|
|
138
138
|
def normalize_headers(headers)
|
data/lib/dommy/rack/resources.rb
CHANGED
|
@@ -19,6 +19,12 @@ module Dommy
|
|
|
19
19
|
|
|
20
20
|
def get(url, headers: {}) = request(method: "GET", url: url, headers: headers)
|
|
21
21
|
|
|
22
|
+
# Whether a GET of `url` would be served: what a copy kept from an
|
|
23
|
+
# earlier page (Dommy::Js::ModulePreload) asks before it stands in for
|
|
24
|
+
# the request, so a blocked or declined host is recorded and refused as
|
|
25
|
+
# a request to it would be.
|
|
26
|
+
def serves?(url) = !served_target(method: "GET", url: url, headers: {}, body: nil).nil?
|
|
27
|
+
|
|
22
28
|
def request(method:, url:, headers: {}, body: nil)
|
|
23
29
|
target = served_target(method: method, url: url, headers: headers, body: body)
|
|
24
30
|
return nil unless target
|
data/lib/dommy/rack/response.rb
CHANGED
|
@@ -9,6 +9,9 @@ module Dommy
|
|
|
9
9
|
# and the Dommy document is parsed lazily on first access.
|
|
10
10
|
class Response
|
|
11
11
|
HTML_CONTENT_TYPES = ["text/html", "application/xhtml+xml"].freeze
|
|
12
|
+
# Besides JavaScript and JSON, the types a browser loads as a text
|
|
13
|
+
# document (HTML's page load processing model for text files).
|
|
14
|
+
TEXT_CONTENT_TYPES = ["text/plain", "text/css", "text/vtt"].freeze
|
|
12
15
|
REDIRECT_STATUSES = [301, 302, 303, 307, 308].freeze
|
|
13
16
|
|
|
14
17
|
attr_reader :status, :headers, :url
|
|
@@ -44,6 +47,17 @@ module Dommy
|
|
|
44
47
|
HTML_CONTENT_TYPES.include?(content_type)
|
|
45
48
|
end
|
|
46
49
|
|
|
50
|
+
# Whether a navigation to this response shows the body as text: a
|
|
51
|
+
# document holding one <pre> with the body in it, as a browser renders
|
|
52
|
+
# a stylesheet, a script, JSON or a text/plain error page.
|
|
53
|
+
def text?
|
|
54
|
+
TEXT_CONTENT_TYPES.include?(content_type) || json? ||
|
|
55
|
+
::Dommy::HTMLScriptElement.javascript_mime_type_essence_match?(content_type)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Whether a navigation to this response replaces the page's document.
|
|
59
|
+
def document? = html? || text?
|
|
60
|
+
|
|
47
61
|
# True when the response advertises a JSON content type, including
|
|
48
62
|
# structured-suffix types such as application/vnd.api+json.
|
|
49
63
|
def json?
|
|
@@ -101,7 +115,7 @@ module Dommy
|
|
|
101
115
|
Array(values).flat_map { |v| v.to_s.split("\n") }.reject(&:empty?)
|
|
102
116
|
end
|
|
103
117
|
|
|
104
|
-
# The parsed Dommy window, or nil for
|
|
118
|
+
# The parsed Dommy window, or nil for a response that gets no document.
|
|
105
119
|
def window
|
|
106
120
|
parse_document! unless @document_parsed
|
|
107
121
|
@window
|
|
@@ -126,12 +140,18 @@ module Dommy
|
|
|
126
140
|
|
|
127
141
|
def parse_document!
|
|
128
142
|
@document_parsed = true
|
|
129
|
-
return unless
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
#
|
|
133
|
-
#
|
|
134
|
-
|
|
143
|
+
return unless document?
|
|
144
|
+
|
|
145
|
+
# Decoded as the response declares (a byte-order mark, the
|
|
146
|
+
# Content-Type's charset, an HTML page's `<meta charset>`, else
|
|
147
|
+
# UTF-8), and the document told which encoding that was.
|
|
148
|
+
text, encoding = ::Dommy::Encodings.decode_document(@body, header("content-type"), html: html?)
|
|
149
|
+
@window = html? ? Dommy.parse(text, encoding: encoding) : ::Dommy::Internal::ChildNavigable.text_window(text, content_type)
|
|
150
|
+
@window.document.__internal_character_encoding__ = encoding
|
|
151
|
+
# Establish the document's URL (not a navigation: nothing is asked of
|
|
152
|
+
# the navigation delegate, and the first session history entry is this
|
|
153
|
+
# URL).
|
|
154
|
+
@window.location.__internal_set_url__(@url) if @url
|
|
135
155
|
@window.document.content_type = content_type if content_type
|
|
136
156
|
end
|
|
137
157
|
|
data/lib/dommy/rack/session.rb
CHANGED
|
@@ -26,6 +26,21 @@ module Dommy
|
|
|
26
26
|
def traverse(delta)
|
|
27
27
|
@session.__internal_enqueue_page_traverse__(@window, delta)
|
|
28
28
|
end
|
|
29
|
+
|
|
30
|
+
# `history.length` is the session's joint history.
|
|
31
|
+
def history_length = @session.history.length
|
|
32
|
+
|
|
33
|
+
# `window.stop()`: drop the page's navigation not yet performed.
|
|
34
|
+
def stop = @session.__internal_stop_page_navigation__(@window)
|
|
35
|
+
|
|
36
|
+
# An iframe's child navigable navigating to `url` (its `src`, or a
|
|
37
|
+
# navigation from inside it): with `load_frames: true` the session
|
|
38
|
+
# fetches it from the app and answers the document's Window; otherwise
|
|
39
|
+
# nil, and the frame keeps its document (#within_frame fetches on
|
|
40
|
+
# demand instead).
|
|
41
|
+
def load_frame(frame, url:, method: "GET", body: nil, params: nil, enctype: nil, headers: {}, **)
|
|
42
|
+
@session.__internal_load_frame__(url: url, method: method, body: body, params: params, enctype: enctype, headers: headers)
|
|
43
|
+
end
|
|
29
44
|
end
|
|
30
45
|
|
|
31
46
|
# A single browser-like session over a Rack application. Owns the current
|
|
@@ -84,8 +99,14 @@ module Dommy
|
|
|
84
99
|
trace: false,
|
|
85
100
|
trace_level: :verbose,
|
|
86
101
|
trace_dom: false,
|
|
87
|
-
trace_snapshots: false
|
|
102
|
+
trace_snapshots: false,
|
|
103
|
+
load_frames: false)
|
|
88
104
|
@app = app
|
|
105
|
+
# Navigate iframes from their `src` the way a browser does (a request
|
|
106
|
+
# to the app per frame, from a task before the page's load event).
|
|
107
|
+
# Off by default: a page's frames are left at their initial
|
|
108
|
+
# about:blank, and #within_frame fetches a frame when asked.
|
|
109
|
+
@load_frames = load_frames
|
|
89
110
|
# Fail on anything the page's JavaScript left unhandled, at the next
|
|
90
111
|
# checkpoint. Off by default so an embedding browser (which only reads
|
|
91
112
|
# `js_errors`) is unaffected; a test front end turns it on.
|
|
@@ -126,6 +147,10 @@ module Dommy
|
|
|
126
147
|
@headers = HeaderStore.new
|
|
127
148
|
@navigation = Navigation.new(self, @config)
|
|
128
149
|
@history = History.new
|
|
150
|
+
# Web Storage shared by every page of this session (see
|
|
151
|
+
# Dommy::StorageProvider): localStorage per origin, sessionStorage per
|
|
152
|
+
# origin for this one top-level browsing session.
|
|
153
|
+
@storage_provider = Dommy::StorageProvider.new
|
|
129
154
|
@current_url = nil
|
|
130
155
|
@current_window = nil
|
|
131
156
|
@last_request = nil
|
|
@@ -191,9 +216,9 @@ module Dommy
|
|
|
191
216
|
result
|
|
192
217
|
end
|
|
193
218
|
|
|
194
|
-
# Settle work ready at the current virtual
|
|
195
|
-
#
|
|
196
|
-
# #advance_time.
|
|
219
|
+
# Settle the page and its frames: the work ready at the current virtual
|
|
220
|
+
# time (microtasks, due-now timers, the next requestAnimationFrame) in
|
|
221
|
+
# every realm. A future setTimeout(ms) needs #advance_time.
|
|
197
222
|
def settle
|
|
198
223
|
require_js!.settle
|
|
199
224
|
__internal_flush_page_navigation__
|
|
@@ -208,6 +233,43 @@ module Dommy
|
|
|
208
233
|
self
|
|
209
234
|
end
|
|
210
235
|
|
|
236
|
+
# The current page's virtual clock in ms (nil when JS is disabled).
|
|
237
|
+
def virtual_time = @js_runtime&.now_ms
|
|
238
|
+
|
|
239
|
+
# The virtual ms until the current page's next timer is due, or nil when
|
|
240
|
+
# none is scheduled (or JS is disabled). Moving the clock that far is the
|
|
241
|
+
# soonest the page can change by itself.
|
|
242
|
+
def next_timer_delay = @js_runtime&.next_timer_delay
|
|
243
|
+
|
|
244
|
+
# Whether a completion another thread handed back (a response, a
|
|
245
|
+
# socket message) waits to be delivered: the next turn of the event
|
|
246
|
+
# loop (#advance_time, even by 0) applies it.
|
|
247
|
+
def completion_pending?
|
|
248
|
+
@js_runtime ? @js_runtime.external_pending? : false
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# Whether a fetch the page handed to the `network_executor` is still
|
|
252
|
+
# running on a worker. Its response will change the page without the
|
|
253
|
+
# clock moving, so only waiting in real time sees it arrive; once the
|
|
254
|
+
# worker hands it back it is #completion_pending?. Without an executor
|
|
255
|
+
# (the default) fetch completes before it returns.
|
|
256
|
+
def fetch_in_flight?
|
|
257
|
+
@js_runtime ? @js_runtime.external_work_in_flight? : false
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Whether a WebSocket or EventSource of the page (or of one of its
|
|
261
|
+
# frames) is open, so the app can push to the page at any moment —
|
|
262
|
+
# something the virtual clock does not drive, which only waiting in
|
|
263
|
+
# real time can see arrive. A connection whose window was unloaded (its
|
|
264
|
+
# frame navigated or removed) is closed here; a closed one carries
|
|
265
|
+
# nothing more and nothing brings it back (a reconnecting page opens a
|
|
266
|
+
# new one), so it leaves the list.
|
|
267
|
+
def open_connections?
|
|
268
|
+
close_unloaded_connections
|
|
269
|
+
@page_connections&.reject! { |_window, transport| transport.closed? }
|
|
270
|
+
!Array(@page_connections).empty?
|
|
271
|
+
end
|
|
272
|
+
|
|
211
273
|
# Uncaught JS errors / unhandled rejections and console output collected
|
|
212
274
|
# by the JS runtime ([] when JS is disabled). Only what the PAGE left
|
|
213
275
|
# unhandled is here: an error it cancels in `window.onerror` never reaches
|
|
@@ -240,16 +302,15 @@ module Dommy
|
|
|
240
302
|
@js_runtime&.error_log&.check!(context: @current_url)
|
|
241
303
|
end
|
|
242
304
|
|
|
243
|
-
# Full session teardown: the JS runtime(s) plus
|
|
244
|
-
#
|
|
305
|
+
# Full session teardown: the JS runtime(s) plus the page's open
|
|
306
|
+
# WebSockets and EventSources. Safe to call when JS is disabled, and
|
|
307
|
+
# repeatedly.
|
|
245
308
|
# Tear the session down, then fail on anything the page left unhandled and
|
|
246
309
|
# nobody has reported. Disposing first means the failure cannot leave live
|
|
247
310
|
# realms behind.
|
|
248
311
|
def dispose
|
|
249
|
-
Array(@
|
|
250
|
-
@
|
|
251
|
-
Array(@live_event_source_transports).each(&:dispose)
|
|
252
|
-
@live_event_source_transports = nil
|
|
312
|
+
Array(@page_connections).each { |_window, transport| transport.dispose }
|
|
313
|
+
@page_connections = nil
|
|
253
314
|
log = @js_runtime&.error_log
|
|
254
315
|
dispose_js
|
|
255
316
|
log&.check!(context: @current_url)
|
|
@@ -277,7 +338,7 @@ module Dommy
|
|
|
277
338
|
app: @app, ws: ws, scheduler: window.scheduler, url: target,
|
|
278
339
|
origin: target.origin, cookie_string: @cookie_jar.cookies_for(target.to_s)
|
|
279
340
|
)
|
|
280
|
-
(@
|
|
341
|
+
(@page_connections ||= []) << [window, transport]
|
|
281
342
|
transport
|
|
282
343
|
end
|
|
283
344
|
end
|
|
@@ -299,7 +360,7 @@ module Dommy
|
|
|
299
360
|
origin: target.origin,
|
|
300
361
|
cookie_string: with_credentials ? @cookie_jar.cookies_for(target.to_s) : ""
|
|
301
362
|
)
|
|
302
|
-
(@
|
|
363
|
+
(@page_connections ||= []) << [window, transport]
|
|
303
364
|
transport
|
|
304
365
|
end
|
|
305
366
|
end
|
|
@@ -381,6 +442,21 @@ module Dommy
|
|
|
381
442
|
# Internal: Resources records a declined cross-origin host here.
|
|
382
443
|
def __internal_record_blocked_subresource(host) = @subresource_policy.record_blocked(host)
|
|
383
444
|
|
|
445
|
+
# --- Window size ---
|
|
446
|
+
|
|
447
|
+
# The browser window's viewport as [width, height] in CSS px, or nil while
|
|
448
|
+
# the session has not been resized (each page then has Dommy's default).
|
|
449
|
+
attr_reader :viewport_size
|
|
450
|
+
|
|
451
|
+
# Resize the browser window. Like a browser's, the size outlives the
|
|
452
|
+
# page: the current one sees @media / matchMedia re-evaluated and a
|
|
453
|
+
# `resize` event, and every page loaded later starts at this size.
|
|
454
|
+
def resize_viewport(width, height)
|
|
455
|
+
@viewport_size = [Integer(width), Integer(height)]
|
|
456
|
+
@current_window&.resize_to(*@viewport_size)
|
|
457
|
+
self
|
|
458
|
+
end
|
|
459
|
+
|
|
384
460
|
# --- Navigation API ---
|
|
385
461
|
|
|
386
462
|
# Navigate to `path` (GET). For a `javascript: true` session, the loaded
|
|
@@ -399,9 +475,9 @@ module Dommy
|
|
|
399
475
|
result
|
|
400
476
|
end
|
|
401
477
|
|
|
402
|
-
def navigate(method: "GET", url:, params: nil, body: nil, enctype: nil, headers: {}, replace: false)
|
|
478
|
+
def navigate(method: "GET", url:, params: nil, body: nil, enctype: nil, headers: {}, replace: false, method_override: false)
|
|
403
479
|
@navigation.navigate(method: method, url: url, params: params, body: body, enctype: enctype,
|
|
404
|
-
headers: headers, replace: replace)
|
|
480
|
+
headers: headers, replace: replace, method_override: method_override)
|
|
405
481
|
end
|
|
406
482
|
|
|
407
483
|
def reload
|
|
@@ -460,6 +536,14 @@ module Dommy
|
|
|
460
536
|
@pending_navigation = {traverse: delta}
|
|
461
537
|
end
|
|
462
538
|
|
|
539
|
+
def __internal_stop_page_navigation__(window)
|
|
540
|
+
@pending_navigation = nil if window.equal?(@current_window)
|
|
541
|
+
nil
|
|
542
|
+
end
|
|
543
|
+
|
|
544
|
+
# The provider of this session's Web Storage areas.
|
|
545
|
+
attr_reader :storage_provider
|
|
546
|
+
|
|
463
547
|
# Perform a recorded page navigation, if any. Called after the JS runtime
|
|
464
548
|
# drains so the document/realm swap never runs with the outgoing realm's
|
|
465
549
|
# JS on the stack.
|
|
@@ -469,7 +553,7 @@ module Dommy
|
|
|
469
553
|
|
|
470
554
|
@pending_navigation = nil
|
|
471
555
|
if nav.key?(:traverse)
|
|
472
|
-
traverse_history(nav[:traverse]
|
|
556
|
+
traverse_history(nav[:traverse])
|
|
473
557
|
else
|
|
474
558
|
perform_page_navigation(nav)
|
|
475
559
|
end
|
|
@@ -558,6 +642,28 @@ module Dommy
|
|
|
558
642
|
@navigation.fetch(url, method: method, params: params, body: body, headers: headers, redirect: redirect)
|
|
559
643
|
end
|
|
560
644
|
|
|
645
|
+
# Whether the session loads iframes from their `src` (see `load_frames:`).
|
|
646
|
+
def load_frames? = @load_frames == true
|
|
647
|
+
|
|
648
|
+
# Internal: fetch a frame navigation (`load_frames: true`) and build the
|
|
649
|
+
# Window of its document — an empty, opaque-origin error page when the
|
|
650
|
+
# request failed. nil when the session does not load frames.
|
|
651
|
+
def __internal_load_frame__(url:, method: "GET", body: nil, params: nil, enctype: nil, headers: {})
|
|
652
|
+
return nil unless load_frames?
|
|
653
|
+
|
|
654
|
+
response = begin
|
|
655
|
+
@navigation.fetch(url, method: method, params: params, body: body, enctype: enctype,
|
|
656
|
+
headers: headers.merge(referer_headers))
|
|
657
|
+
rescue Error
|
|
658
|
+
nil
|
|
659
|
+
end
|
|
660
|
+
return Dommy::Internal::ChildNavigable.error_window(url) unless response
|
|
661
|
+
|
|
662
|
+
window = Dommy::Internal::ChildNavigable.window_for_response(response.body, response.headers.find { |k, _| k.to_s.casecmp?("content-type") }&.last, response.url || url)
|
|
663
|
+
window.approximate_layout = true if @approximate_layout
|
|
664
|
+
window
|
|
665
|
+
end
|
|
666
|
+
|
|
561
667
|
# --- Current page state ---
|
|
562
668
|
|
|
563
669
|
def current_url = @current_url
|
|
@@ -709,9 +815,19 @@ module Dommy
|
|
|
709
815
|
|
|
710
816
|
# --- Link navigation ---
|
|
711
817
|
|
|
818
|
+
# click_link / click_button operate on the page and return the element
|
|
819
|
+
# they clicked, with or without JavaScript, as Capybara's do; what the
|
|
820
|
+
# click led to is the page, and #last_response. (With JavaScript the
|
|
821
|
+
# page may take the click over, so a response is not theirs to return.)
|
|
822
|
+
# The request API — #visit, #get / #post, #submit_form,
|
|
823
|
+
# #click_link_element — is what returns a Response.
|
|
712
824
|
def click_link(locator)
|
|
713
825
|
@trace&.__internal_open_action(:click_link, locator)
|
|
714
|
-
|
|
826
|
+
link = finder.find_link(locator)
|
|
827
|
+
return click_through_page(link) if javascript?
|
|
828
|
+
|
|
829
|
+
click_link_element(link)
|
|
830
|
+
link
|
|
715
831
|
end
|
|
716
832
|
|
|
717
833
|
def click_link_element(element)
|
|
@@ -738,11 +854,12 @@ module Dommy
|
|
|
738
854
|
def click_button(locator)
|
|
739
855
|
@trace&.__internal_open_action(:click_button, locator)
|
|
740
856
|
button = finder.find_button(locator)
|
|
857
|
+
return click_through_page(button) if javascript?
|
|
858
|
+
|
|
741
859
|
# Only submit buttons submit a form. type=button / type=reset are
|
|
742
860
|
# no-ops here since there is no JavaScript to handle their click.
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
submit_form(finder.form_for(button), submitter: button)
|
|
861
|
+
submit_form(finder.form_for(button), submitter: button) if submit_button?(button)
|
|
862
|
+
button
|
|
746
863
|
end
|
|
747
864
|
|
|
748
865
|
def submit_form(form, submitter: nil)
|
|
@@ -751,7 +868,8 @@ module Dommy
|
|
|
751
868
|
result = FormSubmission.new(form, submitter, @config).submit!
|
|
752
869
|
@trace&.__internal_record_form(method: result[:method], url: result[:url], params: result[:params])
|
|
753
870
|
navigate(method: result[:method], url: resolve_document_url(result[:url]),
|
|
754
|
-
params: result[:params], enctype: result[:enctype], headers: referer_headers
|
|
871
|
+
params: result[:params], enctype: result[:enctype], headers: referer_headers,
|
|
872
|
+
method_override: result[:method_override])
|
|
755
873
|
end
|
|
756
874
|
|
|
757
875
|
alias submit submit_form
|
|
@@ -812,7 +930,12 @@ module Dommy
|
|
|
812
930
|
|
|
813
931
|
# One step of the joint back/forward traversal (see #back).
|
|
814
932
|
def traverse_history(direction)
|
|
815
|
-
target =
|
|
933
|
+
target =
|
|
934
|
+
case direction
|
|
935
|
+
when :back then @history.back
|
|
936
|
+
when :forward then @history.forward
|
|
937
|
+
else @history.go(direction.to_i)
|
|
938
|
+
end
|
|
816
939
|
return nil unless target
|
|
817
940
|
|
|
818
941
|
if target.window&.equal?(@current_window) && target.windex
|
|
@@ -848,16 +971,31 @@ module Dommy
|
|
|
848
971
|
end
|
|
849
972
|
|
|
850
973
|
# Apply a final navigation response: update last_response, current_url,
|
|
851
|
-
# the document (HTML
|
|
974
|
+
# the document (HTML, or text shown in a <pre>), and the history stack.
|
|
852
975
|
def apply_navigation_response(response, final_url, push_history: true, replace: false)
|
|
853
976
|
@last_response = response
|
|
854
977
|
@current_url = final_url
|
|
855
|
-
if response.
|
|
978
|
+
if response.document?
|
|
979
|
+
previous_window = @current_window
|
|
856
980
|
@current_window = response.window
|
|
981
|
+
@current_window.storage_provider = @storage_provider
|
|
982
|
+
# document.cookie / cookieStore read and write the jar the session's
|
|
983
|
+
# requests use.
|
|
984
|
+
@current_window.cookie_jar = @cookie_jar
|
|
985
|
+
referrer = @last_request && @last_request["HTTP_REFERER"]
|
|
986
|
+
@current_window.document.__internal_referrer__ = referrer.to_s if referrer
|
|
987
|
+
# The page navigated away from is no longer fully active.
|
|
988
|
+
if previous_window && !previous_window.equal?(@current_window)
|
|
989
|
+
previous_window.__internal_discard__
|
|
990
|
+
close_unloaded_connections
|
|
991
|
+
end
|
|
857
992
|
@current_window.dialog_handler = @dialog_handler
|
|
858
993
|
# Set the geometry mode before scripts boot so the very first
|
|
859
994
|
# getBoundingClientRect a framework calls already sees it.
|
|
860
995
|
@current_window.approximate_layout = @approximate_layout if @approximate_layout
|
|
996
|
+
# A new page opens at the window's size, before any script or style
|
|
997
|
+
# reads it; nothing resized, so no `resize` event.
|
|
998
|
+
apply_viewport_size(@current_window) if @viewport_size
|
|
861
999
|
# Fill external stylesheets before listeners (script boot /
|
|
862
1000
|
# DOMContentLoaded) run, so CSS-driven computed styles and :visible
|
|
863
1001
|
# are correct from the first observation.
|
|
@@ -959,6 +1097,35 @@ module Dommy
|
|
|
959
1097
|
|
|
960
1098
|
private
|
|
961
1099
|
|
|
1100
|
+
# With JavaScript, a click is the page's to handle, as in a browser: its
|
|
1101
|
+
# listeners see it (Turbo, a framework's router), and an un-prevented
|
|
1102
|
+
# one runs the element's activation behavior — a submit button submits
|
|
1103
|
+
# its form, a link is followed — through the page's own navigation.
|
|
1104
|
+
def click_through_page(element)
|
|
1105
|
+
with_interaction { Dommy::Interaction::EventSynthesis.click(element) }
|
|
1106
|
+
element
|
|
1107
|
+
end
|
|
1108
|
+
|
|
1109
|
+
# Unloading a document closes the WebSockets and EventSources it opened
|
|
1110
|
+
# (HTML "unload a document"): those of every window no longer fully
|
|
1111
|
+
# active — the page navigated away from and its frames, a frame that
|
|
1112
|
+
# navigated, a removed iframe's. The readers are not waited for: one
|
|
1113
|
+
# blocked in a streaming body ends on its own.
|
|
1114
|
+
def close_unloaded_connections
|
|
1115
|
+
@page_connections&.reject! do |window, transport|
|
|
1116
|
+
next false if window.__internal_fully_active__?
|
|
1117
|
+
|
|
1118
|
+
transport.dispose(wait: false)
|
|
1119
|
+
true
|
|
1120
|
+
end
|
|
1121
|
+
end
|
|
1122
|
+
|
|
1123
|
+
def apply_viewport_size(window)
|
|
1124
|
+
env = window.media_environment
|
|
1125
|
+
env.viewport_width, env.viewport_height = @viewport_size
|
|
1126
|
+
window.__internal_media_environment_changed__
|
|
1127
|
+
end
|
|
1128
|
+
|
|
962
1129
|
# Fire `listeners` with `arg`, inline or (with a `sched`) posted to its
|
|
963
1130
|
# inbox so they run later on the page thread. Shared by #page_exchange
|
|
964
1131
|
# and #build_worker_exchange, which otherwise hand-rolled the same
|
|
@@ -1057,11 +1224,14 @@ module Dommy
|
|
|
1057
1224
|
return unless %w[http https].include?(uri_scheme(target))
|
|
1058
1225
|
return if nav[:params].nil? && same_page_fragment?(target)
|
|
1059
1226
|
|
|
1060
|
-
|
|
1227
|
+
requested = (nav[:method] || "GET").to_s.upcase
|
|
1228
|
+
method = requested
|
|
1061
1229
|
params = nav[:params]
|
|
1062
1230
|
method, params = apply_delegate_method_override(method, params) if params
|
|
1231
|
+
# A form the page submitted is traced as #submit_form's are.
|
|
1232
|
+
@trace&.__internal_record_form(method: method, url: target, params: params) if nav[:source] == :form
|
|
1063
1233
|
navigate(method: method, url: target, params: params, body: nav[:body], enctype: nav[:enctype],
|
|
1064
|
-
headers: referer_headers, replace: nav[:replace])
|
|
1234
|
+
headers: referer_headers, replace: nav[:replace], method_override: method != requested)
|
|
1065
1235
|
end
|
|
1066
1236
|
|
|
1067
1237
|
# The delegate path serializes forms through core FormSubmission, which
|
|
@@ -15,6 +15,9 @@ module Dommy
|
|
|
15
15
|
# driver's JS support — one realm manager, two front ends.
|
|
16
16
|
class SessionRuntime
|
|
17
17
|
PUMP_SLICE_MS = 50
|
|
18
|
+
# How many times #settle goes round the realms still handing each other
|
|
19
|
+
# work, against two that never stop.
|
|
20
|
+
SETTLE_ROUNDS = 10
|
|
18
21
|
|
|
19
22
|
# `current_document` yields the document execute/evaluate should target
|
|
20
23
|
# (the session's current document by default; the Capybara driver passes
|
|
@@ -67,18 +70,35 @@ module Dommy
|
|
|
67
70
|
|
|
68
71
|
def supports_script_args? = current_runtime.respond_to?(:execute_with_args)
|
|
69
72
|
|
|
70
|
-
# Settle
|
|
71
|
-
#
|
|
73
|
+
# Settle the page: in every live realm (the top window's and its
|
|
74
|
+
# frames'), the work ready at its current virtual time — microtasks,
|
|
75
|
+
# due-now timers, the next animation frame's callbacks. A realm that
|
|
76
|
+
# settled can hand another one work that is ready at once (a
|
|
77
|
+
# postMessage to the parent, a response a worker handed back), so the
|
|
78
|
+
# realms left with such work are settled again, until none is. A
|
|
79
|
+
# realm's next animation frame does not count: an animation that
|
|
80
|
+
# requests one frame after another would never let the page settle.
|
|
81
|
+
# Snapshot iteration: a settling realm may navigate and replace the map.
|
|
72
82
|
def settle
|
|
73
|
-
|
|
83
|
+
realms = live_realms
|
|
84
|
+
SETTLE_ROUNDS.times do
|
|
85
|
+
realms.each { |_doc, runtime| runtime.settle }
|
|
86
|
+
realms = live_realms.select { |doc, _runtime| ready_work?(scheduler_of(doc)) }
|
|
87
|
+
break if realms.empty?
|
|
88
|
+
end
|
|
74
89
|
self
|
|
75
90
|
end
|
|
76
91
|
|
|
77
|
-
# Advance the
|
|
78
|
-
#
|
|
92
|
+
# Advance the page's virtual clock: every live realm's (the top window's
|
|
93
|
+
# and its frames'), which move together as one page's time does, running
|
|
94
|
+
# the timers that come due and the completions handed back, then
|
|
95
|
+
# draining each realm's microtasks. Snapshot iteration: a fired timer
|
|
96
|
+
# may navigate and replace the map.
|
|
79
97
|
def advance_time(ms)
|
|
80
|
-
|
|
81
|
-
|
|
98
|
+
live_realms.each do |doc, runtime|
|
|
99
|
+
scheduler_of(doc)&.advance_time(ms)
|
|
100
|
+
runtime.drain_microtasks
|
|
101
|
+
end
|
|
82
102
|
self
|
|
83
103
|
end
|
|
84
104
|
|
|
@@ -89,14 +109,34 @@ module Dommy
|
|
|
89
109
|
self
|
|
90
110
|
end
|
|
91
111
|
|
|
92
|
-
# Advance
|
|
93
|
-
#
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
112
|
+
# Advance the page's clock a slice (dommy-js-quickjs's legacy Capybara
|
|
113
|
+
# adapter polls with it).
|
|
114
|
+
def pump = advance_time(PUMP_SLICE_MS)
|
|
115
|
+
|
|
116
|
+
# The current realm's virtual clock, in ms.
|
|
117
|
+
def now_ms = scheduler_of(@current_document.call)&.now_ms
|
|
118
|
+
|
|
119
|
+
# The virtual ms until the page's next timer (setTimeout, setInterval,
|
|
120
|
+
# requestAnimationFrame) in any realm is due — 0 for one due already —
|
|
121
|
+
# or nil when none is scheduled.
|
|
122
|
+
def next_timer_delay
|
|
123
|
+
live_realms.filter_map do |doc, _runtime|
|
|
124
|
+
scheduler = scheduler_of(doc)
|
|
125
|
+
due = scheduler&.next_due_timer_at
|
|
126
|
+
due && [due - scheduler.now_ms, 0].max
|
|
127
|
+
end.min
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Whether a completion another thread handed back (a response, a socket
|
|
131
|
+
# message) waits to be delivered to any realm.
|
|
132
|
+
def external_pending?
|
|
133
|
+
live_realms.any? { |doc, _runtime| scheduler_of(doc)&.external_pending? }
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Whether any realm has handed work to a worker (a fetch on the network
|
|
137
|
+
# executor) that has not come back yet.
|
|
138
|
+
def external_work_in_flight?
|
|
139
|
+
live_realms.any? { |doc, _runtime| scheduler_of(doc)&.external_work_in_flight? }
|
|
100
140
|
end
|
|
101
141
|
|
|
102
142
|
# The realm VM for one document, built lazily and cached by identity so a
|
|
@@ -115,6 +155,15 @@ module Dommy
|
|
|
115
155
|
|
|
116
156
|
private
|
|
117
157
|
|
|
158
|
+
# Whether `scheduler`'s realm has work ready now: a timer already due or
|
|
159
|
+
# a completion handed back.
|
|
160
|
+
def ready_work?(scheduler)
|
|
161
|
+
return false unless scheduler
|
|
162
|
+
|
|
163
|
+
due = scheduler.next_due_timer_at
|
|
164
|
+
scheduler.external_pending? || (!due.nil? && due <= scheduler.now_ms)
|
|
165
|
+
end
|
|
166
|
+
|
|
118
167
|
# The deterministic scheduler driving a document's realm (nil when the
|
|
119
168
|
# document or its window is absent), keeping the `doc -> window ->
|
|
120
169
|
# scheduler` walk in one place.
|
|
@@ -217,6 +266,19 @@ module Dommy
|
|
|
217
266
|
doc.query_selector_all("script[src]").filter_map { |el| el.get_attribute("src") }
|
|
218
267
|
end
|
|
219
268
|
|
|
269
|
+
# The realms whose document is fully active, as [document, runtime]
|
|
270
|
+
# pairs. A frame that navigated away or was removed keeps its realm —
|
|
271
|
+
# the page may hold its objects — but HTML runs no task of a document
|
|
272
|
+
# that is not fully active, so its timers stay where they are, a
|
|
273
|
+
# completion handed back to it is never delivered, and nothing waits
|
|
274
|
+
# on it. A snapshot: a realm that runs may navigate and change the map.
|
|
275
|
+
def live_realms
|
|
276
|
+
@runtimes.to_a.select do |doc, _runtime|
|
|
277
|
+
window = doc&.default_view
|
|
278
|
+
window.nil? || !window.respond_to?(:__internal_fully_active__?) || window.__internal_fully_active__?
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
220
282
|
def dispose_all
|
|
221
283
|
@runtimes.each_value(&:dispose)
|
|
222
284
|
@runtimes = {}.compare_by_identity
|
data/lib/dommy/rack/version.rb
CHANGED
|
@@ -79,12 +79,20 @@ module Dommy
|
|
|
79
79
|
send_close_frame(code == 1005 ? 1000 : code, reason)
|
|
80
80
|
end
|
|
81
81
|
|
|
82
|
+
# Whether the connection is over, so no message can arrive on it: the
|
|
83
|
+
# page closed it, or the reader stopped (the app ended it, the socket
|
|
84
|
+
# dropped, the handshake failed).
|
|
85
|
+
def closed? = @closed || @reader_done
|
|
86
|
+
|
|
82
87
|
# Hard teardown (session dispose): drop the socket; the reader thread
|
|
83
88
|
# exits on EOF. Safe to call repeatedly.
|
|
84
|
-
|
|
89
|
+
# Hard teardown: drop the socket; the reader thread exits on EOF. With
|
|
90
|
+
# `wait` it is also joined (briefly), for a session going away for
|
|
91
|
+
# good; a page navigating away does not wait for it.
|
|
92
|
+
def dispose(wait: true)
|
|
85
93
|
@closed = true
|
|
86
94
|
@client_io&.close unless @client_io&.closed?
|
|
87
|
-
@reader&.join(1)
|
|
95
|
+
@reader&.join(1) if wait
|
|
88
96
|
rescue IOError
|
|
89
97
|
nil
|
|
90
98
|
end
|
|
@@ -141,6 +149,7 @@ module Dommy
|
|
|
141
149
|
post { @ws.__internal_transport_closed__(1006, "", was_clean: false) } unless @closed
|
|
142
150
|
ensure
|
|
143
151
|
@client_io.close rescue nil
|
|
152
|
+
@reader_done = true
|
|
144
153
|
end
|
|
145
154
|
|
|
146
155
|
class HandshakeFailed < StandardError; end
|
data/sig/dommy/rack.rbs
CHANGED
|
@@ -72,7 +72,7 @@ module Dommy
|
|
|
72
72
|
class CookieEntry < Struct[untyped]
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
-
def initialize: () -> void
|
|
75
|
+
def initialize: (?clock: untyped) -> void
|
|
76
76
|
def store_from_header: (String set_cookie_string, String request_url) -> void
|
|
77
77
|
def set!: (untyped name, untyped value, ?domain: String?, ?path: String?, ?expires: untyped, ?secure: bool, ?http_only: bool) -> untyped
|
|
78
78
|
def get: (untyped name) -> String?
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dommy-rack
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.17.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- takahashim
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - "~>"
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 0.
|
|
18
|
+
version: 0.17.0
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - "~>"
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 0.
|
|
25
|
+
version: 0.17.0
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: rack
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|