webmock icon indicating copy to clipboard operation
webmock copied to clipboard

Cannot stub `OPTIONS *` requests

Open postmodern opened this issue 3 years ago • 2 comments

It does not appear to be possible to stub an OPTIONS request where the path is *.

Example

require 'spec_helper'
require 'webmock/rspec'

describe Test do
  it "must stub a OPTIONS request with path '*'" do
    stub_request(:options,"example.com*")

    http = Net::HTTP.new('example.com',80)
    req = Net::HTTP::Options.new('*')
    http.request(req)
  end
end

Result

Test
  must stub a OPTIONS request with path '*' (FAILED - 1)

Failures:

  1) Test must stub a OPTIONS request with path '*'
     Failure/Error: http.request(req)
     
     Addressable::URI::InvalidURIError:
       Invalid port number: "80*"
     # ./vendor/bundle/ruby/3.1.0/gems/addressable-2.8.0/lib/addressable/uri.rb:1430:in `port='
     # ./vendor/bundle/ruby/3.1.0/gems/addressable-2.8.0/lib/addressable/uri.rb:848:in `block in initialize'
     # ./vendor/bundle/ruby/3.1.0/gems/addressable-2.8.0/lib/addressable/uri.rb:2419:in `defer_validation'
     # ./vendor/bundle/ruby/3.1.0/gems/addressable-2.8.0/lib/addressable/uri.rb:840:in `initialize'
     # ./vendor/bundle/ruby/3.1.0/gems/addressable-2.8.0/lib/addressable/uri.rb:147:in `new'
     # ./vendor/bundle/ruby/3.1.0/gems/addressable-2.8.0/lib/addressable/uri.rb:147:in `parse'
     # ./vendor/bundle/ruby/3.1.0/gems/addressable-2.8.0/lib/addressable/uri.rb:225:in `heuristic_parse'
     # ./vendor/bundle/ruby/3.1.0/gems/webmock-3.14.0/lib/webmock/util/uri.rb:11:in `block in <class:URI>'
     # ./vendor/bundle/ruby/3.1.0/gems/webmock-3.14.0/lib/webmock/util/uri.rb:27:in `heuristic_parse'
     # ./vendor/bundle/ruby/3.1.0/gems/webmock-3.14.0/lib/webmock/util/uri.rb:15:in `block in <class:URI>'
     # ./vendor/bundle/ruby/3.1.0/gems/webmock-3.14.0/lib/webmock/util/uri.rb:33:in `normalize_uri'
     # ./vendor/bundle/ruby/3.1.0/gems/webmock-3.14.0/lib/webmock/request_signature.rb:10:in `initialize'
     # ./vendor/bundle/ruby/3.1.0/gems/webmock-3.14.0/lib/webmock/http_lib_adapters/net_http.rb:341:in `new'
     # ./vendor/bundle/ruby/3.1.0/gems/webmock-3.14.0/lib/webmock/http_lib_adapters/net_http.rb:341:in `request_signature_from_request'
     # ./vendor/bundle/ruby/3.1.0/gems/webmock-3.14.0/lib/webmock/http_lib_adapters/net_http.rb:75:in `request'
     # ./spec/test_spec.rb:10:in `block (2 levels) in <top (required)>'
     # ./vendor/bundle/ruby/3.1.0/gems/webmock-3.14.0/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'

postmodern avatar Jul 16 '22 01:07 postmodern

Thank you for raising. Indeed, it expects a valid path path string and does not accept *. Do you know if *, single character, is the only exception?

bblimke avatar Aug 02 '22 13:08 bblimke

AFAIK, only OPTIONS * and CONNECT host:port are the only examples of when HTTP methods do not accept an absolute path. Normally, OPTIONS accepts an absolute path, but * indicates requesting the allowed options for the entire server.

postmodern avatar Aug 03 '22 20:08 postmodern