2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2016-01-27 17:20:47 -05:00
|
|
|
require 'addressable/uri'
|
2016-01-27 13:53:59 -05:00
|
|
|
|
2015-08-24 01:14:48 -04:00
|
|
|
module Capybara
|
|
|
|
# @api private
|
|
|
|
module Queries
|
|
|
|
class CurrentPathQuery < BaseQuery
|
|
|
|
def initialize(expected_path, options = {})
|
2017-05-28 11:54:55 -04:00
|
|
|
super(options)
|
2015-08-24 01:14:48 -04:00
|
|
|
@expected_path = expected_path
|
2017-06-20 18:14:32 -04:00
|
|
|
warn "DEPRECATED: The :only_path option is deprecated in favor of the :ignore_query option" if options.has_key?(:only_path)
|
|
|
|
|
2015-08-24 01:14:48 -04:00
|
|
|
@options = {
|
2017-06-20 18:14:32 -04:00
|
|
|
url: !@expected_path.is_a?(Regexp) && !::Addressable::URI.parse(@expected_path || "").hostname.nil?,
|
|
|
|
only_path: false,
|
|
|
|
ignore_query: false }.merge(options)
|
2015-08-24 01:14:48 -04:00
|
|
|
assert_valid_keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolves_for?(session)
|
2017-06-20 18:14:32 -04:00
|
|
|
uri = ::Addressable::URI.parse(session.current_url)
|
|
|
|
uri.query = nil if uri && options[:ignore_query]
|
2015-08-24 01:14:48 -04:00
|
|
|
@actual_path = if options[:url]
|
2017-06-20 18:14:32 -04:00
|
|
|
uri.to_s
|
2015-08-24 01:14:48 -04:00
|
|
|
else
|
|
|
|
if options[:only_path]
|
2017-06-20 18:14:32 -04:00
|
|
|
uri && uri.path
|
2015-08-24 01:14:48 -04:00
|
|
|
else
|
2017-06-20 18:14:32 -04:00
|
|
|
uri && uri.request_uri
|
2015-08-24 01:14:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @expected_path.is_a? Regexp
|
2017-11-28 01:35:04 -05:00
|
|
|
@actual_path.to_s.match(@expected_path)
|
2015-08-24 01:14:48 -04:00
|
|
|
else
|
2017-01-26 19:10:51 -05:00
|
|
|
::Addressable::URI.parse(@expected_path) == ::Addressable::URI.parse(@actual_path)
|
2015-08-24 01:14:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message
|
|
|
|
failure_message_helper
|
|
|
|
end
|
|
|
|
|
|
|
|
def negative_failure_message
|
|
|
|
failure_message_helper(' not')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def failure_message_helper(negated = '')
|
|
|
|
verb = (@expected_path.is_a?(Regexp))? 'match' : 'equal'
|
|
|
|
"expected #{@actual_path.inspect}#{negated} to #{verb} #{@expected_path.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_keys
|
2017-06-20 18:14:32 -04:00
|
|
|
[:wait, :url, :only_path, :ignore_query]
|
2015-08-24 01:14:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_valid_keys
|
|
|
|
super
|
|
|
|
if options[:url] && options[:only_path]
|
|
|
|
raise ArgumentError, "the :url and :only_path options cannot both be true"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|