1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

raise error if app_host/default_host are incorrectly set

This commit is contained in:
Thomas Walpole 2016-05-06 10:15:59 -07:00
parent 55c2ec3542
commit 84d854244a
2 changed files with 47 additions and 2 deletions

View file

@ -20,7 +20,8 @@ module Capybara
class ReadOnlyElementError < CapybaraError; end
class << self
attr_accessor :asset_host, :app_host, :run_server, :default_host, :always_include_port
attr_reader :app_host, :default_host
attr_accessor :asset_host, :run_server, :always_include_port
attr_accessor :server_port, :exact, :match, :exact_options, :visible_text_only
attr_accessor :default_selector, :default_max_wait_time, :ignore_hidden_elements
attr_accessor :save_path, :wait_on_first_by_default, :automatic_reload
@ -40,7 +41,7 @@ module Capybara
#
# === Configurable options
#
# [app_host = String] The default host to use when giving a relative URL to visit
# [app_host = String/nil] The default host to use when giving a relative URL to visit, must be a valid URL e.g. http://www.example.com
# [always_include_port = Boolean] Whether the Rack server's port should automatically be inserted into every visited URL (Default: false)
# [asset_host = String] Where dynamic assets are hosted - will be prepended to relative asset locations if present (Default: nil)
# [run_server = Boolean] Whether to start a Rack server for the given Rack app (Default: true)
@ -387,6 +388,16 @@ module Capybara
@save_and_open_page_path = path
end
def app_host=(url)
raise ArgumentError.new("Capybara.app_host should be set to a url (http://www.example.com)") unless url.nil? || (url =~ URI::regexp)
@app_host = url
end
def default_host=(url)
raise ArgumentError.new("Capybara.default_host should be set to a url (http://www.example.com)") unless url.nil? || (url =~ URI::regexp)
@default_host = url
end
def included(base)
base.send(:include, Capybara::DSL)
warn "`include Capybara` is deprecated. Please use `include Capybara::DSL` instead."

View file

@ -77,6 +77,40 @@ RSpec.describe Capybara do
expect(Capybara.server).to eq(server)
end
end
describe 'app_host' do
after do
Capybara.app_host = nil
end
it "should warn if not a valid URL" do
expect { Capybara.app_host = "www.example.com" }.to raise_error(ArgumentError, /Capybara\.app_host should be set to a url/)
end
it "should not warn if a valid URL" do
expect { Capybara.app_host = "http://www.example.com" }.not_to raise_error
end
it "should not warn if nil" do
expect { Capybara.app_host = nil }.not_to raise_error
end
end
describe 'default_host' do
around do |test|
old_default = Capybara.default_host
test.run
Capybara.default_host = old_default
end
it "should warn if not a valid URL" do
expect { Capybara.default_host = "www.example.com" }.to raise_error(ArgumentError, /Capybara\.default_host should be set to a url/)
end
it "should not warn if a valid URL" do
expect { Capybara.default_host = "http://www.example.com" }.not_to raise_error
end
end
end
RSpec.describe Capybara::Session do