{"record":{"id":"7396e35102399678","repo":"teamcapybara/capybara","slug":"capybara-default-host-should-be-set-to-a-url-http","errorCode":null,"errorMessage":"Capybara.default_host should be set to a url (http://www.example.com). Attempted to set #{url.inspect}.","messagePattern":"Capybara\\.default_host should be set to a url \\(http://www\\.example\\.com\\)\\. Attempted to set #(.+?)\\.","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/capybara/session/config.rb","lineNumber":99,"sourceCode":"\n    remove_method :server_errors=\n    def server_errors=(errors)\n      (@server_errors ||= []).replace(errors.dup)\n    end\n\n    remove_method :app_host=\n    def app_host=(url)\n      unless url.nil? || url.match?(URI_PARSER.make_regexp)\n        raise ArgumentError, \"Capybara.app_host should be set to a url (http://www.example.com). Attempted to set #{url.inspect}.\"\n      end\n\n      @app_host = url\n    end\n\n    remove_method :default_host=\n    def default_host=(url)\n      unless url.nil? || url.match?(URI_PARSER.make_regexp)\n        raise ArgumentError, \"Capybara.default_host should be set to a url (http://www.example.com). Attempted to set #{url.inspect}.\"\n      end\n\n      @default_host = url\n    end\n\n    remove_method :test_id=\n    ##\n    #\n    # Set an attribute to be optionally matched against the locator for builtin selector types.\n    # This attribute will be checked by builtin selector types whenever id would normally be checked.\n    # If `nil` then it will be ignored.\n    #\n    # @param [String, Symbol, nil] id Name of the attribute to use as the test id\n    #\n    def test_id=(id)\n      @test_id = id&.to_sym\n    end\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/session/config.rb#L81-L117","documentation":"Capybara validates the global app_host/default_host configuration setters: any non-nil value must fully match Ruby's URI regular expression, i.e. include a scheme such as http:// or https://. When the value fails that match, the setter raises ArgumentError immediately rather than storing a malformed host that would later produce broken request URLs. nil is explicitly allowed and clears the setting.","triggerScenarios":"Calling Capybara.app_host=, Capybara.default_host= (or session.config.app_host= / session.config.default_host=) with a scheme-less string such as 'www.example.com' or 'localhost:3000'. Typical culprit: Capybara.app_host = ENV['APP_HOST'] where the environment variable holds only a hostname or host:port.","commonSituations":"Reading host settings from environment variables or .env files that omit the scheme (or contain a typo like 'http:/example.com' or a trailing newline); configuring app_host for browser drivers (Selenium) or external test apps; conflating default_host (host used for host-relative URLs under rack_test) with app_host (base URL for visit with absolute paths) during setup.","solutions":["Set a full URL including scheme: Capybara.default_host = 'http://www.example.com' (same form for app_host).","If the value comes from ENV, normalize it first: host = ENV['APP_HOST']; host = \"http://#{host}\" unless host.nil? || host.match?(%r{\\Ahttps?://}).","Pass nil to clear the setting; the setter accepts nil without raising.","Check for whitespace/newlines or malformed schemes in the value before assigning (e.g. value.strip and a URI::DEFAULT_PARSER.make_regexp match)."],"exampleFix":"# before\nCapybara.app_host = ENV['APP_HOST'] # raises ArgumentError if APP_HOST=www.example.com\n\n# after\napp_host = ENV['APP_HOST']\napp_host = \"http://#{app_host}\" unless app_host.nil? || app_host.start_with?('http://', 'https://')\nCapybara.app_host = app_host","handlingStrategy":"validation","validationCode":"require 'uri'\n\ndef safe_host=(value)\n  return if value.nil?\n  raise ArgumentError, \"#{value.inspect} is not a full URL\" unless value.match?(URI::DEFAULT_PARSER.make_regexp)\n  Capybara.app_host = value\nend","typeGuard":"def valid_capybara_host?(value)\n  value.nil? || (value.is_a?(String) && value.match?(URI::DEFAULT_PARSER.make_regexp))\nend","tryCatchPattern":"begin\n  Capybara.default_host = ENV['DEFAULT_HOST']\nrescue ArgumentError => e\n  abort \"Fix DEFAULT_HOST to include http:// or https://: #{e.message}\"\nend","preventionTips":["Store full URLs (scheme + host) in CAPYBARA_APP_HOST / APP_HOST environment variables and document the requirement in the project README.","Normalize scheme-less env values once at spec_helper/rails_helper load time instead of at every assignment.","Add a startup sanity check: URI.parse(Capybara.app_host) in a spec_helper hook so misconfiguration fails fast with a clear message."],"tags":["capybara","configuration","url-validation","argumenterror","ruby"],"backgroundTag":"invalid-url-configuration","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}