From 268f79c3d9b954a6bf859acff6fcbb0fd5c9c22b Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 27 Sep 2014 00:28:53 -0500 Subject: [PATCH] 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. --- test/test_helper.rb | 2 +- test/unit/downloads/file_test.rb | 32 ++++++++++++++++++++++---------- test/unit/upload_test.rb | 11 +++++++---- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 55b011b99..cea5a584a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 diff --git a/test/unit/downloads/file_test.rb b/test/unit/downloads/file_test.rb index 689012d82..8a55a7d1b 100644 --- a/test/unit/downloads/file_test.rb +++ b/test/unit/downloads/file_test.rb @@ -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 diff --git a/test/unit/upload_test.rb b/test/unit/upload_test.rb index 0fd39a8f1..ecc1d8ccb 100644 --- a/test/unit/upload_test.rb +++ b/test/unit/upload_test.rb @@ -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