bblimke / webmock

Library for stubbing and setting expectations on HTTP requests in Ruby.
MIT License
3.94k stars 554 forks source link

Not blocking request from Box ruby client #608

Open om-nishu-trantor opened 8 years ago

om-nishu-trantor commented 8 years ago

I have been using Boxr gem for fetching data from BOX Apis. But somehow Webmock does not seems to be blocking the requests. Here is the file responsible for making web requests in the Boxr gem.

Getting 401(Always will, if hits the real api) want to mock the response and check if responses are handled correctly. Any pointers ?

bblimke commented 8 years ago

@om-nishu-trantor I don't have enough information to help you.

Can you provide a sample code using boxr gem that reproduces the issue?

What version of WebMock?

Is webmock definitely loaded and enabled?

om-nishu-trantor commented 8 years ago

webmock 1.24.5

before(:all) do
    WebMock.disable_net_connect!(:allow_localhost => true)
  end

I am conditionally blocking web requests in this particular spec. Yes it is loaded, because I have 2 specs, 1 for gdrive and another for box. Gdrive spec works fine, but Box hits the real request and says the following :-

Boxr::BoxrError:
       401: Bearer realm="Service", error="invalid_token", error_description="The access token provided is invalid."

Snippets :-

require 'rails_helper'

RSpec.describe MyImportModel, type: :model do

  before(:all) do
    WebMock.disable_net_connect!(:allow_localhost => true)
  end

  after(:all) do
    WebMock.allow_net_connect!
  end
describe '#mymethod' do
  it 'should block check the response manipulation' do
      stub_request(:get, Regexp.new('https://api.box.com/2.0/search')).
        to_return(:status => 200, :body => {}.to_json)
      expect do
        described_class.mymethod
      end.to change {
        MyImportModel.count
      }.by(2)
    end
  end
end
skalb commented 8 years ago

I just had the same issue. It looks like the Boxr::BOX_CLIENT is declared globally before Webmock injects itself: https://github.com/cburnette/boxr/blob/c35eafcd44474e853d0f3cbe108e7e4385afa1d8/lib/boxr.rb#L57

As a quick hack, I just force re-create the client in my spec

before :all do
  Boxr::BOX_CLIENT = HTTPClient.new
end
jprince commented 7 years ago

@skalb this answer just helped me figure out why the VCR gem wasn't creating cassettes for my specs that hit the Box API using Boxr - thanks!

junonya commented 5 years ago

@skalb I was saved. Thanks!