Permalink
Browse files

Fix Rack::GridFS::Endpoint instantiated directly having no mapper

  • Loading branch information...
1 parent 4709812 commit 7109e063ff0539c5360182099186ae607a278c37 @ches ches committed May 12, 2011
Showing with 103 additions and 17 deletions.
  1. +4 −0 Gemfile
  2. +1 −1 README.rdoc
  3. +1 −1 Rakefile
  4. +1 −0 lib/rack/gridfs.rb
  5. +5 −2 lib/rack/gridfs/endpoint/base.rb
  6. +1 −0 rack-gridfs.gemspec
  7. +1 −1 test/caching_test.rb
  8. +13 −2 test/config_test.rb
  9. +1 −1 test/exceptions_test.rb
  10. +57 −3 test/gridfs_test.rb
  11. +18 −6 test/test_helper.rb
View
@@ -1,2 +1,6 @@
source :rubygems
gemspec
+
+gem 'ruby-debug', :platforms => :mri_18
+gem 'ruby-debug19', :platforms => :mri_19, :require => 'ruby-debug'
+
View
@@ -83,7 +83,7 @@ returns a "static" file or a 404.
To use in Rails 3, you can insert into the middleware stack as above, or mount
the app directly in your routes (recommended). In <tt>config/routes.rb</tt>:
- mount Rack::GridFS::Endpoint, :at => "gridfs"
+ mount Rack::GridFS::Endpoint.new(:db => Mongoid.database), :at => "gridfs"
This allows for much more straightforward and sensible configuration, if you do
not require other middleware in front of GridFS (Rack-based authorization, for
View
@@ -1,4 +1,4 @@
-require 'bundler'
+require 'bundler/setup'
Bundler::GemHelper.install_tasks
require 'rake/testtask'
View
@@ -36,6 +36,7 @@ def call(env)
private
+ # TODO: doc explanation/example of custom mapper
def normalize_options(options)
options.tap do |opts|
opts[:prefix] ||= "gridfs"
@@ -29,7 +29,10 @@ def key_for_path(path)
protected
def default_options
- { :lookup => :id }
+ {
+ :lookup => :id,
+ :mapper => lambda { |path| %r!/(.+)!.match(path)[1] }
+ }
end
def with_rescues
@@ -70,4 +73,4 @@ def headers(file)
end
end
end
-end
+end
View
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('bundler', '>= 1.0.0')
s.add_development_dependency('mocha', '0.9.12')
s.add_development_dependency('rack-test')
+ s.add_development_dependency('rake')
s.add_development_dependency('shoulda')
s.files = Dir.glob("lib/**/*") + %w(LICENSE README.rdoc Rakefile)
View
@@ -7,7 +7,7 @@ class CachingTest < Test::Unit::TestCase
context "Rack::GridFS::Endpoint::Caching" do
setup do
def app
- setup_app(:lookup => :path, :expires => 1800)
+ setup_endpoint(:lookup => :path, :expires => 1800)
end
@text_file = load_artifact('test.txt', nil, path='text')
View
@@ -15,7 +15,8 @@ class ConfigTest < Test::Unit::TestCase
:database => 'mydatabase',
:prefix => 'myprefix',
:username => 'bob',
- :password => 'so-s3cur3'
+ :password => 'so-s3cur3',
+ :mapper => lambda { |path| %r{^/files/myprefix/(.+)}.match(path)[1] }
}
end
@@ -59,7 +60,7 @@ class ConfigTest < Test::Unit::TestCase
assert_equal mware.instance_variable_get(:@options)[:prefix], 'gridfs'
end
- should "have a normalize prefix" do
+ should "have a normalized prefix" do
mware = Rack::GridFS.new(nil, @options.merge({:prefix => '/myprefix'}))
assert_equal mware.instance_variable_get(:@options)[:prefix], 'myprefix'
end
@@ -84,6 +85,16 @@ class ConfigTest < Test::Unit::TestCase
assert_nil mware.instance_variable_get(:@password)
end
+ should "have a mapper option" do
+ mware = Rack::GridFS::Endpoint.new(@options)
+ assert_equal @options[:mapper], mware.instance_variable_get(:@mapper)
+ end
+
+ should "have a default mapper" do
+ mware = Rack::GridFS::Endpoint.new(@options.except(:mapper))
+ assert_not_nil mware.instance_variable_get(:@options)[:mapper]
+ end
+
should "connect to the MongoDB server" do
Rack::GridFS::Endpoint.any_instance.expects(:connect!).returns(true).once
Rack::GridFS::Endpoint.new(@options).db
View
@@ -7,7 +7,7 @@ class ExceptionsTest < Test::Unit::TestCase
context "Rack::GridFS Exceptions" do
setup do
def app
- setup_app(:lookup => :path)
+ setup_middleware(:lookup => :path)
end
@text_file = load_artifact('test.txt', nil)
View
@@ -1,12 +1,13 @@
require 'test_helper'
require 'pp'
+
class Rack::GridFSTest < Test::Unit::TestCase
include Rack::Test::Methods
include Rack::GridFS::Test::Methods
context "Rack::GridFS" do
setup do
- def app; setup_app end
+ def app; setup_middleware end
end
should "load artifacts" do
@@ -30,7 +31,6 @@ def app; setup_app end
context "for lookup by ObjectId" do
setup do
- def app; setup_app(:lookup => :id) end
@text_id = load_artifact('test.txt', 'text/plain')
@html_id = load_artifact('test.html', 'text/html')
end
@@ -68,7 +68,7 @@ def app; setup_app(:lookup => :id) end
context "for lookup by filename" do
setup do
- def app; setup_app(:lookup => :path) end
+ def app; setup_middleware(:lookup => :path) end
@text_file = load_artifact('test.txt', nil, path='text')
@html_file = load_artifact('test.html', nil, path='html')
end
@@ -112,5 +112,59 @@ def app; setup_app(:lookup => :path) end
end
+ context "Rack::GridFS::Endpoint" do
+ context "for lookup by ObjectId" do
+ setup do
+ def app; setup_endpoint end
+ @text_id = load_artifact('test.txt', 'text/plain')
+ end
+
+ teardown do
+ db.collection('fs.files').remove
+ end
+
+ should "return TXT files stored in GridFS" do
+ get "/gridfs/#{@text_id}"
+ assert_equal "Lorem ipsum dolor sit amet.", last_response.body
+ end
+
+ should "return a not found for a unknown object" do
+ get '/gridfs/unknown'
+ assert last_response.not_found?
+ end
+
+ should "return the proper content type for TXT files" do
+ get "/gridfs/#{@text_id}"
+ assert_equal 'text/plain', last_response.content_type
+ end
+ end
+
+ context "for lookup by filename" do
+ setup do
+ def app; setup_endpoint(:lookup => :path) end
+ @text_file = load_artifact('test.txt', nil, path='text')
+ end
+
+ teardown do
+ db.collection('fs.files').remove
+ end
+
+ should "return TXT files stored in GridFS" do
+ get "/gridfs/#{@text_file.filename}"
+ assert_equal "Lorem ipsum dolor sit amet.", last_response.body
+ end
+
+ should "return the proper content type for TXT files" do
+ get "/gridfs/#{@text_file.filename}"
+ assert_equal 'text/plain', last_response.content_type
+ end
+
+ should "return a not found for a unknown path" do
+ get '/gridfs/unknown'
+ assert last_response.not_found?
+ end
+ end
+ end
+
end
View
@@ -1,5 +1,3 @@
-require 'rubygems'
-require 'bundler/setup'
require 'test/unit'
require 'shoulda'
require 'mocha'
@@ -27,14 +25,14 @@ def stub_mongodb_connection
end
def test_database_options
- { :hostname => 'localhost', :port => 27017, :database => 'test', :prefix => 'gridfs', :lookup => :path }
+ { :hostname => 'localhost', :port => 27017, :database => 'test', :prefix => 'gridfs' }
end
def db
@db ||= Mongo::Connection.new(test_database_options[:hostname], test_database_options[:port]).db(test_database_options[:database])
end
- def setup_app(opts={})
+ def setup_middleware(opts={})
gridfs_opts = test_database_options.merge(opts)
Rack::Builder.new do
@@ -44,14 +42,28 @@ def setup_app(opts={})
end
end
+ def setup_endpoint(opts={})
+ endpoint_opts = test_database_options.except(:prefix).merge(opts)
+
+ Rack::Builder.new do
+ use Rack::ConditionalGet
+ map '/gridfs' do
+ run Rack::GridFS::Endpoint.new(endpoint_opts)
+ end
+ map '/' do
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
+ end
+ end
+ end
+
def load_artifact(filename, content_type, path=nil)
contents = ::File.read(::File.join(::File.dirname(__FILE__), 'artifacts', filename))
if path
grid = Mongo::GridFileSystem.new(db)
file = [path, filename].join('/')
grid.open(file, 'w') { |f| f.write contents }
grid.open(file, 'r')
- else
+ else
Mongo::Grid.new(db).put(contents, :filename => filename, :content_type => content_type)
end
end
@@ -66,4 +78,4 @@ def assert_equal_header(expected, header)
end
end
end
-end
+end

0 comments on commit 7109e06

Please sign in to comment.