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

WebMock.reset! is not effective between examples #1085

Open
tomekc opened this issue Nov 29, 2024 · 1 comment
Open

WebMock.reset! is not effective between examples #1085

tomekc opened this issue Nov 29, 2024 · 1 comment

Comments

@tomekc
Copy link

tomekc commented Nov 29, 2024

Hello. I am using WebMock for the first time, because I need to stub requests made by RestClient used under the hood.

My test "suite" consists of two or three examples, and I noticed a strange gap reported in coverage. It turned out that examples, when run in isolation, work fine, but when run as a whole context, only the first example works as expected, and the second one silently passes.

I went to extreme and noticed that despite WebMock.reset! the previously set stubs are still effective, and that could be reason for the second example not actually testing anything. It is also prominent in time to run example: singular milliseconds.

Here is current code sample, I was a bit desperate hence the redundant calls:

Note: The download.mycompany.com hostname has to be pass-through, i.e. allow to execute normally.

    before(:each) do
      puts "=== WEBMOCK ENABLE ==="
      WebMock.enable!
      WebMock.allow_net_connect!(net_http_connect_on_start: ["download.mycompany.com"]) 
    end

    after(:each) do
      puts "=== WEBMOCK RESET ==="
      WebMock.reset!
      WebMock.disable_net_connect!
      WebMock.allow_net_connect!
      WebMock.disable!
    end

    it 'should handle multiple redirects with one without host name and absolute path' do
      WebMock.reset!
      WebMock::API.stub_request(:get, "http://www.example.com/foo").to_return(status: 302, headers: { 'Location' => 'http://www.othersite.com/picture' })
      WebMock::API.stub_request(:get, "http://www.othersite.com/picture").to_return(status: 302, headers: { 'Location' => '/image.jpg' })
      WebMock::API.stub_request(:get, "http://www.othersite.com/image.jpg").to_return(status: 200, body: File.new('spec/mountain.jpg')) # Expected resolution of redirects
      WebMock::API.stub_request(:get, "http://www.example.com/image.jpg").to_return(status: 404) # Improperly resolved redirect

      url = 'http://www.example.com/foo'
      show(url, type: :fetch)
      byebug
      puts "=== HEADERS 1 === #{response.headers}"
      expect(response.status).to eq(200)
    end

    it 'should handle multiple redirects with one without host and relative path' do
      WebMock.reset!
      # WebMock::API.stub_request(:get, "http://www.example.com/foo").to_return(status: 302, headers: { 'Location' => 'http://www.othersite.com/folder/picture' })
      # WebMock::API.stub_request(:get, "http://www.othersite.com/folder/picture").to_return(status: 302, headers: { 'Location' => 'image.jpg' })
      # WebMock::API.stub_request(:get, "http://www.othersite.com/folder/image.jpg").to_return(status: 200, body: File.new('spec/mountain.jpg')) # Expected resolution of redirects
      # WebMock::API.stub_request(:get, "http://www.example.com/image.jpg").to_return(status: 404) # Improperly resolved redirect

      url = 'http://www.example.com/foo'
      show(url, type: :fetch)
      byebug
      puts "=== HEADERS 2 === #{response.headers}"
      expect(response.status).to eq(200)
    end

Note that despite commenting out the stubbing in second example, the test still passes. I am printing the headers in both cases, and they are exactly the same (with difference on Last-Modified):

=== HEADERS 1 === {"Date"=>"Fri, 29 Nov 2024 13:02:27 GMT", "Content-Type"=>"image/jpeg", "Content-Length"=>"71545", "Connection"=>"keep-alive", "Timing-Allow-Origin"=>"*", "Surrogate-Reporting"=>"width=495,height=360,bytes=71545,format=\"jpg\",o=1,ef=(17,30)", "Edge-Cache-Tag"=>"520829016844370983840956685117111516177,3ea99c12212b7e63564d61e84c23455a", "Access-Control-Allow-Origin"=>"*", "Etag"=>"\"7b89f89d9ae4a38b25c7e5695cb1cfaf\"", "Cache-Tag"=>"520829016844370983840956685117111516177,3ea99c12212b7e63564d61e84c23455a", "Last-Modified"=>"Fri, 29 Nov 2024 13:02:28 GMT", "Surrogate-Key"=>"520829016844370983840956685117111516177 3ea99c12212b7e63564d61e84c23455a", "Cache-Control"=>"public, max-age=604800", "Access-Control-Allow-Headers"=>"X-Requested-With", "X-Request-Id"=>"91984c28130cfb0d14547cc11d79dbc0"}



=== HEADERS 2 === {"Date"=>"Fri, 29 Nov 2024 13:02:39 GMT", "Content-Type"=>"image/jpeg", "Content-Length"=>"71545", "Connection"=>"keep-alive", "Surrogate-Key"=>"520829016844370983840956685117111516177 3ea99c12212b7e63564d61e84c23455a", "Surrogate-Reporting"=>"width=495,height=360,bytes=71545,format=\"jpg\",o=1,ef=(17,30)", "X-Orig-Request-Id"=>"91984c28130cfb0d14547cc11d79dbc0", "Edge-Cache-Tag"=>"520829016844370983840956685117111516177,3ea99c12212b7e63564d61e84c23455a", "Cache-Tag"=>"520829016844370983840956685117111516177,3ea99c12212b7e63564d61e84c23455a", "Accept-Ranges"=>"bytes", "Cache-Control"=>"public, max-age=604800", "Etag"=>"\"7b89f89d9ae4a38b25c7e5695cb1cfaf\"", "Last-Modified"=>"Fri, 29 Nov 2024 13:02:27 GMT", "Access-Control-Allow-Headers"=>"X-Requested-With", "Access-Control-Allow-Origin"=>"*", "Timing-Allow-Origin"=>"*"}
@tomekc
Copy link
Author

tomekc commented Nov 30, 2024

Just realized that there might be some caching in action. Therefore using different URLs for each test example seems to solve the problem.

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

No branches or pull requests

1 participant