1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Remove 0.9.x compatibility specs :)

This commit is contained in:
Ryan Tomayko 2009-06-05 21:53:27 -07:00
parent 9de67b15c6
commit f4b11b967f
36 changed files with 1 additions and 1731 deletions

View file

@ -2,35 +2,16 @@ require 'rake/clean'
require 'rake/testtask'
require 'fileutils'
task :default => [:test, :compat]
task :default => :test
task :spec => :test
# SPECS ===============================================================
task(:test) { puts "==> Running main test suite" }
Rake::TestTask.new(:test) do |t|
t.test_files = FileList['test/*_test.rb']
t.ruby_opts = ['-rubygems'] if defined? Gem
end
desc "Run < 0.9.x compatibility specs"
task :compat do
begin
require 'mocha'
require 'test/spec'
at_exit { exit 0 } # disable test-spec at_exit runner
puts "==> Running compat test suite"
Rake::TestTask.new(:compat) do |t|
t.test_files = FileList['compat/*_test.rb']
t.ruby_opts = ['-rubygems'] if defined? Gem
end
rescue LoadError
warn 'Skipping compat tests. mocha and/or test-spec gems not installed.'
end
end
# PACKAGING ============================================================
# Load the gemspec using the same limitations as github

View file

@ -1,282 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Sinatra" do
setup do
Sinatra.application = nil
end
specify "should put all DSL methods on (main)" do
object = Object.new
methods = %w[get put post head delete configure template helpers set]
methods.each do |method|
object.private_methods.map { |m| m.to_sym }.should.include(method.to_sym)
end
end
specify "should handle result of nil" do
get '/' do
nil
end
get_it '/'
should.be.ok
body.should == ''
end
specify "handles events" do
get '/:name' do
'Hello ' + params["name"]
end
get_it '/Blake'
should.be.ok
body.should.equal 'Hello Blake'
end
specify "handles splats" do
get '/hi/*' do
params["splat"].kind_of?(Array).should.equal true
params["splat"].first
end
get_it '/hi/Blake'
should.be.ok
body.should.equal 'Blake'
end
specify "handles multiple splats" do
get '/say/*/to/*' do
params["splat"].join(' ')
end
get_it '/say/hello/to/world'
should.be.ok
body.should.equal 'hello world'
end
specify "allow empty splats" do
get '/say/*/to*/*' do
params["splat"].join(' ')
end
get_it '/say/hello/to/world'
should.be.ok
body.should.equal 'hello world' # second splat is empty
get_it '/say/hello/tomy/world'
should.be.ok
body.should.equal 'hello my world'
end
specify "gives access to underlying response header Hash" do
get '/' do
header['X-Test'] = 'Is this thing on?'
headers 'X-Test2' => 'Foo', 'X-Test3' => 'Bar'
''
end
get_it '/'
should.be.ok
headers.should.include 'X-Test'
headers['X-Test'].should.equal 'Is this thing on?'
headers.should.include 'X-Test3'
headers['X-Test3'].should.equal 'Bar'
end
specify "follows redirects" do
get '/' do
redirect '/blake'
end
get '/blake' do
'Mizerany'
end
get_it '/'
should.be.redirection
body.should.equal ''
follow!
should.be.ok
body.should.equal 'Mizerany'
end
specify "renders a body with a redirect" do
helpers do
def foo ; 'blah' ; end
end
get "/" do
redirect 'foo', :foo
end
get_it '/'
should.be.redirection
headers['Location'].should.equal 'foo'
body.should.equal 'blah'
end
specify "redirects permanently with 301 status code" do
get "/" do
redirect 'foo', 301
end
get_it '/'
should.be.redirection
headers['Location'].should.equal 'foo'
status.should.equal 301
body.should.be.empty
end
specify "stop sets content and ends event" do
get '/set_body' do
stop 'Hello!'
stop 'World!'
fail 'stop should have halted'
end
get_it '/set_body'
should.be.ok
body.should.equal 'Hello!'
end
specify "should easily set response Content-Type" do
get '/foo.html' do
content_type 'text/html', :charset => 'utf-8'
"<h1>Hello, World</h1>"
end
get_it '/foo.html'
should.be.ok
headers['Content-Type'].should.equal 'text/html;charset=utf-8'
body.should.equal '<h1>Hello, World</h1>'
get '/foo_test.xml' do
content_type :xml
"<feed></feed>"
end
get_it '/foo_test.xml'
should.be.ok
headers['Content-Type'].should.equal 'application/xml'
body.should.equal '<feed></feed>'
end
specify "supports conditional GETs with last_modified" do
modified_at = Time.now
get '/maybe' do
last_modified modified_at
'response body, maybe'
end
get_it '/maybe'
should.be.ok
body.should.equal 'response body, maybe'
get_it '/maybe', :env => { 'HTTP_IF_MODIFIED_SINCE' => modified_at.httpdate }
status.should.equal 304
body.should.equal ''
end
specify "supports conditional GETs with entity_tag" do
get '/strong' do
entity_tag 'FOO'
'foo response'
end
get_it '/strong'
should.be.ok
body.should.equal 'foo response'
get_it '/strong', {},
'HTTP_IF_NONE_MATCH' => '"BAR"'
should.be.ok
body.should.equal 'foo response'
get_it '/strong', {},
'HTTP_IF_NONE_MATCH' => '"FOO"'
status.should.equal 304
body.should.equal ''
get_it '/strong', {},
'HTTP_IF_NONE_MATCH' => '"BAR", *'
status.should.equal 304
body.should.equal ''
end
specify "delegates HEAD requests to GET handlers" do
get '/invisible' do
"I am invisible to the world"
end
head_it '/invisible'
should.be.ok
body.should.not.equal "I am invisible to the world"
body.should.equal ''
end
specify "supports PUT" do
put '/' do
'puted'
end
put_it '/'
assert_equal 'puted', body
end
specify "rewrites POSTs with _method param to PUT" do
put '/' do
'puted'
end
post_it '/', :_method => 'PUT'
assert_equal 'puted', body
end
specify "rewrites POSTs with lowercase _method param to PUT" do
put '/' do
'puted'
end
post_it '/', :_method => 'put'
body.should.equal 'puted'
end
specify "does not rewrite GETs with _method param to PUT" do
get '/' do
'getted'
end
get_it '/', :_method => 'put'
should.be.ok
body.should.equal 'getted'
end
specify "ignores _method query string parameter on non-POST requests" do
post '/' do
'posted'
end
put '/' do
'booo'
end
post_it "/?_method=PUT"
should.be.ok
body.should.equal 'posted'
end
specify "does not read body if content type is not url encoded" do
post '/foo.xml' do
request.env['CONTENT_TYPE'].should.be == 'application/xml'
request.content_type.should.be == 'application/xml'
request.body.read
end
post_it '/foo.xml', '<foo></foo>', :content_type => 'application/xml'
@response.should.be.ok
@response.body.should.be == '<foo></foo>'
end
end

View file

@ -1,262 +0,0 @@
require File.dirname(__FILE__) + '/helper'
require 'uri'
class TesterWithEach
def each
yield 'foo'
yield 'bar'
yield 'baz'
end
end
context "An app returns" do
setup do
Sinatra.application = nil
end
specify "404 if no events found" do
request = Rack::MockRequest.new(@app)
get_it '/'
should.be.not_found
body.should.equal '<h1>Not Found</h1>'
end
specify "200 if success" do
get '/' do
'Hello World'
end
get_it '/'
should.be.ok
body.should.equal 'Hello World'
end
specify "an objects result from each if it has it" do
get '/' do
TesterWithEach.new
end
get_it '/'
should.be.ok
body.should.equal 'foobarbaz'
end
specify "404 if NotFound is raised" do
get '/' do
raise Sinatra::NotFound
end
get_it '/'
should.be.not_found
end
end
context "Application#configure blocks" do
setup do
Sinatra.application = nil
end
specify "run when no environment specified" do
ref = false
configure { ref = true }
ref.should.equal true
end
specify "run when matching environment specified" do
ref = false
configure(:test) { ref = true }
ref.should.equal true
end
specify "do not run when no matching environment specified" do
configure(:foo) { flunk "block should not have been executed" }
configure(:development, :production, :foo) { flunk "block should not have been executed" }
end
specify "accept multiple environments" do
ref = false
configure(:foo, :test, :bar) { ref = true }
ref.should.equal true
end
end
context "Events in an app" do
setup do
Sinatra.application = nil
end
specify "evaluate in a clean context" do
helpers do
def foo
'foo'
end
end
get '/foo' do
foo
end
get_it '/foo'
should.be.ok
body.should.equal 'foo'
end
specify "get access to request, response, and params" do
get '/:foo' do
params["foo"] + params["bar"]
end
get_it '/foo?bar=baz'
should.be.ok
body.should.equal 'foobaz'
end
specify "can filters by agent" do
get '/', :agent => /Windows/ do
request.env['HTTP_USER_AGENT']
end
get_it '/', :env => { :agent => 'Windows' }
should.be.ok
body.should.equal 'Windows'
get_it '/', :env => { :agent => 'Mac' }
should.not.be.ok
end
specify "can use regex to get parts of user-agent" do
get '/', :agent => /Windows (NT)/ do
params[:agent].first
end
get_it '/', :env => { :agent => 'Windows NT' }
body.should.equal 'NT'
end
specify "can deal with spaces in paths" do
path = '/path with spaces'
get path do
"Look ma, a path with spaces!"
end
get_it URI.encode(path)
body.should.equal "Look ma, a path with spaces!"
end
specify "route based on host" do
get '/' do
'asdf'
end
get_it '/'
assert ok?
assert_equal('asdf', body)
get '/foo', :host => 'foo.sinatrarb.com' do
'in foo!'
end
get '/foo', :host => 'bar.sinatrarb.com' do
'in bar!'
end
get_it '/foo', {}, 'HTTP_HOST' => 'foo.sinatrarb.com'
assert ok?
assert_equal 'in foo!', body
get_it '/foo', {}, 'HTTP_HOST' => 'bar.sinatrarb.com'
assert ok?
assert_equal 'in bar!', body
get_it '/foo'
assert not_found?
end
end
context "Options in an app" do
setup do
Sinatra.application = nil
@app = Sinatra::application
end
specify "can be set singly on app" do
@app.set :foo, 1234
@app.options.foo.should.equal 1234
end
specify "can be set singly from top-level" do
set_option :foo, 1234
@app.options.foo.should.equal 1234
end
specify "can be set multiply on app" do
@app.options.foo.should.be.nil
@app.set :foo => 1234,
:bar => 'hello, world'
@app.options.foo.should.equal 1234
@app.options.bar.should.equal 'hello, world'
end
specify "can be set multiply from top-level" do
@app.options.foo.should.be.nil
set_options :foo => 1234,
:bar => 'hello, world'
@app.options.foo.should.equal 1234
@app.options.bar.should.equal 'hello, world'
end
specify "can be enabled on app" do
@app.options.foo.should.be.nil
@app.enable :sessions, :foo, :bar
@app.options.sessions.should.equal true
@app.options.foo.should.equal true
@app.options.bar.should.equal true
end
specify "can be enabled from top-level" do
@app.options.foo.should.be.nil
enable :sessions, :foo, :bar
@app.options.sessions.should.equal true
@app.options.foo.should.equal true
@app.options.bar.should.equal true
end
specify "can be disabled on app" do
@app.options.foo.should.be.nil
@app.disable :sessions, :foo, :bar
@app.options.sessions.should.equal false
@app.options.foo.should.equal false
@app.options.bar.should.equal false
end
specify "can be enabled from top-level" do
@app.options.foo.should.be.nil
disable :sessions, :foo, :bar
@app.options.sessions.should.equal false
@app.options.foo.should.equal false
@app.options.bar.should.equal false
end
end

View file

@ -1,101 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Builder" do
setup do
Sinatra.application = nil
end
context "without layouts" do
setup do
Sinatra.application = nil
end
specify "should render" do
get '/no_layout' do
builder 'xml.instruct!'
end
get_it '/no_layout'
should.be.ok
body.should == %(<?xml version="1.0" encoding="UTF-8"?>\n)
end
specify "should render inline block" do
get '/no_layout_and_inlined' do
@name = "Frank & Mary"
builder do |xml|
xml.couple @name
end
end
get_it '/no_layout_and_inlined'
should.be.ok
body.should == %(<couple>Frank &amp; Mary</couple>\n)
end
end
context "Templates (in general)" do
setup do
Sinatra.application = nil
end
specify "are read from files if Symbols" do
get '/from_file' do
@name = 'Blue'
builder :foo, :views_directory => File.dirname(__FILE__) + "/views"
end
get_it '/from_file'
should.be.ok
body.should.equal %(<exclaim>You rock Blue!</exclaim>\n)
end
specify "use layout.ext by default if available" do
get '/' do
builder :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
end
get_it '/'
should.be.ok
body.should.equal "<layout>\n<this>is foo!</this>\n</layout>\n"
end
specify "renders without layout" do
get '/' do
builder :no_layout, :views_directory => File.dirname(__FILE__) + "/views/no_layout"
end
get_it '/'
should.be.ok
body.should.equal "<foo>No Layout!</foo>\n"
end
specify "raises error if template not found" do
get '/' do
builder :not_found
end
lambda { get_it '/' }.should.raise(Errno::ENOENT)
end
end
end

View file

@ -1,12 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Compat" do
setup do
Sinatra.application = nil
@app = Sinatra.application
end
specify "makes EventContext available" do
assert_same Sinatra::Default, Sinatra::EventContext
end
end

View file

@ -1,62 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Custom Errors" do
setup do
Sinatra.application = nil
end
specify "override the default 404" do
get_it '/'
should.be.not_found
body.should.equal '<h1>Not Found</h1>'
error Sinatra::NotFound do
'Custom 404'
end
get_it '/'
should.be.not_found
body.should.equal 'Custom 404'
end
specify "override the default 500" do
Sinatra.application.options.raise_errors = false
get '/' do
raise 'asdf'
end
get_it '/'
status.should.equal 500
body.should.equal '<h1>Internal Server Error</h1>'
error do
'Custom 500 for ' + request.env['sinatra.error'].message
end
get_it '/'
get_it '/'
status.should.equal 500
body.should.equal 'Custom 500 for asdf'
Sinatra.application.options.raise_errors = true
end
class UnmappedError < RuntimeError; end
specify "should bring unmapped error back to the top" do
get '/' do
raise UnmappedError, 'test'
end
assert_raises(UnmappedError) do
get_it '/'
end
end
end

View file

@ -1,136 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Erb" do
setup do
Sinatra.application = nil
end
context "without layouts" do
setup do
Sinatra.application = nil
end
specify "should render" do
get '/no_layout' do
erb '<%= 1 + 1 %>'
end
get_it '/no_layout'
should.be.ok
body.should == '2'
end
specify "should take an options hash with :locals set with a string" do
get '/locals' do
erb '<%= foo %>', :locals => {:foo => "Bar"}
end
get_it '/locals'
should.be.ok
body.should == 'Bar'
end
specify "should take an options hash with :locals set with a complex object" do
get '/locals-complex' do
erb '<%= foo[0] %>', :locals => {:foo => ["foo", "bar", "baz"]}
end
get_it '/locals-complex'
should.be.ok
body.should == 'foo'
end
end
context "with layouts" do
setup do
Sinatra.application = nil
end
specify "can be inline" do
layout do
%Q{This is <%= yield %>!}
end
get '/lay' do
erb 'Blake'
end
get_it '/lay'
should.be.ok
body.should.equal 'This is Blake!'
end
specify "can use named layouts" do
layout :pretty do
%Q{<h1><%= yield %></h1>}
end
get '/pretty' do
erb 'Foo', :layout => :pretty
end
get '/not_pretty' do
erb 'Bar'
end
get_it '/pretty'
body.should.equal '<h1>Foo</h1>'
get_it '/not_pretty'
body.should.equal 'Bar'
end
specify "can be read from a file if they're not inlined" do
get '/foo' do
@title = 'Welcome to the Hello Program'
erb 'Blake', :layout => :foo_layout,
:views_directory => File.dirname(__FILE__) + "/views"
end
get_it '/foo'
body.should.equal "Welcome to the Hello Program\nHi Blake\n"
end
end
context "Templates (in general)" do
specify "are read from files if Symbols" do
get '/from_file' do
@name = 'Alena'
erb :foo, :views_directory => File.dirname(__FILE__) + "/views"
end
get_it '/from_file'
body.should.equal 'You rock Alena!'
end
specify "use layout.ext by default if available" do
get '/layout_from_file' do
erb :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
end
get_it '/layout_from_file'
should.be.ok
body.should.equal "x This is foo! x \n"
end
end
end

View file

@ -1,78 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Simple Events" do
def simple_request_hash(method, path)
Rack::Request.new({
'REQUEST_METHOD' => method.to_s.upcase,
'PATH_INFO' => path
})
end
class MockResult < Struct.new(:block, :params)
end
def invoke_simple(path, request_path, &b)
params = nil
get path do
params = self.params
b.call if b
end
get_it request_path
MockResult.new(b, params)
end
setup { Sinatra.application = nil }
specify "return last value" do
block = Proc.new { 'Simple' }
result = invoke_simple('/', '/', &block)
result.should.not.be.nil
result.block.should.be block
result.params.should.equal Hash.new
end
specify "takes params in path" do
result = invoke_simple('/:foo/:bar', '/a/b')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => 'b'
# unscapes
Sinatra.application = nil
result = invoke_simple('/:foo/:bar', '/a/blake%20mizerany')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => 'blake mizerany'
end
specify "takes optional params in path" do
result = invoke_simple('/?:foo?/?:bar?', '/a/b')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => 'b'
Sinatra.application = nil
result = invoke_simple('/?:foo?/?:bar?', '/a/')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => nil
Sinatra.application = nil
result = invoke_simple('/?:foo?/?:bar?', '/a')
result.should.not.be.nil
result.params.should.equal "foo" => 'a', "bar" => nil
Sinatra.application = nil
result = invoke_simple('/:foo?/?:bar?', '/')
result.should.not.be.nil
result.params.should.equal "foo" => nil, "bar" => nil
end
specify "ignores to many /'s" do
result = invoke_simple('/x/y', '/x//y')
result.should.not.be.nil
end
specify "understands splat" do
invoke_simple('/foo/*', '/foo/bar').should.not.be.nil
invoke_simple('/foo/*', '/foo/bar/baz').should.not.be.nil
invoke_simple('/foo/*', '/foo/baz').should.not.be.nil
end
end

View file

@ -1,30 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "before filters" do
setup do
Sinatra.application = nil
@app = Sinatra.application
end
specify "should be executed in the order defined" do
invoked = 0x0
@app.before { invoked = 0x01 }
@app.before { invoked |= 0x02 }
@app.get('/') { 'Hello World' }
get_it '/'
should.be.ok
body.should.be == 'Hello World'
invoked.should.be == 0x03
end
specify "should be capable of modifying the request" do
@app.get('/foo') { 'foo' }
@app.get('/bar') { 'bar' }
@app.before { request.path_info = '/bar' }
get_it '/foo'
should.be.ok
body.should.be == 'bar'
end
end

View file

@ -1,236 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Haml" do
setup do
Sinatra.application = nil
end
context "without layouts" do
setup do
Sinatra.application = nil
end
specify "should render" do
get '/no_layout' do
haml '== #{1+1}'
end
get_it '/no_layout'
should.be.ok
body.should == "2\n"
end
end
context "with layouts" do
setup do
Sinatra.application = nil
end
specify "can be inline" do
layout do
'== This is #{yield}!'
end
get '/lay' do
haml 'Blake'
end
get_it '/lay'
should.be.ok
body.should.equal "This is Blake\n!\n"
end
specify "can use named layouts" do
layout :pretty do
'%h1== #{yield}'
end
get '/pretty' do
haml 'Foo', :layout => :pretty
end
get '/not_pretty' do
haml 'Bar'
end
get_it '/pretty'
body.should.equal "<h1>Foo</h1>\n"
get_it '/not_pretty'
body.should.equal "Bar\n"
end
specify "can be read from a file if they're not inlined" do
get '/foo' do
@title = 'Welcome to the Hello Program'
haml 'Blake', :layout => :foo_layout,
:views_directory => File.dirname(__FILE__) + "/views"
end
get_it '/foo'
body.should.equal "Welcome to the Hello Program\nHi Blake\n"
end
specify "can be read from file and layout from text" do
get '/foo' do
haml 'Test', :layout => '== Foo #{yield}'
end
get_it '/foo'
body.should.equal "Foo Test\n"
end
end
context "Templates (in general)" do
setup do
Sinatra.application = nil
end
specify "are read from files if Symbols" do
get '/from_file' do
@name = 'Alena'
haml :foo, :views_directory => File.dirname(__FILE__) + "/views"
end
get_it '/from_file'
body.should.equal "You rock Alena!\n"
end
specify "use layout.ext by default if available" do
get '/' do
haml :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
end
get_it '/'
should.be.ok
body.should.equal "x This is foo!\n x\n"
end
specify "renders without layout" do
get '/' do
haml :no_layout, :views_directory => File.dirname(__FILE__) + "/views/no_layout"
end
get_it '/'
should.be.ok
body.should.equal "<h1>No Layout!</h1>\n"
end
specify "can render with no layout" do
layout do
"X\n= yield\nX"
end
get '/' do
haml 'blake', :layout => false
end
get_it '/'
body.should.equal "blake\n"
end
specify "raises error if template not found" do
get '/' do
haml :not_found
end
lambda { get_it '/' }.should.raise(Errno::ENOENT)
end
specify "use layout.ext by default if available" do
template :foo do
'asdf'
end
get '/' do
haml :foo, :layout => false,
:views_directory => File.dirname(__FILE__) + "/views/layout_test"
end
get_it '/'
should.be.ok
body.should.equal "asdf\n"
end
end
describe 'Options passed to the HAML interpreter' do
setup do
Sinatra.application = nil
end
specify 'default to filename and line of caller' do
get '/' do
haml 'foo'
end
Haml::Engine.expects(:new).with('foo', {:filename => __FILE__,
:line => (__LINE__-4)}).returns(stub(:render => 'foo'))
get_it '/'
should.be.ok
end
specify 'can be configured by passing :options to haml' do
get '/' do
haml 'foo', :options => {:format => :html4}
end
Haml::Engine.expects(:new).with('foo', {:filename => __FILE__,
:line => (__LINE__-4), :format => :html4}).returns(stub(:render => 'foo'))
get_it '/'
should.be.ok
end
specify 'can be configured using set_option :haml' do
configure do
set_option :haml, :format => :html4,
:escape_html => true
end
get '/' do
haml 'foo'
end
Haml::Engine.expects(:new).with('foo', {:filename => __FILE__,
:line => (__LINE__-4), :format => :html4,
:escape_html => true}).returns(stub(:render => 'foo'))
get_it '/'
should.be.ok
end
end
end

View file

@ -1,33 +0,0 @@
require 'rubygems'
require 'mocha'
# disable warnings in compat specs.
$VERBOSE = nil
$:.unshift File.dirname(File.dirname(__FILE__)) + "/lib"
ENV['RACK_ENV'] ||= 'test'
require 'sinatra'
require 'sinatra/test'
require 'sinatra/test/unit'
require 'sinatra/test/spec'
module Sinatra::Test
# we need to remove the new test helper methods since they conflict with
# the top-level methods of the same name.
%w(get head post put delete).each do |verb|
remove_method verb
end
include Sinatra::Delegator
end
class Test::Unit::TestCase
include Sinatra::Test
PASSTHROUGH_EXCEPTIONS = [] unless const_defined?(:PASSTHROUGH_EXCEPTIONS)
def setup
@app = lambda { |env| Sinatra::Application.call(env) }
end
end

View file

@ -1,72 +0,0 @@
require File.dirname(__FILE__) + '/helper'
class FooError < RuntimeError; end
context "Mapped errors" do
setup do
Sinatra.application = nil
Sinatra.application.options.raise_errors = false
end
specify "are rescued and run in context" do
error FooError do
'MAPPED ERROR!'
end
get '/' do
raise FooError
end
get_it '/'
should.be.server_error
body.should.equal 'MAPPED ERROR!'
end
specify "renders empty if no each method on result" do
error FooError do
nil
end
get '/' do
raise FooError
end
get_it '/'
should.be.server_error
body.should.be.empty
end
specify "doesn't override status if set" do
error FooError do
status(200)
end
get '/' do
raise FooError
end
get_it '/'
should.be.ok
end
specify "raises errors when the raise_errors option is set" do
Sinatra.application.options.raise_errors = true
error FooError do
end
get '/' do
raise FooError
end
assert_raises(FooError) { get_it('/') }
end
end

View file

@ -1,45 +0,0 @@
require File.dirname(__FILE__) + '/helper'
class UpcaseMiddleware
def initialize(app, *args, &block)
@app = app
@args = args
@block = block
end
def call(env)
env['PATH_INFO'] = env['PATH_INFO'].to_s.upcase
@app.call(env)
end
end
context "Middleware Pipelines" do
setup do
Sinatra.application = nil
@app = Sinatra.application
end
teardown do
Sinatra.application = nil
end
specify "should add middleware with use" do
block = Proc.new { |env| }
@app.use UpcaseMiddleware
@app.use UpcaseMiddleware, "foo", "bar"
@app.use UpcaseMiddleware, "foo", "bar", &block
@app.send(:middleware).should.include([UpcaseMiddleware, [], nil])
@app.send(:middleware).should.include([UpcaseMiddleware, ["foo", "bar"], nil])
@app.send(:middleware).should.include([UpcaseMiddleware, ["foo", "bar"], block])
end
specify "should run middleware added with use" do
get('/foo') { "FAIL!" }
get('/FOO') { "PASS!" }
use UpcaseMiddleware
get_it '/foo'
should.be.ok
body.should.equal "PASS!"
end
end

View file

@ -1 +0,0 @@
<foo></foo>

View file

@ -1,67 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Sass" do
setup do
Sinatra.application = nil
end
context "Templates (in general)" do
setup do
Sinatra.application = nil
end
specify "are read from files if Symbols" do
get '/from_file' do
sass :foo, :views_directory => File.dirname(__FILE__) + "/views"
end
get_it '/from_file'
should.be.ok
body.should.equal "#sass {\n background_color: #FFF; }\n"
end
specify "raise an error if template not found" do
get '/' do
sass :not_found
end
lambda { get_it '/' }.should.raise(Errno::ENOENT)
end
specify "ignore default layout file with .sass extension" do
get '/' do
sass :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
end
get_it '/'
should.be.ok
body.should.equal "#sass {\n background_color: #FFF; }\n"
end
specify "ignore explicitly specified layout file" do
get '/' do
sass :foo, :layout => :layout, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
end
get_it '/'
should.be.ok
body.should.equal "#sass {\n background_color: #FFF; }\n"
end
it "passes :sass option to the Sass engine" do
get '/' do
sass "#sass\n :background-color #FFF\n :color #000\n", :sass => {:style => :compact}
end
get_it '/'
should.be.ok
body.should.equal "#sass { background-color: #FFF; color: #000; }\n"
end
end
end

View file

@ -1,42 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Sessions" do
setup { Sinatra.application = nil }
specify "should be off by default" do
get '/asdf' do
session[:test] = true
"asdf"
end
get '/test' do
session[:test] == true ? "true" : "false"
end
get_it '/asdf', {}, 'HTTP_HOST' => 'foo.sinatrarb.com'
assert ok?
assert !include?('Set-Cookie')
end
specify "should be able to store data accross requests" do
set_option :sessions, true
set_option :environment, :not_test # necessary because sessions are disabled
get '/foo' do
session[:test] = true
"asdf"
end
get '/bar' do
session[:test] == true ? "true" : "false"
end
get_it '/foo', :env => { :host => 'foo.sinatrarb.com' }
assert ok?
assert include?('Set-Cookie')
set_option :environment, :test
end
end

View file

@ -1,133 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Static files (by default)" do
setup do
Sinatra.application = nil
Sinatra.application.options.public = File.dirname(__FILE__) + '/public'
end
specify "are served from root/public" do
get_it '/foo.xml'
should.be.ok
headers['Content-Length'].should.equal '12'
headers['Content-Type'].should.equal 'application/xml'
body.should.equal "<foo></foo>\n"
end
specify "are not served when verb is not GET or HEAD" do
post_it '/foo.xml'
# these should actually be giving back a 405 Method Not Allowed but that
# complicates the routing logic quite a bit.
should.be.not_found
status.should.equal 404
end
specify "are served when verb is HEAD but missing a body" do
head_it '/foo.xml'
should.be.ok
headers['Content-Length'].should.equal '12'
headers['Content-Type'].should.equal 'application/xml'
body.should.equal ""
end
# static files override dynamic/internal events and ...
specify "are served when conflicting events exists" do
get '/foo.xml' do
'this is not foo.xml!'
end
get_it '/foo.xml'
should.be.ok
body.should.equal "<foo></foo>\n"
end
specify "are irrelevant when request_method is not GET/HEAD" do
put '/foo.xml' do
'putted!'
end
put_it '/foo.xml'
should.be.ok
body.should.equal 'putted!'
get_it '/foo.xml'
should.be.ok
body.should.equal "<foo></foo>\n"
end
specify "include a Last-Modified header" do
last_modified = File.mtime(Sinatra.application.options.public + '/foo.xml')
get_it('/foo.xml')
should.be.ok
body.should.not.be.empty
headers['Last-Modified'].should.equal last_modified.httpdate
end
# Deprecated. Use: ConditionalGet middleware.
specify "are not served when If-Modified-Since matches" do
last_modified = File.mtime(Sinatra.application.options.public + '/foo.xml')
@request = Rack::MockRequest.new(Sinatra.application)
@response = @request.get('/foo.xml', 'HTTP_IF_MODIFIED_SINCE' => last_modified.httpdate)
status.should.equal 304
body.should.be.empty
end
specify "should omit Content-Disposition headers" do
get_it('/foo.xml')
should.be.ok
headers['Content-Disposition'].should.be.nil
headers['Content-Transfer-Encoding'].should.be.nil
end
specify "should be served even if their path is url escaped" do
get_it('/fo%6f.xml')
should.be.ok
body.should.equal "<foo></foo>\n"
end
end
context "SendData" do
setup do
Sinatra.application = nil
end
# Deprecated. send_data is going away.
specify "should send the data with options" do
get '/' do
send_data 'asdf', :status => 500
end
get_it '/'
should.be.server_error
body.should.equal 'asdf'
end
# Deprecated. The Content-Disposition is no longer handled by sendfile.
specify "should include a Content-Disposition header" do
get '/' do
send_file File.dirname(__FILE__) + '/public/foo.xml',
:disposition => 'attachment'
end
get_it '/'
should.be.ok
headers['Content-Disposition'].should.not.be.nil
headers['Content-Disposition'].should.equal 'attachment; filename="foo.xml"'
end
specify "should include a Content-Disposition header when :disposition set to attachment" do
get '/' do
send_file File.dirname(__FILE__) + '/public/foo.xml',
:disposition => 'attachment'
end
get_it '/'
should.be.ok
headers['Content-Disposition'].should.not.be.nil
headers['Content-Disposition'].should.equal 'attachment; filename="foo.xml"'
end
end

View file

@ -1,19 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Symbol Params" do
setup do
Sinatra.application = nil
end
specify "should be accessable as Strings or Symbols" do
get '/' do
params[:foo] + params['foo']
end
get_it '/', :foo => "X"
assert_equal('XX', body)
end
end

View file

@ -1,30 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Templates" do
specify "are read from files if Symbols" do
get '/from_file' do
@name = 'Alena'
erb :foo, :views_directory => File.dirname(__FILE__) + "/views"
end
get_it '/from_file'
body.should.equal 'You rock Alena!'
end
specify "use layout.ext by default if available" do
get '/layout_from_file' do
erb :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
end
get_it '/layout_from_file'
should.be.ok
body.should.equal "x This is foo! x \n"
end
end

View file

@ -1,47 +0,0 @@
require File.dirname(__FILE__) + '/helper'
context "Rendering in file templates" do
setup do
Sinatra.application = nil
use_in_file_templates!
end
specify "should set template" do
assert Sinatra.application.templates[:foo]
end
specify "should set layout" do
assert Sinatra.application.templates[:layout]
end
specify "should render without layout if specified" do
get '/' do
haml :foo, :layout => false
end
get_it '/'
assert_equal "this is foo\n", body
end
specify "should render with layout if specified" do
get '/' do
haml :foo
end
get_it '/'
assert_equal "X\nthis is foo\nX\n", body
end
end
__END__
@@ foo
this is foo
@@ layout
X
= yield
X

View file

@ -1 +0,0 @@
xml.exclaim "You rock #{@name}!"

View file

@ -1 +0,0 @@
You rock <%= @name %>!

View file

@ -1 +0,0 @@
== You rock #{@name}!

View file

@ -1,2 +0,0 @@
#sass
:background_color #FFF

View file

@ -1,2 +0,0 @@
<%= @title %>
Hi <%= yield %>

View file

@ -1,2 +0,0 @@
== #{@title}
== Hi #{yield}

View file

@ -1 +0,0 @@
xml.this "is foo!"

View file

@ -1 +0,0 @@
This is foo!

View file

@ -1 +0,0 @@
This is foo!

View file

@ -1,2 +0,0 @@
#sass
:background_color #FFF

View file

@ -1,3 +0,0 @@
xml.layout do
xml << yield
end

View file

@ -1 +0,0 @@
x <%= yield %> x

View file

@ -1 +0,0 @@
== x #{yield} x

View file

@ -1,2 +0,0 @@
b0rked!
= yield

View file

@ -1 +0,0 @@
xml.foo "No Layout!"

View file

@ -1 +0,0 @@
%h1 No Layout!