summaryrefslogtreecommitdiff
path: root/spec/support/shared/functional/http.rb
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2014-04-21 14:52:07 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2014-04-21 14:52:07 -0700
commitbc8048b41ab6c0c320fbb2de42a72ce2df29a79d (patch)
tree21cef6b228278355f81c779958d81cc542a6f889 /spec/support/shared/functional/http.rb
parent24fbf5ac7b5b1e605871da959ba87dc2b9a9bab6 (diff)
downloadchef-bc8048b41ab6c0c320fbb2de42a72ce2df29a79d.tar.gz
wrap methods in a module
Diffstat (limited to 'spec/support/shared/functional/http.rb')
-rw-r--r--spec/support/shared/functional/http.rb227
1 files changed, 115 insertions, 112 deletions
diff --git a/spec/support/shared/functional/http.rb b/spec/support/shared/functional/http.rb
index c6be93550a..86fe467d92 100644
--- a/spec/support/shared/functional/http.rb
+++ b/spec/support/shared/functional/http.rb
@@ -20,136 +20,139 @@
# shared code for Chef::REST and Chef::HTTP::Simple and other Chef::HTTP wrappers
#
-def nyan_uncompressed_filename
- File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png')
-end
-
-def nyan_compressed_filename
- File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png.gz')
-end
-
-def binread(file)
- content = File.open(file, "rb") do |f|
- f.read
+module ChefHTTPShared
+ def nyan_uncompressed_filename
+ File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png')
end
- content.force_encoding(Encoding::BINARY) if "".respond_to?(:force_encoding)
- content
-end
-def start_tiny_server(server_opts={})
- 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
- #
-
- # just a normal file
- # (expected_content should be uncompressed)
- @api.get("/nyan_cat.png", 200) {
- File.open(nyan_uncompressed_filename, "rb") do |f|
- f.read
- end
- }
-
- # this ends in .gz, we do not uncompress it and drop it on the filesystem as a .gz file (the internet often lies)
- # (expected_content should be compressed)
- @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
- }
+ def nyan_compressed_filename
+ File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png.gz')
+ end
- # this is an uncompressed file that was compressed by some mod_gzip-ish webserver thingy, so we will expand it
- # (expected_content should be uncompressed)
- @api.get("/nyan_cat_compressed.png", 200, nil, { 'Content-Type' => 'application/gzip', 'Content-Encoding' => 'gzip' } ) {
- File.open(nyan_compressed_filename, "rb") do |f|
+ 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
- #
- # endpoints that set Content-Length correctly
- #
+ def start_tiny_server(server_opts={})
+ 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
+ #
+
+ # just a normal file
+ # (expected_content should be uncompressed)
+ @api.get("/nyan_cat.png", 200) {
+ File.open(nyan_uncompressed_filename, "rb") do |f|
+ f.read
+ end
+ }
- # (expected_content should be uncompressed)
- @api.get("/nyan_cat_content_length.png", 200, nil,
- {
- 'Content-Length' => nyan_uncompressed_size.to_s,
+ # this ends in .gz, we do not uncompress it and drop it on the filesystem as a .gz file (the internet often lies)
+ # (expected_content should be compressed)
+ @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
}
- ) {
- File.open(nyan_uncompressed_filename, "rb") do |f|
- f.read
- end
- }
-
- # (expected_content should be uncompressed)
- @api.get("/nyan_cat_content_length_compressed.png", 200, nil,
- {
- 'Content-Length' => nyan_compressed_size.to_s,
- 'Content-Type' => 'application/gzip',
- 'Content-Encoding' => 'gzip'
+
+ # this is an uncompressed file that was compressed by some mod_gzip-ish webserver thingy, so we will expand it
+ # (expected_content should be uncompressed)
+ @api.get("/nyan_cat_compressed.png", 200, nil, { 'Content-Type' => 'application/gzip', 'Content-Encoding' => 'gzip' } ) {
+ File.open(nyan_compressed_filename, "rb") do |f|
+ f.read
+ end
}
- ) {
- File.open(nyan_compressed_filename, "rb") do |f|
- f.read
- end
- }
- #
- # endpoints that simulate truncated downloads (bad content-length header)
- #
+ #
+ # endpoints that set Content-Length correctly
+ #
+
+ # (expected_content should be uncompressed)
+ @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
+ }
- # (expected_content should be uncompressed)
- @api.get("/nyan_cat_truncated.png", 200, nil,
- {
- 'Content-Length' => (nyan_uncompressed_size + 1).to_s,
+ # (expected_content should be uncompressed)
+ @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
}
- ) {
- File.open(nyan_uncompressed_filename, "rb") do |f|
- f.read
- end
- }
-
- # (expected_content should be uncompressed)
- @api.get("/nyan_cat_truncated_compressed.png", 200, nil,
- {
- 'Content-Length' => (nyan_compressed_size + 1).to_s,
- 'Content-Type' => 'application/gzip',
- 'Content-Encoding' => 'gzip'
+
+ #
+ # endpoints that simulate truncated downloads (bad content-length header)
+ #
+
+ # (expected_content should be uncompressed)
+ @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
}
- ) {
- 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)
- #
+ # (expected_content should be uncompressed)
+ @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
+ }
- # (expected_content should be uncompressed)
- @api.get("/nyan_cat_transfer_encoding.png", 200, nil,
- {
- 'Content-Length' => (nyan_uncompressed_size + 1).to_s,
- 'Transfer-Encoding' => 'anything',
+ #
+ # in the presense of a transfer-encoding header, we must ignore the content-length (this bad content-length should work)
+ #
+
+ # (expected_content should be uncompressed)
+ @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
}
- ) {
- File.open(nyan_uncompressed_filename, "rb") do |f|
- f.read
- end
- }
-end
+ end
+
+ def stop_tiny_server
+ @server.stop
+ @server = @api = nil
+ end
-def stop_tiny_server
- @server.stop
- @server = @api = nil
end
shared_examples_for "downloading all the things" do