Skip to content

Commit

Permalink
Merge branch 'master' into better_logging
Browse files Browse the repository at this point in the history
Conflicts:
	lib/sinatra/base.rb
  • Loading branch information
rkh committed Mar 13, 2011
2 parents 85a80e0 + 654eebb commit 02cd967
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGES
@@ -1,3 +1,8 @@
= 1.2.1 / Not Yet Release

* Use a generated session secret when using `enable :sessions`. (Konstantin
Haase)

= 1.2.0 / 2011-03-03

* Added `slim` rendering method for rendering Slim templates. (Steve
Expand Down
18 changes: 13 additions & 5 deletions lib/sinatra/base.rb
Expand Up @@ -7,7 +7,7 @@
require 'tilt'

module Sinatra
VERSION = '1.2.0'
VERSION = '1.2.1'

# The request object. See Rack::Request for more info:
# http://rack.rubyforge.org/doc/classes/Rack/Request.html
Expand Down Expand Up @@ -1237,10 +1237,10 @@ def new(*args, &bk)
# an instance of this class as end point.
def build(*args, &bk)
builder = Rack::Builder.new
setup_logging builder
builder.use Rack::Session::Cookie if sessions?
builder.use Rack::MethodOverride if method_override?
builder.use ShowExceptions if show_exceptions?
setup_logging builder
setup_sessions builder
builder.use Rack::MethodOverride if method_override?
builder.use ShowExceptions if show_exceptions?
middleware.each { |c,a,b| builder.use(c, *a, &b) }
builder.run new!(*args, &bk)
builder
Expand All @@ -1264,6 +1264,11 @@ def setup_logging(builder)
end
end

def setup_sessions(builder)
return unless sessions?
builder.use Rack::Session::Cookie, :secret => session_secret
end

def detect_rack_handler
servers = Array(server)
servers.each do |server_name|
Expand Down Expand Up @@ -1360,6 +1365,9 @@ def self.force_encoding(data, *) data end
set :default_encoding, "utf-8"
set :add_charset, [/^text\//, 'application/javascript', 'application/xml', 'application/xhtml+xml']

# explicitly generating this eagerly to play nice with preforking
set :session_secret, '%x' % rand(2**255)

class << self
alias_method :methodoverride?, :method_override?
alias_method :methodoverride=, :method_override=
Expand Down
6 changes: 6 additions & 0 deletions test/helper.rb
Expand Up @@ -25,6 +25,12 @@ class Sinatra::Base
include Test::Unit::Assertions
end

class Rack::Builder
def include?(middleware)
@ins.any? { |m| p m ; middleware === m }
end
end

Sinatra::Base.set :environment, :test

class Test::Unit::TestCase
Expand Down
29 changes: 29 additions & 0 deletions test/helpers_test.rb
Expand Up @@ -255,6 +255,35 @@ def test_default
follow_redirect!
assert_equal 'hi bar', body
end

it 'inserts session middleware' do
mock_app do
enable :sessions
get '/' do
assert env['rack.session']
assert env['rack.session.options']
'ok'
end
end

get '/'
assert_body 'ok'
end

it 'sets a default session secret' do
mock_app do
enable :sessions
get '/' do
secret = env['rack.session.options'][:secret]
assert secret
assert_equal secret, settings.session_secret
'ok'
end
end

get '/'
assert_body 'ok'
end
end

describe 'mime_type' do
Expand Down

0 comments on commit 02cd967

Please sign in to comment.