mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
* reset! for tests
* logging
This commit is contained in:
parent
7fe71bf136
commit
eb3ab51e72
7 changed files with 55 additions and 29 deletions
|
@ -122,7 +122,7 @@ module Sinatra
|
|||
def redirect(url)
|
||||
logger.info "Redirecting to: #{url}"
|
||||
status(302)
|
||||
headers.merge!({'Location'=> url})
|
||||
headers.merge!('Location' => url)
|
||||
return ''
|
||||
end
|
||||
|
||||
|
@ -133,6 +133,7 @@ module Sinatra
|
|||
response.send(name, *args)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def setup_default_events!
|
||||
|
@ -206,10 +207,13 @@ module Sinatra
|
|||
def call(env)
|
||||
|
||||
reload! if Sinatra.development?
|
||||
|
||||
time = Time.now
|
||||
|
||||
request = Rack::Request.new(env)
|
||||
|
||||
if found = serve_static_file(request.path_info)
|
||||
log_request_and_response(time, request, Rack::Response.new(found))
|
||||
return found
|
||||
end
|
||||
|
||||
|
@ -223,16 +227,19 @@ module Sinatra
|
|||
begin
|
||||
context = handle_with_filters(context, &route.block)
|
||||
context.status ||= route.default_status
|
||||
context.finish
|
||||
rescue => e
|
||||
raise e if config[:raise_errors]
|
||||
route = Sinatra.routes[500]
|
||||
context.status 500
|
||||
context.body Array(context.instance_eval(&route.block))
|
||||
context.finish
|
||||
ensure
|
||||
log_request_and_response(time, request, response)
|
||||
logger.flush
|
||||
end
|
||||
|
||||
context.finish
|
||||
end
|
||||
|
||||
|
||||
def define_route(verb, path, &b)
|
||||
routes[verb] << route = Route.new(path, &b)
|
||||
route
|
||||
|
@ -247,8 +254,9 @@ module Sinatra
|
|||
end
|
||||
|
||||
def reset!
|
||||
self.config = nil
|
||||
routes.clear
|
||||
config = nil
|
||||
filters.clear
|
||||
setup_default_events!
|
||||
end
|
||||
|
||||
|
@ -260,6 +268,29 @@ module Sinatra
|
|||
end
|
||||
|
||||
protected
|
||||
|
||||
def log_request_and_response(time, request, response)
|
||||
now = Time.now
|
||||
|
||||
# Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
|
||||
# lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
|
||||
# %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
|
||||
logger.info %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n} %
|
||||
[
|
||||
request.env["REMOTE_ADDR"] || "-",
|
||||
request.env["REMOTE_USER"] || "-",
|
||||
now.strftime("%d/%b/%Y %H:%M:%S"),
|
||||
request.env["REQUEST_METHOD"],
|
||||
request.env["PATH_INFO"],
|
||||
request.env["QUERY_STRING"].empty? ?
|
||||
"" :
|
||||
"?" + request.env["QUERY_STRING"],
|
||||
request.env["HTTP_VERSION"],
|
||||
response.status.to_s[0..3].to_i,
|
||||
(response.body.length.zero? ? "-" : response.body.length.to_s),
|
||||
now - time
|
||||
]
|
||||
end
|
||||
|
||||
def handle_with_filters(cx, &b)
|
||||
caught = catch(:halt) do
|
||||
|
@ -310,6 +341,10 @@ module Sinatra
|
|||
Sinatra.config[:default_params].merge(h)
|
||||
end
|
||||
|
||||
def pretty_print(pp)
|
||||
pp.text "{Route: #{@pattern} : [#{@param_keys.map(&:inspect).join(",")}] }"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Error
|
||||
|
@ -372,16 +407,7 @@ def configures(*envs)
|
|||
!Sinatra.config[:reloading]
|
||||
end
|
||||
|
||||
Sinatra.logger = Sinatra::Logger.new(STDOUT)
|
||||
|
||||
if Sinatra.config[:env] != :test
|
||||
Thread.new do
|
||||
while true
|
||||
Sinatra.logger.flush
|
||||
sleep(0.3)
|
||||
end
|
||||
end
|
||||
end
|
||||
Sinatra.logger = Sinatra::Logger.new("#{Sinatra.config[:env]}.log")
|
||||
|
||||
Sinatra.setup_default_events!
|
||||
|
||||
|
|
|
@ -3,10 +3,9 @@ require File.dirname(__FILE__) + '/helper'
|
|||
context "Dispatching" do
|
||||
|
||||
setup do
|
||||
Sinatra.config = nil
|
||||
Sinatra.routes.clear
|
||||
Sinatra.setup_default_events!
|
||||
Sinatra.reset!
|
||||
end
|
||||
|
||||
|
||||
specify "should return the correct block" do
|
||||
r = get '/' do
|
||||
|
|
|
@ -3,9 +3,7 @@ require File.dirname(__FILE__) + '/helper'
|
|||
context "Defining Errors" do
|
||||
|
||||
setup do
|
||||
Sinatra.routes.clear
|
||||
Sinatra.config = nil
|
||||
Sinatra.setup_default_events!
|
||||
Sinatra.reset!
|
||||
end
|
||||
|
||||
specify "should raise error if no block given" do
|
||||
|
|
|
@ -12,9 +12,7 @@ end
|
|||
context "Filters" do
|
||||
|
||||
setup do
|
||||
Sinatra.filters.clear
|
||||
Sinatra.routes.clear
|
||||
Sinatra.config = nil
|
||||
Sinatra.reset!
|
||||
end
|
||||
|
||||
specify "halts when told" do
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
require File.dirname(__FILE__) + '/../lib/sinatra'
|
||||
require File.dirname(__FILE__) + '/../lib/sinatra/test/spec'
|
||||
require 'mocha'
|
||||
|
||||
# class Test::Spec::TestCase
|
||||
# alias :run_without_reset :run
|
||||
# end
|
||||
|
|
|
@ -2,6 +2,10 @@ require File.dirname(__FILE__) + '/helper'
|
|||
|
||||
context "Helpers" do
|
||||
|
||||
setup do
|
||||
Sinatra.reset!
|
||||
end
|
||||
|
||||
specify "for event context" do
|
||||
|
||||
helpers do
|
||||
|
@ -15,7 +19,7 @@ context "Helpers" do
|
|||
end
|
||||
|
||||
get_it '/'
|
||||
|
||||
|
||||
should.be.ok
|
||||
body.should.equal 'foo'
|
||||
|
||||
|
|
|
@ -3,10 +3,7 @@ require File.dirname(__FILE__) + '/helper'
|
|||
context "Static" do
|
||||
|
||||
setup do
|
||||
Sinatra.routes.clear
|
||||
Sinatra.config = nil
|
||||
Sinatra.setup_default_events!
|
||||
|
||||
Sinatra.reset!
|
||||
Sinatra.config[:root] = File.dirname(__FILE__)
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue