Skip to content

Failures shouldn't recommend mocking the "User-Agent" header. #596

Done

Description

@abscondment

Some libraries (e.g. Twilio) include platform and Ruby version in their User-Agent. webmock suggests stubbing requests Twilio requests like this:

You can stub this request with the following snippet:
stub_request(:get, "https://api.twilio.com/2010-04-01/Accounts/id/AvailablePhoneNumbers/US/Local.json").
  with(:headers => {'Accept'=>'application/json', 'Accept-Charset'=>'utf-8', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'twilio-ruby/4.5.0 (ruby/x86_64-darwin15 2.2.4-p230)'}).
  to_return(:status => 200, :body => "", :headers => {})

This is the problematic part, and it's easily missed:

'User-Agent'=>'twilio-ruby/4.5.0 (ruby/x86_64-darwin15 2.2.4-p230)'

If one includes that header in the stub, tests will pass locally. travis et al will fail opaquely because they are (very likely) testing different versions on a different platform.

Instead of blindly pumping out all headers in this helpful error message, I'd like to propose deleting whatever User-Agent is set. I'd be happy to provide a patch if this sounds desirable.

Activity

  1. bblimke commented on Mar 12, 2016

    @bblimke
    Owner

    👍 good idea. Please do submit a pull request.

  2. everaldo commented on Nov 9, 2018

    @everaldo

    @abscondment , did you get any solution for that?

  3. everaldo commented on Nov 9, 2018

    @everaldo
  4. naufil50 commented on Aug 1, 2023

    @naufil50

    I faced similar issue, removed header from stub and it worked fine.

  5. bblimke commented on Nov 23, 2026

    @bblimke
    Owner

    Thank you @abscondment, and @KelseyDH who asked for the same in #857. Since 3.39.0, the stub that WebMock suggests for an unmatched request leaves User-Agent out, so a stub pasted from it keeps matching after the client is upgraded and on other platforms. The message still shows the request with all its headers, and the other headers stay in the suggestion.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions