Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Httparty 0.15.0 not working properly with WebMock? #547

Closed
ehannes opened this issue Jul 12, 2017 · 5 comments
Closed

Httparty 0.15.0 not working properly with WebMock? #547

ehannes opened this issue Jul 12, 2017 · 5 comments

Comments

@ehannes
Copy link

ehannes commented Jul 12, 2017

I am developing a gem that uses Httparty. I have stubbed a request in one of my tests with WebMock. The test passes with httparty version 0.14.0 and breaks in 0.15.0. Removing the request stubbing reveals the request. Have a look at the examples below.

With httparty 0.14.0:

WebMock::NetConnectNotAllowedError:
  Real HTTP connections are disabled.  Unregistered request:
  GET http://myapi.com/ with headers {
    'Accept'=>'application/json',
    'Access-Token'=>'xxxxxxxx-zzzz-yyyy-aaaa-bbbbbbbbbbbb',
    'Authorization-Code'=>'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
    'Client-Secret'=>'1234567890', 'Content-Type'=>'application/json'
  }

With httparty 0.15.0:

WebMock::NetConnectNotAllowedError:
  Real HTTP connections are disabled. Unregistered request:
  GET http://myapi.com/ with headers {
    'Accept'=>'*/*',
    'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
    'User-Agent'=>'Ruby'
  }

As you can see the headers are totally different (and totally wrong) with 0.15.0. Is this a breaking change in 0.15.0. What can I do to fix it?

@jnunemaker
Copy link
Owner

Hmmm. Nothing I can think of immediately. Any chance you could provide a small script or failing test PR to demonstrate the failure that was reproducible? That would really help for fixing it.

@ehannes
Copy link
Author

ehannes commented Aug 4, 2017

@jnunemaker Luckily, the project I am working with is Open Source! I have a branch where my specs are failing due to above issue. Take a look at https://github.com/ehannes/fortnox-api/tree/httparty-issue. If that is too much to ask for, or if you have any questions, please contact me :)

@ehannes
Copy link
Author

ehannes commented Oct 11, 2017

Any chance someone with knowledge of httparty can have a look at this? :)

@jnunemaker
Copy link
Owner

I think #518 + how you are setting headers in fortnox is what affected you. If you add headers DEFAULT_HEADERS.dup after the assignment of DEFAULT_HEADERS in base.rb the spec passes on 0.15.6 (I tested locally).

I usually recommend not using the class methods from an instance (ala self.class.blah). Instead, I store things unique to the instance or each request in instance variables and pass them into the class request methods (get, post, etc.) from the instance. For what you are doing, I might do something like:

module Fortnox
  module API
    class Base
      include HTTParty

      headers({
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
      })

      def initialize(headers = {})
        @base_uri = get_base_url
        @headers = headers.merge({
          'Client-Secret' => get_client_secret,
        })

        check_access_tokens!
      end

      def get(path, options = {}, &block)
        provided_headers = options[:headers] || {}
        options[:headers] = @headers.merge(provided_headers)
        options[:base_uri] ||= @base_uri
        self.class.get(path, options, &block)
      end

      # def post, etc
    end
  end
end

Using the class methods from the instance makes things not thread safe and pollutes the class.

@jnunemaker
Copy link
Owner

Oh, and let me know if you have any more questions about it. Here is the spec passing on that branch with 0.15.6:

$ bundle exec rake
/Users/jnunemaker/.rbenv/versions/2.3.3/bin/ruby -I/Users/jnunemaker/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib:/Users/jnunemaker/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-support-3.6.0/lib /Users/jnunemaker/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
Run options: include {:focus=>true}

Randomized with seed 41511
{:before=>{"Content-Type"=>"application/json", "Accept"=>"application/json"}}
{:after=>{"Content-Type"=>"application/json", "Accept"=>"application/json", "Authorization-Code"=>"ea3862b0-189c-464b-8e23-1b9702365ea1", "Client-Secret"=>"P5K5vE3Kun", "Access-Token"=>"3f08d038-f380-4893-94a0-a08f6e60e67a"}}
.

Finished in 0.01033 seconds (files took 0.9959 seconds to load)
1 example, 0 failures

Randomized with seed 41511

$ bundle show httparty
/Users/jnunemaker/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/httparty-0.15.6
$ bundle -v
ruBundler version 1.14.6
$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants