Module: Turbolinks::Assertions
- Defined in:
- lib/turbolinks/assertions.rb
Constant Summary collapse
- TURBOLINKS_VISIT =
/Turbolinks\.visit\("([^"]+)", {"action":"([^"]+)"}\)/
Instance Method Summary collapse
- #assert_redirected_to(options = {}, message = nil) ⇒ Object
- #assert_turbolinks_visited(options = {}, message = nil) ⇒ Object
-
#turbolinks_request? ⇒ Boolean
Rough heuristic to detect whether this was a Turbolinks request: non-GET request with a text/javascript response.
- #turbolinks_visit_location_and_action ⇒ Object
Instance Method Details
#assert_redirected_to(options = {}, message = nil) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/turbolinks/assertions.rb', line 5 def assert_redirected_to( = {}, = nil) if turbolinks_request? assert_turbolinks_visited(, ) else super end end |
#assert_turbolinks_visited(options = {}, message = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/turbolinks/assertions.rb', line 13 def assert_turbolinks_visited( = {}, = nil) assert_response(:ok, ) assert_equal("text/javascript", response.try(:media_type) || response.content_type) visit_location, _ = turbolinks_visit_location_and_action redirect_is = normalize_argument_to_redirection(visit_location) redirect_expected = normalize_argument_to_redirection() ||= "Expected response to be a Turbolinks visit to <#{redirect_expected}> but was a visit to <#{redirect_is}>" assert_operator redirect_expected, :===, redirect_is, end |
#turbolinks_request? ⇒ Boolean
Rough heuristic to detect whether this was a Turbolinks request: non-GET request with a text/javascript response.
Technically we’d check that Turbolinks-Referrer request header is also set, but that’d require us to pass the header from post/patch/etc test methods by overriding them to provide a ‘turbolinks:` option.
We can’t check ‘request.xhr?` here, either, since the X-Requested-With header is cleared after controller action processing to prevent it from leaking into subsequent requests.
36 37 38 |
# File 'lib/turbolinks/assertions.rb', line 36 def turbolinks_request? !request.get? && (response.try(:media_type) || response.content_type) == "text/javascript" end |
#turbolinks_visit_location_and_action ⇒ Object
40 41 42 43 44 |
# File 'lib/turbolinks/assertions.rb', line 40 def turbolinks_visit_location_and_action if response.body =~ TURBOLINKS_VISIT [ $1, $2 ] end end |