Skip to content

Commit

Permalink
basic post support
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Oct 24, 2013
1 parent f47766e commit ca42dca
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ DEPENDENCIES
bundler (>= 1.0.0)
fakes3!
rake
rest-client
right_aws
3 changes: 2 additions & 1 deletion fakes3.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ Gem::Specification.new do |s|
s.add_development_dependency "bundler", ">= 1.0.0"
s.add_development_dependency "aws-s3"
s.add_development_dependency "right_aws"
s.add_development_dependency "rest-client"
s.add_development_dependency "rake"
#s.add_development_dependency "aws-sdk"
#s.add_development_dependency "ruby-debug"
#s.add_development_dependency "ruby-debug19"
#s.add_development_dependency "debugger"
s.add_dependency "thor"
s.add_dependency "builder"

Expand Down
23 changes: 17 additions & 6 deletions lib/fakes3/file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,25 @@ def store_object(bucket,object_name,request)

md5 = Digest::MD5.new
# TODO put a tmpfile here first and mv it over at the end

File.open(content,'wb') do |f|
request.body do |chunk|
f << chunk
md5 << chunk

match=request.content_type.match(/^multipart\/form-data; boundary=(.+)/)
boundary = match[1] if match
if boundary
boundary = WEBrick::HTTPUtils::dequote(boundary)
filedata = WEBrick::HTTPUtils::parse_form_data(request.body, boundary)
raise HTTPStatus::BadRequest if filedata['file'].empty?
File.open(content, 'wb') do |f|
f<<filedata['file']
md5<<filedata['file']
end
else
File.open(content,'wb') do |f|
request.body do |chunk|
f << chunk
md5 << chunk
end
end
end

metadata_struct = {}
metadata_struct[:md5] = md5.hexdigest
metadata_struct[:content_type] = request.header["content-type"].first
Expand Down
56 changes: 55 additions & 1 deletion lib/fakes3/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,46 @@ def do_PUT(request,response)
response['Content-Type'] = "text/xml"
end

# Posts aren't supported yet
def do_POST(request,response)
# check that we've received file data
unless request.content_type =~ /^multipart\/form-data; boundary=(.+)/
raise WEBrick::HTTPStatus::BadRequest
end
s_req = normalize_request(request)
key=request.query['key']
success_action_redirect=request.query['success_action_redirect']
success_action_status=request.query['success_action_status']

filename = 'default'
filename = $1 if request.body =~ /filename="(.*)"/
key=key.gsub('${filename}', filename)

bucket_obj = @store.get_bucket(s_req.bucket) || @store.create_bucket(s_req.bucket)
real_obj=@store.store_object(bucket_obj, key, s_req.webrick_request)

response['Etag'] = "\"#{real_obj.md5}\""
response.body = ""
if success_action_redirect
response.status = 307
response['Location']=success_action_redirect
else
response.status = success_action_status || 204
if response.status=="201"
response.body= <<-eos
<?xml version="1.0" encoding="UTF-8"?>
<PostResponse>
<Location>http://somethinghere/#{key}</Location>
<Bucket>#{s_req.bucket}</Bucket>
<Key>#{key}</Key>
<ETag>#{response['Etag']}</ETag>
</PostResponse>
eos
end
end
response['Content-Type'] = 'text/xml'
response['Access-Control-Allow-Origin']='*'
puts "response status is #{response.status}"
puts "response body is #{response.body}"
end

def do_DELETE(request,response)
Expand All @@ -169,6 +207,11 @@ def do_DELETE(request,response)
response.status = 204
response.body = ""
end

def do_OPTIONS(request, response)
super
response["Access-Control-Allow-Origin"]="*"
end

private

Expand Down Expand Up @@ -271,6 +314,15 @@ def normalize_put(webrick_req,s_req)
s_req.webrick_request = webrick_req
end

def normalize_post(webrick_req,s_req)
path = webrick_req.path
path_len = path.size

s_req.path = webrick_req.query['key']

s_req.webrick_request = webrick_req
end

# This method takes a webrick request and generates a normalized FakeS3 request
def normalize_request(webrick_req)
host_header= webrick_req["Host"]
Expand All @@ -294,6 +346,8 @@ def normalize_request(webrick_req)
normalize_get(webrick_req,s_req)
when 'DELETE'
normalize_delete(webrick_req,s_req)
when 'POST'
normalize_post(webrick_req,s_req)
else
raise "Unknown Request"
end
Expand Down
51 changes: 51 additions & 0 deletions test/post_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'test/test_helper'
require 'debugger'
require 'rest-client'

class PostTest < Test::Unit::TestCase
def setup
@url='http://posttest.localhost:10453/'
end

def teardown
end

def test_options
res=RestClient.options( @url ) { |response|
assert_equal(response.headers[:access_control_allow_origin],"*")
}
end

def test_redirect
res=RestClient.post( @url,
'key'=>'uploads/12345/${filename}',
'success_action_redirect'=>'http://somewhere.else.com/',
'file'=>File.new(__FILE__,"rb")
){ |response|
assert_equal(response.code, 307)
assert_equal(response.headers[:location], 'http://somewhere.else.com/')
}
end

def test_status_200
res=RestClient.post( @url,
'key'=>'uploads/12345/${filename}',
'success_action_status'=>'200',
'file'=>File.new(__FILE__,"rb")
){ |response|
assert_equal(response.code, 200)
}
end

def test_status_201
res=RestClient.post( @url,
'key'=>'uploads/12345/${filename}',
'success_action_status'=>'201',
'file'=>File.new(__FILE__,"rb")
){ |response|
assert_equal(response.code, 201)
assert_match(response.body, /uploads\/12345\/post_test.rb/)
}
end

end

0 comments on commit ca42dca

Please sign in to comment.