webmock icon indicating copy to clipboard operation
webmock copied to clipboard

Not blocking request from Box ruby client

Open om-nishu-trantor opened this issue 9 years ago • 5 comments

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 ?

om-nishu-trantor avatar Apr 22 '16 10:04 om-nishu-trantor

@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?

bblimke avatar Apr 22 '16 10:04 bblimke

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

om-nishu-trantor avatar Apr 22 '16 10:04 om-nishu-trantor

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

skalb avatar Apr 29 '16 00:04 skalb

@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!

jprince avatar Nov 01 '16 19:11 jprince

@skalb I was saved. Thanks!

junonya avatar Jul 02 '19 07:07 junonya