Make VCR disallow unexpected HTTP requests.

This makes it so that tests fail when they make HTTP requests they
aren't expected to. Update these tests so that they use VCR like they
should.
This commit is contained in:
evazion
2014-09-27 00:28:53 -05:00
parent ec0f226f46
commit 268f79c3d9
3 changed files with 30 additions and 15 deletions

View File

@@ -100,5 +100,5 @@ MEMCACHE = MockMemcache.new
VCR.configure do |c|
c.cassette_library_dir = "test/fixtures/vcr_cassettes"
c.hook_into :webmock
c.allow_http_connections_when_no_cassette = true
# c.allow_http_connections_when_no_cassette = true
end

View File

@@ -17,7 +17,7 @@ module Downloads
setup do
Net::HTTP.stubs(:start).raises(Errno::ETIMEDOUT)
end
should "retry three times" do
assert_raises(Errno::ETIMEDOUT) do
@download.http_get_streaming {}
@@ -27,28 +27,38 @@ module Downloads
end
should "stream a file from an HTTP source" do
@download.http_get_streaming do |resp|
assert_equal("200", resp.code)
assert(resp["Content-Length"].to_i > 0, "File should be larger than 0 bytes")
VCR.use_cassette("download-file-http", :record => :once) do
@download.http_get_streaming do |resp|
assert_equal("200", resp.code)
assert(resp["Content-Length"].to_i > 0, "File should be larger than 0 bytes")
end
end
end
should "throw an exception when the file is larger than the maximum" do
assert_raise(Downloads::File::Error) do
@download.http_get_streaming(:max_size => 1) do |resp|
VCR.use_cassette("download-file-http", :record => :once) do
@download.http_get_streaming(:max_size => 1) do |resp|
end
end
end
end
should "store the file in the tempfile path" do
@download.download!
VCR.use_cassette("download-file-http", :record => :once) do
@download.download!
end
assert_equal(@source, @download.source)
assert(::File.exists?(@tempfile.path), "temp file should exist")
assert(::File.size(@tempfile.path) > 0, "should have data")
end
should "initialize the content type" do
@download.download!
VCR.use_cassette("download-file-http", :record => :once) do
@download.download!
end
assert_match(/image\/gif/, @download.content_type)
end
end
@@ -65,9 +75,11 @@ module Downloads
end
should "stream a file from an HTTPS source" do
@download.http_get_streaming do |resp|
assert_equal("200", resp.code)
assert(resp["Content-Length"].to_i > 0, "File should be larger than 0 bytes")
VCR.use_cassette("download-file-https", :record => :once) do
@download.http_get_streaming do |resp|
assert_equal("200", resp.code)
assert(resp["Content-Length"].to_i > 0, "File should be larger than 0 bytes")
end
end
end
end

View File

@@ -104,10 +104,13 @@ class UploadTest < ActiveSupport::TestCase
should "initialize the final path after downloading a file" do
@upload = FactoryGirl.create(:source_upload)
path = "#{Rails.root}/tmp/test.download.jpg"
assert_nothing_raised {@upload.download_from_source(path)}
assert(File.exists?(path))
assert_equal(8558, File.size(path))
assert_equal(path, @upload.file_path)
VCR.use_cassette("upload-test-file", :record => :once) do
assert_nothing_raised {@upload.download_from_source(path)}
assert(File.exists?(path))
assert_equal(8558, File.size(path))
assert_equal(path, @upload.file_path)
end
end
end