Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
Remove class method mock (Net::HTTP.any_instance), affects other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmanzella committed Dec 6, 2022
1 parent 63f5363 commit 249a66b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
20 changes: 13 additions & 7 deletions lib/theme_check/remote_asset_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ def content
return if @uri.nil?
return @content unless @content.nil?

res = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: @uri.scheme == 'https') do |http|
req = Net::HTTP::Get.new(@uri)
req['Accept-Encoding'] = 'gzip, deflate, br'
http.request(req)
end

@content = res.body
@content = request(@uri)

rescue OpenSSL::SSL::SSLError, Zlib::StreamError, *NET_HTTP_EXCEPTIONS
@contents = ''
Expand All @@ -47,5 +41,17 @@ def gzipped_size
return if @uri.nil?
@gzipped_size ||= content.bytesize
end

private

def request(uri)
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
req = Net::HTTP::Get.new(uri)
req['Accept-Encoding'] = 'gzip, deflate, br'
http.request(req)
end

res.body
end
end
end
14 changes: 8 additions & 6 deletions test/remote_asset_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

module ThemeCheck
class RemoteAssetFileTest < Minitest::Test
FakeResponse = Struct.new(:body)

def setup
@src = 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js'
@asset = RemoteAssetFile.from_src(@src)
Expand All @@ -16,10 +14,8 @@ def test_instance_caching
end

def test_network_request
Net::HTTP.any_instance
.expects(:request)
.with { |req| req['Accept-Encoding'] == 'gzip, deflate, br' }
.returns(FakeResponse.new("..."))
@asset.expects(:request).with(uri).returns("...")

assert_equal("...", @asset.content)
assert_equal(3, @asset.gzipped_size)
end
Expand All @@ -35,5 +31,11 @@ def test_handles_eaddr_not_avail_errors
assert(asset.gzipped_size == 0)
assert(asset.content.empty?)
end

private

def uri
RemoteAssetFile.uri(@src)
end
end
end

0 comments on commit 249a66b

Please sign in to comment.