Skip to content

Commit

Permalink
Added configuration options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake Carlson committed Sep 21, 2009
1 parent fd27ad0 commit d37dca9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 13 deletions.
37 changes: 35 additions & 2 deletions lib/rack/gridfs.rb
@@ -1,12 +1,45 @@
require 'mongo'
require 'timeout'
require 'active_support/core_ext'

module Rack

class GridFSConnectonError < StandardError ; end

class GridFS

attr_reader :hostname, :port, :database, :prefix, :connection

def initialize(app, options = {})
@app = app
@options = options
options.reverse_merge!(
:hostname => 'localhost',
:port => Mongo::Connection::DEFAULT_PORT,
:prefix => 'gridfs'
)

@app = app
@hostname = options[:hostname]
@port = options[:port]
@database = options[:database]
@prefix = options[:prefix]

connect!
end

def call(env)
@app.call(env)
end

private

def connect!
Timeout::timeout(5) do
self.connection = Mongo::Connection.new(hostname).db(database)
end
rescue
raise Rack::GridFSConnectonError, 'Unable to connect to the MongoDB server'
end

end

end
81 changes: 70 additions & 11 deletions test/rack/gridfs_test.rb
Expand Up @@ -3,6 +3,10 @@
class Rack::GridFSTest < Test::Unit::TestCase
include Rack::Test::Methods

def options_for_gridfs
{ :hostname => 'myhostname.mydomain', :port => 8765, :database => 'mydatabase', :prefix => 'myprefix' }
end

def app
Rack::Builder.new do
use Rack::GridFS
Expand All @@ -11,22 +15,77 @@ def app
end

context "Rack::GridFS" do

should "run a mock request" do
get '/'
assert last_response.ok?

setup do
Rack::GridFS.any_instance.stubs(:connect!).returns(true)
end

should "run an old school mock request" do
app = Rack::Builder.new do
use Rack::GridFS
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
context "on initialization" do

should "have a hostname option" do
mware = Rack::GridFS.new(nil, options_for_gridfs)
assert_equal options_for_gridfs[:hostname], mware.hostname
end

should "have a default hostname" do
mware = Rack::GridFS.new(nil, options_for_gridfs.except(:hostname))
assert_equal 'localhost', mware.hostname
end

should "have a port option" do
mware = Rack::GridFS.new(nil, options_for_gridfs)
assert_equal options_for_gridfs[:port], mware.port
end

should "have a default port" do
mware = Rack::GridFS.new(nil, options_for_gridfs.except(:port))
assert_equal Mongo::Connection::DEFAULT_PORT, mware.port
end

should "have a database option" do
mware = Rack::GridFS.new(nil, options_for_gridfs)
assert_equal options_for_gridfs[:database], mware.database
end

should "not have a default database" do
mware = Rack::GridFS.new(nil, options_for_gridfs.except(:database))
assert_nil mware.database
end

should "have a prefix option" do
mware = Rack::GridFS.new(nil, options_for_gridfs)
assert_equal mware.prefix, options_for_gridfs[:prefix]
end

should "have a default prefix" do
mware = Rack::GridFS.new(nil, options_for_gridfs.except(:prefix))
assert_equal mware.prefix, 'gridfs'
end

should "connect to the MongoDB server" do
Rack::GridFS.any_instance.expects(:connect!).returns(true).once
Rack::GridFS.new(nil, options_for_gridfs)
end

response = Rack::MockRequest.new(app).get('/')
assert response.ok?
end


context "experimenting with mock requests" do
should "run a mock request" do
get '/'
assert last_response.ok?
end

should "run an old school mock request" do
app = Rack::Builder.new do
use Rack::GridFS
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
end

response = Rack::MockRequest.new(app).get('/')
assert response.ok?
end
end

end

end
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
@@ -1,9 +1,13 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'

gem 'mocha', '0.9.4'
require 'mocha'

require 'rack/builder'
require 'rack/mock'
require 'rack/test'

require 'mongo'
require File.join(File.dirname(__FILE__), '..', 'lib', 'rack', 'gridfs')

0 comments on commit d37dca9

Please sign in to comment.