rubyregexrspecstubwebmock

Ruby - Webmock: Match URI using regular expression


I'm working with rspec and webmock and I'm looking into stubbing request. I do have a problem when I try to use regex to match the URI.

Everything was working fine when I used the stub below, without matching a specific URI (/.*/)

it "returns nil and stores an error when the response code is not OK" do
      stub_request(:get, /.*/).
        with(
        :headers => insertion_api.send(:default_headers,  false).merge('User-Agent'=>'Ruby'),
        :body => {}
      ).
       to_return(
        :status => Insertion.internal_server_error.to_i,
        :body => "{\"message\": \"failure\"}",
        :headers => { 'Cookie' => [session_token] }
      )

      expect(insertion_api.get_iou(uid)).to be_nil
      expect(insertion_api.error).to eq("An internal server error occurred")
     end

Since I want to be more specific in my test to improve readability, if I try to match a this specific URI: /insertion_order/012awQQd?fields=name,type&depth=4 using the stub below:

it "returns nil and stores an error when the response code is not OK" do
          stub_request(:get, %r{insertion_order/\w+\?fields\=[\w,]+\&depth\=[0-9]}).
            with(
            :headers => insertion_api.send(:default_headers,  false).merge('User-Agent'=>'Ruby'),
            :body => {}
          ).
           to_return(
            :status => Insertion.internal_server_error.to_i,
            :body => "{\"message\": \"failure\"}",
            :headers => { 'Cookie' => [session_token] }
          )

          expect(insertion_api.get_iou(uid)).to be_nil
          expect(insertion_api.error).to eq("An internal server error occurred")
         end

running the test I've got:

WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET https://mocktocapture.com/mgmt/insertion_order/0C12345678 with body '{}' with headers {'Accept'=>'application/vnd.xxx.mgmt+json; version=2.0', 'Cookie'=>'y0Urv3ryLon6s3cur1tYT0k3ng0zeh3r3', 'User-Agent'=>'Ruby'}

       You can stub this request with the following snippet:

       stub_request(:get, "https://mocktocapture.com/mgmt/insertion_order_units/0C12345678").
         with(:body => "{}",
              :headers => {'Accept'=>'application/vnd.dataxu.mgmt+json; version=2.0', 'Cookie'=>'y0Urv3ryLon6s3cur1tYT0k3ng0zeh3r3', 'User-Agent'=>'Ruby'}).
         to_return(:status => 200, :body => "", :headers => {})

       registered request stubs:

       stub_request(:get, "/insertion_order\/\w+\?fields\=[\w,]+\&depth\=[0-9]/").
         with(:body => {},
              :headers => {'Accept'=>'application/vnd.xxx.mgmt+json; version=2.0', 'Cookie'=>'y0Urv3ryLon6s3cur1tYT0k3ng0zeh3r3', 'User-Agent'=>'Ruby'})

The regex I've used is correct, but I don't understand why I've got this error message.


Solution

  • The request you got is :

    https://mocktocapture.com/mgmt/insertion_order/0C12345678
    

    You have given the regexp :

    %r{insertion_order/\w+\?fields\=[\w,]+\&depth\=[0-9]}
    

    In the regexp you have specified with the "\?" that it is mandatory that the request should contain "?" (or a query) after "insertion_order/\w+". In the request you got there aren't any query parameters. That's why it isn't matching the request.

    One way you can fix that is to make the part that comes after "insertion_order/\w+" in the regexp optional. I would do it like this :

    %r{insertion_order/\w+(\?fields\=[\w,]+\&depth\=[0-9])?}