Browse files

add support for Storage::Rackspace::File#access_control_allow_origin …

…and #origin

Support Access-Control-Allow-Origin and Origin headers, borrowed
testing ideas from pull request #1251
  • Loading branch information...
1 parent 7af36b0 commit 7d1f83d294ce2672b14b0f5074d7c54dad59d709 @dustacio committed Nov 7, 2012
Showing with 77 additions and 0 deletions.
  1. +4 −0 lib/fog/rackspace/models/storage/file.rb
  2. +73 −0 tests/rackspace/models/storage/file_tests.rb
View
4 lib/fog/rackspace/models/storage/file.rb
@@ -12,6 +12,8 @@ class File < Fog::Model
attribute :content_type, :aliases => ['content_type', 'Content-Type']
attribute :etag, :aliases => ['hash', 'Etag']
attribute :last_modified, :aliases => ['last_modified', 'Last-Modified'], :type => :time
+ attribute :access_control_allow_origin, :aliases => ['Access-Control-Allow-Origin']
+ attribute :origin, :aliases => ['Origin']
def body
attributes[:body] ||= if last_modified
@@ -64,6 +66,8 @@ def public_url
def save(options = {})
requires :body, :directory, :key
options['Content-Type'] = content_type if content_type
+ options['Access-Control-Allow-Origin'] = access_control_allow_origin if access_control_allow_origin
+ options['Origin'] = origin if origin
data = connection.put_object(directory.key, key, body, options)
merge_attributes(data.headers.reject {|key, value| ['Content-Length', 'Content-Type'].include?(key)})
self.content_length = Fog::Storage.get_body_size(body)
View
73 tests/rackspace/models/storage/file_tests.rb
@@ -0,0 +1,73 @@
+Shindo.tests('Fog::Rackspace::Storage | file', ['rackspace']) do
+
+ file_attributes = {
+ :key => 'fog_file_tests',
+ :body => lorem_file
+ }
+
+ directory_attributes = {
+ # Add a random suffix to prevent collision
+ :key => "fogfilestests-#{rand(65536)}"
+ }
+
+ @directory = Fog::Storage[:rackspace].
+ directories.
+ create(directory_attributes)
+
+ model_tests(@directory.files, file_attributes, Fog.mocking?) do
+
+ tests("#access_control_allow_origin") do
+
+ tests("#access_control_allow_origin should default to nil").returns(nil) do
+ @instance.access_control_allow_origin
+ end
+
+ @instance.access_control_allow_origin = 'http://example.com'
+ tests("#access_control_allow_origin should return access control attribute").returns('http://example.com') do
+ @instance.access_control_allow_origin
+ end
+
+ @instance.access_control_allow_origin = 'foo'
+ tests("#access_control_allow_origin= should update access_control_allow_origin").returns('bar') do
+ @instance.access_control_allow_origin = 'bar'
+ @instance.access_control_allow_origin
+ end
+
+ tests("#access_control_allow_origin= should not blow up on nil") do
+ @instance.access_control_allow_origin = nil
+ end
+
+ end
+
+ end
+
+
+ model_tests(@directory.files, file_attributes, Fog.mocking?) do
+
+ tests("#origin") do
+
+ tests("#origin should default to nil").returns(nil) do
+ @instance.origin
+ end
+
+ @instance.origin = 'http://example.com'
+ tests("#origin should return access control attributes").returns('http://example.com') do
+ @instance.origin
+ end
+ @instance.attributes.delete('Origin')
+
+ @instance.attributes['Origin'] = 'foo'
+ tests("#origin= should update origin").returns('bar') do
+ @instance.origin = 'bar'
+ @instance.origin
+ end
+
+ tests("#origin= should not blow up on nil") do
+ @instance.origin = nil
+ end
+
+ end
+
+ end
+ @directory.destroy
+end

0 comments on commit 7d1f83d

Please sign in to comment.