summaryrefslogtreecommitdiff
path: root/spec/functional/resource/remote_file_spec.rb
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2014-04-11 16:03:13 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2014-04-11 16:03:13 -0700
commit24fbf5ac7b5b1e605871da959ba87dc2b9a9bab6 (patch)
tree7dda1ac827180e129d20b55e12cd86e340eaecf1 /spec/functional/resource/remote_file_spec.rb
parentc96317f806cccc1db975eac2e94842cd1cba2ca2 (diff)
downloadchef-24fbf5ac7b5b1e605871da959ba87dc2b9a9bab6.tar.gz
DRY up remote_file func specs
Diffstat (limited to 'spec/functional/resource/remote_file_spec.rb')
-rw-r--r--spec/functional/resource/remote_file_spec.rb141
1 files changed, 7 insertions, 134 deletions
diff --git a/spec/functional/resource/remote_file_spec.rb b/spec/functional/resource/remote_file_spec.rb
index 4468951a7a..0161f6ef56 100644
--- a/spec/functional/resource/remote_file_spec.rb
+++ b/spec/functional/resource/remote_file_spec.rb
@@ -18,6 +18,7 @@
require 'spec_helper'
require 'tiny_server'
+require 'support/shared/functional/http'
describe Chef::Resource::RemoteFile do
@@ -52,107 +53,6 @@ describe Chef::Resource::RemoteFile do
let(:default_mode) { ((0100666 - File.umask) & 07777).to_s(8) }
- def start_tiny_server(server_opts={})
- nyan_uncompressed_filename = File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png')
- nyan_compressed_filename = File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png.gz')
- nyan_uncompressed_size = File::Stat.new(nyan_uncompressed_filename).size
- nyan_compressed_size = File::Stat.new(nyan_compressed_filename).size
-
- @server = TinyServer::Manager.new(server_opts)
- @server.start
- @api = TinyServer::API.instance
- @api.clear
-
- #
- # trivial endpoints
- #
-
- @api.get("/nyan_cat.png", 200) {
- File.open(nyan_uncompressed_filename, "rb") do |f|
- f.read
- end
- }
- @api.get("/nyan_cat.png.gz", 200, nil, { 'Content-Type' => 'application/gzip', 'Content-Encoding' => 'gzip' } ) {
- File.open(nyan_compressed_filename, "rb") do |f|
- f.read
- end
- }
-
- #
- # endpoints that set Content-Length correctly
- #
-
- @api.get("/nyan_cat_content_length.png", 200, nil,
- {
- 'Content-Length' => nyan_uncompressed_size.to_s,
- }
- ) {
- File.open(nyan_uncompressed_filename, "rb") do |f|
- f.read
- end
- }
-
- # this is sent over the wire compressed by the server, but does not have a .gz extension
- @api.get("/nyan_cat_content_length_compressed.png", 200, nil,
- {
- 'Content-Length' => nyan_compressed_size.to_s,
- 'Content-Type' => 'application/gzip',
- 'Content-Encoding' => 'gzip'
- }
- ) {
- File.open(nyan_compressed_filename, "rb") do |f|
- f.read
- end
- }
-
- #
- # endpoints that simulate truncated downloads (bad content-length header)
- #
-
- @api.get("/nyan_cat_truncated.png", 200, nil,
- {
- 'Content-Length' => (nyan_uncompressed_size + 1).to_s,
- }
- ) {
- File.open(nyan_uncompressed_filename, "rb") do |f|
- f.read
- end
- }
- # this is sent over the wire compressed by the server, but does not have a .gz extension
- @api.get("/nyan_cat_truncated_compressed.png", 200, nil,
- {
- 'Content-Length' => (nyan_compressed_size + 1).to_s,
- 'Content-Type' => 'application/gzip',
- 'Content-Encoding' => 'gzip'
- }
- ) {
- File.open(nyan_compressed_filename, "rb") do |f|
- f.read
- end
- }
-
- #
- # in the presense of a transfer-encoding header, we must ignore the content-length (this bad content-length should work)
- #
-
- @api.get("/nyan_cat_transfer_encoding.png", 200, nil,
- {
- 'Content-Length' => (nyan_uncompressed_size + 1).to_s,
- 'Transfer-Encoding' => 'anything',
- }
- ) {
- File.open(nyan_uncompressed_filename, "rb") do |f|
- f.read
- end
- }
-
- end
-
- def stop_tiny_server
- @server.stop
- @server = @api = nil
- end
-
context "when fetching files over HTTP" do
before(:all) do
start_tiny_server
@@ -177,13 +77,7 @@ describe Chef::Resource::RemoteFile do
context "when using normal encoding" do
let(:source) { 'http://localhost:9000/nyan_cat.png' }
- let(:expected_content) do
- content = File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png'), "rb") do |f|
- f.read
- end
- content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding)
- content
- end
+ let(:expected_content) { binread(nyan_uncompressed_filename) }
it_behaves_like "a file resource"
@@ -192,13 +86,7 @@ describe Chef::Resource::RemoteFile do
context "when using gzip encoding" do
let(:source) { 'http://localhost:9000/nyan_cat.png.gz' }
- let(:expected_content) do
- content = File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png.gz'), "rb") do |f|
- f.read
- end
- content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding)
- content
- end
+ let(:expected_content) { binread(nyan_compressed_filename) }
it_behaves_like "a file resource"
@@ -229,28 +117,13 @@ describe Chef::Resource::RemoteFile do
let(:source) { 'https://localhost:9000/nyan_cat.png' }
- let(:expected_content) do
- content = File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png'), "rb") do |f|
- f.read
- end
- content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding)
- content
- end
+ let(:expected_content) { binread(nyan_uncompressed_filename) }
it_behaves_like "a file resource"
end
context "when dealing with content length checking" do
-
- def binread(file)
- content = File.open(file, "rb") do |f|
- f.read
- end
- content.force_encoding(Encoding::BINARY) if "".respond_to?(:force_encoding)
- content
- end
-
before(:all) do
start_tiny_server
end
@@ -260,7 +133,7 @@ describe Chef::Resource::RemoteFile do
end
context "when downloading compressed data" do
- let(:expected_content) { binread( File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png') ) }
+ let(:expected_content) { binread(nyan_uncompressed_filename) }
let(:source) { 'http://localhost:9000/nyan_cat_content_length_compressed.png' }
before do
@@ -282,7 +155,7 @@ describe Chef::Resource::RemoteFile do
end
context "when downloding uncompressed data" do
- let(:expected_content) { binread( File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png') ) }
+ let(:expected_content) { binread(nyan_uncompressed_filename) }
let(:source) { 'http://localhost:9000/nyan_cat_content_length.png' }
before do
@@ -330,7 +203,7 @@ describe Chef::Resource::RemoteFile do
end
context "when downloding data with transfer-encoding set" do
- let(:expected_content) { binread( File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png') ) }
+ let(:expected_content) { binread(nyan_uncompressed_filename) }
let(:source) { 'http://localhost:9000/nyan_cat_transfer_encoding.png' }
before do