Webmock: Issue with ruby 2.4.0

Created on 8 Feb 2017  路  6Comments  路  Source: bblimke/webmock

I am facing following error with ruby 2.4.0, It works fine with 2.3.3

NoMethodError: undefined method `close' for #<StubSocket:0x007f871cf99070>
Did you mean?  closed?
               clone

The line causing is simply a Net::HTTP.post_form

Most helpful comment

@jiggneshhgohel I can see you are using WebMock 2.1.0

Support for ruby 2.4.0 has been added in WebMock 2.3.1

@lokenmakwana what version of webmock do you use?

All 6 comments

Interesting. StubSocket does have close method defined.

Would you mind providing a sample code to reproduce the issue?

I am also facing the same error after upgrading Ruby from version 2.3.2 to 2.4.0 in my Rails 5.0.0.1 application. My spec examples fails with the same error:

Failure/Error: response = http_client.post(json_payload) # json_payload because request's Content-Type is application/json.

      NoMethodError:
        undefined method `close' for #<StubSocket:0x0000000c8c3488>
        Did you mean?  closed?
                       clone

      # /home/jignesh/.rvm/gems/ruby-2.4.0@my_app-rails-5/gems/webmock-2.1.0/lib/webmock/http_lib_adapters/net_http.rb:125:in `start_without_connect'
      # /home/jignesh/.rvm/gems/ruby-2.4.0@my_app-rails-5/gems/webmock-2.1.0/lib/webmock/http_lib_adapters/net_http.rb:150:in `start'
      # /home/jignesh/.rvm/gems/ruby-2.4.0@my_app-rails-5/gems/rest-client-2.0.0/lib/restclient/request.rb:766:in `transmit'
      # /home/jignesh/.rvm/gems/ruby-2.4.0@my_app-rails-5/gems/rest-client-2.0.0/lib/restclient/request.rb:215:in `execute'
      # /home/jignesh/.rvm/gems/ruby-2.4.0@my_app-rails-5/gems/rest-client-2.0.0/lib/restclient/request.rb:52:in `execute'
      # /home/jignesh/.rvm/gems/ruby-2.4.0@my_app-rails-5/gems/rest-client-2.0.0/lib/restclient/resource.rb:67:in `post'
      # ./app/utils/notifications/notification_mode/push_notification.rb:285:in `send_push_notification_request'
...
....
...

Below is the relevant code-snippet

      def send_push_notification_request(payload)
        begin
          json_payload = payload.to_json
          response = http_client.post(json_payload) # json_payload because request's Content-Type is application/json.
        rescue RestClient::Exception => rce
          response = rce.response
        end

        response_status = response.code
        Rails.logger.info "Push Notification Response Status: #{response_status}"

        is_push_delivered = (APPERY_PUSH_NOTIFICATION_SEND_API_SUCCESS_STATUS == response_status)

        unless is_push_delivered
          response_body = response.body.empty? ? "{}" : response.body
          json_hash = JSON.parse(response_body)
          error_message = json_hash["description"]
          error_code = json_hash["code"]
          Rails.logger.warn(">>>>>>>> Could not deliver push notification to device(uid: #{payload[:filter][:deviceID]}). Appery Push Notification API responded with error: '#{error_message}' and error code: '#{error_code}'")
        end

        is_push_delivered
      end

      def http_client
        @http_client ||= RestClient::Resource.new(request_url, headers: request_headers)
      end

Hope that helps in reproducing the error and fixing this issue.

@jiggneshhgohel I can see you are using WebMock 2.1.0

Support for ruby 2.4.0 has been added in WebMock 2.3.1

@lokenmakwana what version of webmock do you use?

@bblimke Thanks for your prompt response. I am updating my gem and will share the spec run results here.

@bblimke Updating webmock gem from 2.1.0 to version 2.3.2 fixed the mentioned error in comment https://github.com/bblimke/webmock/issues/683#issuecomment-279963561.

Thanks a lot!

I was also using WebMock 2.1.0 with ruby 2.4

Just upgraded to WebMock 2.3.2 and everything is fine.

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RKushnir picture RKushnir  路  14Comments

wgordon17 picture wgordon17  路  5Comments

emarthinsen picture emarthinsen  路  8Comments

Integralist picture Integralist  路  5Comments

Watson1978 picture Watson1978  路  4Comments