{"record":{"id":"37fb29d3d59782cf","repo":"bblimke/webmock","slug":"uri-should-be-a-string-regexp-addressable-templ","errorCode":null,"errorMessage":"URI should be a String, Regexp, Addressable::Template, a callable object, or respond to #to_str. Got: #{uri.class}","messagePattern":"URI should be a String, Regexp, Addressable::Template, a callable object, or respond to #to_str\\. Got: #(.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/webmock/request_pattern.rb","lineNumber":92,"sourceCode":"    def validate_basic_auth!(basic_auth)\n      if !basic_auth.is_a?(Array) || basic_auth.map{|e| e.is_a?(String)}.uniq != [true]\n        raise \"The basic_auth option value should be an array which contains 2 strings: username and password\"\n      end\n    end\n\n    def create_uri_pattern(uri)\n      if uri.is_a?(Regexp)\n        URIRegexpPattern.new(uri)\n      elsif uri.is_a?(Addressable::Template)\n        URIAddressablePattern.new(uri)\n      elsif uri.respond_to?(:call)\n        URICallablePattern.new(uri)\n      elsif uri.is_a?(::URI::Generic)\n        URIStringPattern.new(uri.to_s)\n      elsif uri.respond_to?(:to_str)\n        URIStringPattern.new(uri.to_str)\n      else\n        raise ArgumentError.new(\"URI should be a String, Regexp, Addressable::Template, a callable object, or respond to #to_str. Got: #{uri.class}\")\n      end\n    end\n  end\n\n\n  class MethodPattern\n    def initialize(pattern)\n      @pattern = pattern\n    end\n\n    def matches?(method)\n      @pattern == method || @pattern == :any\n    end\n\n    def to_s\n      @pattern.to_s\n    end\n  end","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/bblimke/webmock/blob/b187df8827e1f08a1684a8ddc0a5050dbd449c16/lib/webmock/request_pattern.rb#L74-L110","documentation":"The URI argument to stub_request/a_request must be a String, Regexp, Addressable::Template, a callable object, a stdlib URI::Generic (URI::HTTP and URI::HTTPS inherit from it), or anything responding to #to_str. create_uri_pattern (lib/webmock/request_pattern.rb:80) dispatches on exactly those types and raises ArgumentError naming the class it actually got otherwise. The classic offender is Addressable::URI: it is a webmock dependency and apps parse URLs with it, but it responds to #to_s rather than #to_str, so stub_request(:get, Addressable::URI.parse(url)) raises.","triggerScenarios":"Passing an Addressable::URI instance. Passing nil (an ENV/config lookup that returned nothing - the error says Got: NilClass). Passing a Hash or Symbol. Passing a custom URL wrapper object that only implements to_s.","commonSituations":"Apps that normalize or build URLs through Addressable before stubbing; URL helpers that return a String in one branch and a URI object in another; optional endpoints configured via ENV that are unset in CI.","solutions":["Convert before stubbing: stub_request(:get, uri.to_s) - works for Addressable::URI and URI::HTTP alike","For pattern matching use Addressable::Template.new('https://api.example.com/v1/{id}') which is supported natively, or a Regexp like %r{api\\.example\\.com/v1}","If the value is nil, fix the source: check the ENV or config read that produced it - the error names the class you passed","For dynamic matching pass a lambda: stub_request(:get, ->(uri) { uri.host == 'api.example.com' })"],"exampleFix":"# before\nstub_request(:get, Addressable::URI.parse('https://api.example.com/v1/users'))  # ArgumentError: Got: Addressable::URI\n\n# after\nstub_request(:get, 'https://api.example.com/v1/users')","handlingStrategy":"type-guard","validationCode":"parsed = begin\n  Addressable::URI.parse(raw)\nrescue Addressable::URI::InvalidURIError\n  raw\nend\nstub_request(:get, parsed.is_a?(Addressable::URI) ? parsed.to_s : parsed)","typeGuard":"def stubbable_uri?(u)\n  u.is_a?(String) || u.is_a?(Regexp) || u.is_a?(Addressable::Template) ||\n    u.respond_to?(:call) || u.is_a?(::URI::Generic) || u.respond_to?(:to_str)\nend","tryCatchPattern":null,"preventionTips":["Call .to_s on parsed URI objects at the boundary before stubbing","Keep spec URL constants as plain strings","Fail fast on nil endpoints in spec_helper instead of passing nil through"],"tags":["webmock","uri","addressable","type-validation","ruby"],"backgroundTag":"invalid-argument-type","analyzedSha":"b187df8827e1f08a1684a8ddc0a5050dbd449c16","analyzedAt":"2026-08-23T02:55:22.028Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}