happening 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -90,6 +90,23 @@ Setting permissions looks like this:
90
90
  item = Happening::S3::Item.new('bucket', 'item_id', :aws_access_key_id => 'Your-ID', :aws_secret_access_key => 'secret', :permissions => 'public-write')
91
91
  item.get(:on_success => on_success, :on_error => on_error)
92
92
  end
93
+
94
+ Custom headers:
95
+
96
+ EM.run do
97
+ on_error = Proc.new {|http| puts "An error occured: #{http.response_header.status}"; EM.stop }
98
+ on_success = Proc.new {|http| puts "the response is: #{http.response}"; EM.stop }
99
+ item = Happening::S3::Item.new('bucket', 'item_id', :aws_access_key_id => 'Your-ID', :aws_secret_access_key => 'secret', :permissions => 'public-write')
100
+ item.put(:on_success => on_success,
101
+ :on_error => on_error,
102
+ :headers => {
103
+ 'Cache-Control' => "max-age=252460800",
104
+ 'Content-Type' => 'text/html',
105
+ 'Expires' => 'Fri, 16 Nov 2018 22:09:29 GMT',
106
+ 'x-amz-meta-abc' => 'ABC'
107
+ })
108
+ end
109
+
93
110
 
94
111
  Deleting
95
112
  =============
@@ -6,6 +6,7 @@ module Happening
6
6
  class Item
7
7
 
8
8
  REQUIRED_FIELDS = [:server]
9
+ VALID_HEADERS = ['Cache-Control', 'Content-Disposition', 'Content-Encoding', 'Content-Length', 'Content-MD5', 'Content-Type', 'Expect', 'Expires']
9
10
 
10
11
  attr_accessor :bucket, :aws_id, :options
11
12
 
@@ -34,8 +35,7 @@ module Happening
34
35
  end
35
36
 
36
37
  def put(data, request_options = {}, &blk)
37
- permissions = options[:permissions] != 'private' ? {'x-amz-acl' => options[:permissions] } : {}
38
- headers = needs_to_sign? ? aws.sign("PUT", path, permissions.update({'url' => path})) : {}
38
+ headers = construct_aws_headers('PUT', request_options.delete(:headers) || {})
39
39
  request_options[:on_success] = blk if blk
40
40
  request_options.update(:headers => headers, :data => data)
41
41
  Happening::S3::Request.new(:put, url, request_options).execute
@@ -93,6 +93,18 @@ module Happening
93
93
  def aws
94
94
  @aws ||= Happening::AWS.new(options[:aws_access_key_id], options[:aws_secret_access_key])
95
95
  end
96
+
97
+ def construct_aws_headers(http_method, headers = {})
98
+ unless headers.keys.all?{|header| VALID_HEADERS.include?(header) || header.to_s.match(/\Ax-amz-/) }
99
+ raise ArgumentError, "invalid headers. All headers must either one of #{VALID_HEADERS} or start with 'x-amz-'"
100
+ end
101
+
102
+ permissions = options[:permissions] != 'private' ? {'x-amz-acl' => options[:permissions] } : {}
103
+ headers.update(permissions)
104
+ headers.update({'url' => path})
105
+
106
+ headers = needs_to_sign? ? aws.sign(http_method, path, headers) : headers
107
+ end
96
108
 
97
109
  end
98
110
  end
data/test/s3/item_test.rb CHANGED
@@ -329,6 +329,53 @@ class ItemTest < Test::Unit::TestCase
329
329
  end
330
330
  end
331
331
 
332
+ should "allow to set custom headers" do
333
+ EventMachine::MockHttpRequest.register('https://bucket.s3.amazonaws.com:443/the-key', :put, {
334
+ "Authorization"=>"AWS abc:wrPkGKrlwH2AtNzBVS80vU73TDc=",
335
+ 'date' => @time,
336
+ 'url' => "/bucket/the-key",
337
+ "x-amz-acl" => 'public-read',
338
+ 'Cache-Control' => "max-age=252460800",
339
+ 'Expires' => 'Fri, 16 Nov 2018 22:09:29 GMT',
340
+ 'x-amz-meta-abc' => 'ABC'}, fake_response("data-here"))
341
+
342
+ @item = Happening::S3::Item.new('bucket', 'the-key', :aws_access_key_id => 'abc',
343
+ :aws_secret_access_key => '123' ,
344
+ :permissions => 'public-read')
345
+ run_in_em_loop do
346
+ @item.put('content', :headers => {
347
+ 'Expires' => 'Fri, 16 Nov 2018 22:09:29 GMT',
348
+ 'Cache-Control' => "max-age=252460800",
349
+ 'x-amz-meta-abc' => 'ABC'})
350
+
351
+ EM.add_timer(1) {
352
+ EM.stop_event_loop
353
+ assert_equal 1, EventMachine::MockHttpRequest.count('https://bucket.s3.amazonaws.com:443/the-key', :put, {
354
+ "Authorization"=>"AWS abc:wrPkGKrlwH2AtNzBVS80vU73TDc=",
355
+ 'date' => @time,
356
+ 'url' => "/bucket/the-key",
357
+ 'x-amz-acl' => 'public-read',
358
+ 'Cache-Control' => "max-age=252460800",
359
+ 'Expires' => 'Fri, 16 Nov 2018 22:09:29 GMT',
360
+ 'x-amz-meta-abc' => 'ABC'})
361
+ }
362
+
363
+ end
364
+ end
365
+
366
+ should "validate the headers" do
367
+
368
+ @item = Happening::S3::Item.new('bucket', 'the-key', :aws_access_key_id => 'abc',
369
+ :aws_secret_access_key => '123' ,
370
+ :permissions => 'public-read')
371
+
372
+ assert_raise(ArgumentError) do
373
+ @item.put('content', :headers => {
374
+ 'expires' => 'Fri, 16 Nov 2018 22:09:29 GMT',
375
+ 'cache_control' => "max-age=252460800"})
376
+ end
377
+ end
378
+
332
379
  should "re-post to a new location" do
333
380
  EventMachine::MockHttpRequest.register('https://bucket.s3.amazonaws.com:443/the-key', :put, {
334
381
  "Authorization"=>"AWS abc:lZMKxGDKcQ1PH8yjbpyN7o2sPWg=",
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
9
- version: 0.0.6
8
+ - 7
9
+ version: 0.0.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jonathan Weiss