darkhelmet-sinatra 0.9.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/AUTHORS +41 -0
  2. data/CHANGES +243 -0
  3. data/LICENSE +22 -0
  4. data/README.rdoc +535 -0
  5. data/Rakefile +136 -0
  6. data/compat/app_test.rb +301 -0
  7. data/compat/application_test.rb +334 -0
  8. data/compat/builder_test.rb +101 -0
  9. data/compat/compat_test.rb +12 -0
  10. data/compat/custom_error_test.rb +62 -0
  11. data/compat/erb_test.rb +136 -0
  12. data/compat/events_test.rb +78 -0
  13. data/compat/filter_test.rb +30 -0
  14. data/compat/haml_test.rb +233 -0
  15. data/compat/helper.rb +30 -0
  16. data/compat/mapped_error_test.rb +72 -0
  17. data/compat/pipeline_test.rb +71 -0
  18. data/compat/public/foo.xml +1 -0
  19. data/compat/sass_test.rb +57 -0
  20. data/compat/sessions_test.rb +39 -0
  21. data/compat/streaming_test.rb +133 -0
  22. data/compat/sym_params_test.rb +19 -0
  23. data/compat/template_test.rb +30 -0
  24. data/compat/use_in_file_templates_test.rb +47 -0
  25. data/compat/views/foo.builder +1 -0
  26. data/compat/views/foo.erb +1 -0
  27. data/compat/views/foo.haml +1 -0
  28. data/compat/views/foo.sass +2 -0
  29. data/compat/views/foo_layout.erb +2 -0
  30. data/compat/views/foo_layout.haml +2 -0
  31. data/compat/views/layout_test/foo.builder +1 -0
  32. data/compat/views/layout_test/foo.erb +1 -0
  33. data/compat/views/layout_test/foo.haml +1 -0
  34. data/compat/views/layout_test/foo.sass +2 -0
  35. data/compat/views/layout_test/layout.builder +3 -0
  36. data/compat/views/layout_test/layout.erb +1 -0
  37. data/compat/views/layout_test/layout.haml +1 -0
  38. data/compat/views/layout_test/layout.sass +2 -0
  39. data/compat/views/no_layout/no_layout.builder +1 -0
  40. data/compat/views/no_layout/no_layout.haml +1 -0
  41. data/lib/sinatra/base.rb +1007 -0
  42. data/lib/sinatra/compat.rb +252 -0
  43. data/lib/sinatra/images/404.png +0 -0
  44. data/lib/sinatra/images/500.png +0 -0
  45. data/lib/sinatra/main.rb +47 -0
  46. data/lib/sinatra/test/bacon.rb +19 -0
  47. data/lib/sinatra/test/rspec.rb +13 -0
  48. data/lib/sinatra/test/spec.rb +11 -0
  49. data/lib/sinatra/test/unit.rb +13 -0
  50. data/lib/sinatra/test.rb +121 -0
  51. data/lib/sinatra.rb +8 -0
  52. data/sinatra.gemspec +116 -0
  53. data/test/base_test.rb +112 -0
  54. data/test/builder_test.rb +64 -0
  55. data/test/data/reload_app_file.rb +3 -0
  56. data/test/erb_test.rb +81 -0
  57. data/test/extensions_test.rb +63 -0
  58. data/test/filter_test.rb +99 -0
  59. data/test/haml_test.rb +68 -0
  60. data/test/helper.rb +85 -0
  61. data/test/helpers_test.rb +467 -0
  62. data/test/mapped_error_test.rb +160 -0
  63. data/test/middleware_test.rb +60 -0
  64. data/test/options_test.rb +374 -0
  65. data/test/reload_test.rb +68 -0
  66. data/test/request_test.rb +18 -0
  67. data/test/response_test.rb +42 -0
  68. data/test/result_test.rb +98 -0
  69. data/test/routing_test.rb +712 -0
  70. data/test/sass_test.rb +36 -0
  71. data/test/server_test.rb +41 -0
  72. data/test/sinatra_test.rb +13 -0
  73. data/test/static_test.rb +65 -0
  74. data/test/templates_test.rb +88 -0
  75. data/test/test_test.rb +109 -0
  76. data/test/views/hello.builder +1 -0
  77. data/test/views/hello.erb +1 -0
  78. data/test/views/hello.haml +1 -0
  79. data/test/views/hello.sass +2 -0
  80. data/test/views/hello.test +1 -0
  81. data/test/views/layout2.builder +3 -0
  82. data/test/views/layout2.erb +2 -0
  83. data/test/views/layout2.haml +2 -0
  84. data/test/views/layout2.test +1 -0
  85. metadata +184 -0
@@ -0,0 +1,252 @@
1
+ # Sinatra 0.3.x compatibility module.
2
+ #
3
+ # The following code makes Sinatra 0.9.x compatible with Sinatra 0.3.x to
4
+ # ease the transition to the final 1.0 release. Everything defined in this
5
+ # file will be removed for the 1.0 release.
6
+
7
+ require 'ostruct'
8
+ require 'sinatra/base'
9
+ require 'sinatra/main'
10
+
11
+ # Like Kernel#warn but outputs the location that triggered the warning.
12
+ def sinatra_warn(*message) #:nodoc:
13
+ line = caller.
14
+ detect { |line| line !~ /(?:lib\/sinatra\/|__DELEGATE__)/ }.
15
+ sub(/:in .*/, '')
16
+ warn "#{line}: warning: #{message.join(' ')}"
17
+ end
18
+
19
+ # Rack now supports evented and swiftiplied mongrels through separate
20
+ # handler.
21
+ if ENV['SWIFT']
22
+ sinatra_warn 'the SWIFT environment variable is deprecated;',
23
+ 'use Rack::Handler::SwiftipliedMongrel instead.'
24
+ require 'swiftcore/swiftiplied_mongrel'
25
+ puts "Using Swiftiplied Mongrel"
26
+ elsif ENV['EVENT']
27
+ sinatra_warn 'the EVENT environment variable is deprecated;',
28
+ 'use Rack::Handler::EventedMongrel instead.'
29
+ require 'swiftcore/evented_mongrel'
30
+ puts "Using Evented Mongrel"
31
+ end
32
+
33
+ # Make Rack 0.9.0 backward compatibile with 0.4.0 mime types. This isn't
34
+ # technically a Sinatra issue but many Sinatra apps access the old
35
+ # MIME_TYPES constants due to Sinatra example code.
36
+ require 'rack/file'
37
+ module Rack #:nodoc:
38
+ class File #:nodoc:
39
+ def self.const_missing(const_name)
40
+ if const_name == :MIME_TYPES
41
+ hash = Hash.new { |hash,key| Rack::Mime::MIME_TYPES[".#{key}"] }
42
+ const_set :MIME_TYPES, hash
43
+ sinatra_warn 'Rack::File::MIME_TYPES is deprecated; use Rack::Mime instead.'
44
+ hash
45
+ else
46
+ super
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ module Sinatra
53
+ module Compat #:nodoc:
54
+ end
55
+
56
+ # Make Sinatra::EventContext an alias for Sinatra::Default to unbreak plugins.
57
+ def self.const_missing(const_name) #:nodoc:
58
+ if const_name == :EventContext
59
+ const_set :EventContext, Sinatra::Default
60
+ sinatra_warn 'Sinatra::EventContext is deprecated; use Sinatra::Default instead.'
61
+ Sinatra::Default
62
+ else
63
+ super
64
+ end
65
+ end
66
+
67
+ # The ServerError exception is deprecated. Any exception is considered an
68
+ # internal server error.
69
+ class ServerError < RuntimeError
70
+ def initialize(*args, &block)
71
+ sinatra_warn 'Sinatra::ServerError is deprecated;',
72
+ 'use another exception, error, or Kernel#fail instead.'
73
+ end
74
+ def code ; 500 ; end
75
+ end
76
+
77
+ class Default < Base
78
+ def self.const_missing(const_name) #:nodoc:
79
+ if const_name == :FORWARD_METHODS
80
+ sinatra_warn 'Sinatra::Application::FORWARD_METHODS is deprecated;',
81
+ 'use Sinatra::Delegator::METHODS instead.'
82
+ const_set :FORWARD_METHODS, Sinatra::Delegator::METHODS
83
+ Sinatra::Delegator::METHODS
84
+ else
85
+ super
86
+ end
87
+ end
88
+
89
+ # Deprecated. Use: response['Header-Name']
90
+ def headers(header=nil)
91
+ sinatra_warn "The 'headers' method is deprecated; use 'response' instead."
92
+ response.headers.merge!(header) if header
93
+ response.headers
94
+ end
95
+ alias :header :headers
96
+
97
+ # Deprecated. Use: halt
98
+ def stop(*args, &block)
99
+ sinatra_warn "The 'stop' method is deprecated; use 'halt' instead."
100
+ halt(*args, &block)
101
+ end
102
+
103
+ # Deprecated. Use: etag
104
+ def entity_tag(*args, &block)
105
+ sinatra_warn "The 'entity_tag' method is deprecated; use 'etag' instead."
106
+ etag(*args, &block)
107
+ end
108
+
109
+ # Deprecated. Use the #attachment helper and return the data as a String or
110
+ # Array.
111
+ def send_data(data, options={})
112
+ sinatra_warn "The 'send_data' method is deprecated. use attachment, status, content_type, etc. helpers instead."
113
+
114
+ status options[:status] if options[:status]
115
+ attachment options[:filename] if options[:disposition] == 'attachment'
116
+ content_type options[:type] if options[:type]
117
+ halt data
118
+ end
119
+
120
+ # Throwing halt with a Symbol and the to_result convention are
121
+ # deprecated. Override the invoke method to detect those types of return
122
+ # values.
123
+ def invoke(&block) #:nodoc:
124
+ res = super
125
+ case
126
+ when res.kind_of?(Symbol)
127
+ sinatra_warn "Invoking the :#{res} helper by returning a Symbol is deprecated;",
128
+ "call the helper directly instead."
129
+ @response.body = __send__(res)
130
+ when res.respond_to?(:to_result)
131
+ sinatra_warn "The to_result convention is deprecated."
132
+ @response.body = res.to_result(self)
133
+ end
134
+ res
135
+ end
136
+
137
+ def options #:nodoc:
138
+ Options.new(self.class)
139
+ end
140
+
141
+ class Options < Struct.new(:target) #:nodoc:
142
+ def method_missing(name, *args, &block)
143
+ if target.respond_to?(name)
144
+ target.__send__(name, *args, &block)
145
+ elsif args.empty? && name.to_s !~ /=$/
146
+ sinatra_warn 'accessing undefined options will raise a NameError in Sinatra 1.0'
147
+ nil
148
+ else
149
+ super
150
+ end
151
+ end
152
+ end
153
+
154
+ class << self
155
+ # Deprecated. Options are stored directly on the class object.
156
+ def options
157
+ sinatra_warn "The 'options' class method is deprecated; use 'self' instead."
158
+ Options.new(self)
159
+ end
160
+
161
+ # Deprecated. Use: configure
162
+ def configures(*args, &block)
163
+ sinatra_warn "The 'configures' method is deprecated; use 'configure' instead."
164
+ configure(*args, &block)
165
+ end
166
+
167
+ # Deprecated. Use: set
168
+ def default_options
169
+ sinatra_warn "Sinatra::Application.default_options is deprecated; use 'set' instead."
170
+ fake = lambda { |options| set(options) }
171
+ def fake.merge!(options) ; call(options) ; end
172
+ fake
173
+ end
174
+
175
+ # Deprecated. Use: set
176
+ def set_option(*args, &block)
177
+ sinatra_warn "The 'set_option' method is deprecated; use 'set' instead."
178
+ set(*args, &block)
179
+ end
180
+
181
+ def set_options(*args, &block)
182
+ sinatra_warn "The 'set_options' method is deprecated; use 'set' instead."
183
+ set(*args, &block)
184
+ end
185
+
186
+ # Deprecated. Use: set :environment, ENV
187
+ def env=(value)
188
+ sinatra_warn "The :env option is deprecated; use :environment instead."
189
+ set :environment, value
190
+ end
191
+
192
+ # Deprecated. Use: options.environment
193
+ def env
194
+ sinatra_warn "The :env option is deprecated; use :environment instead."
195
+ environment
196
+ end
197
+ end
198
+
199
+ # Deprecated. Missing messages are no longer delegated to @response.
200
+ def method_missing(name, *args, &b) #:nodoc:
201
+ if @response.respond_to?(name)
202
+ sinatra_warn "The '#{name}' method is deprecated; use 'response.#{name}' instead."
203
+ @response.send(name, *args, &b)
204
+ else
205
+ super
206
+ end
207
+ end
208
+ end
209
+
210
+ class << self
211
+ # Deprecated. Use: Sinatra::Application
212
+ def application
213
+ sinatra_warn "Sinatra.application is deprecated; use Sinatra::Application instead."
214
+ Sinatra::Application
215
+ end
216
+
217
+ # Deprecated. Use: Sinatra::Application.reset!
218
+ def application=(value)
219
+ raise ArgumentError unless value.nil?
220
+ sinatra_warn "Setting Sinatra.application to nil is deprecated; create a new instance instead."
221
+ Sinatra.class_eval do
222
+ remove_const :Application
223
+ const_set :Application, Class.new(Sinatra::Default)
224
+ end
225
+ end
226
+
227
+ def build_application
228
+ sinatra_warn "Sinatra.build_application is deprecated; use Sinatra::Application instead."
229
+ Sinatra::Application
230
+ end
231
+
232
+ def options
233
+ sinatra_warn "Sinatra.options is deprecated; use Sinatra::Application.option_name instead."
234
+ Sinatra::Application.options
235
+ end
236
+
237
+ def port
238
+ sinatra_warn "Sinatra.port is deprecated; use Sinatra::Application.port instead."
239
+ options.port
240
+ end
241
+
242
+ def host
243
+ sinatra_warn "Sinatra.host is deprecated; use Sinatra::Application.host instead."
244
+ options.host
245
+ end
246
+
247
+ def env
248
+ sinatra_warn "Sinatra.env is deprecated; use Sinatra::Application.environment instead."
249
+ options.environment
250
+ end
251
+ end
252
+ end
Binary file
Binary file
@@ -0,0 +1,47 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ class Default < Base
5
+
6
+ # we assume that the first file that requires 'sinatra' is the
7
+ # app_file. all other path related options are calculated based
8
+ # on this path by default.
9
+ set :app_file, lambda {
10
+ ignore = [
11
+ /lib\/sinatra.*\.rb$/, # all sinatra code
12
+ /\(.*\)/, # generated code
13
+ /custom_require\.rb$/ # rubygems require hacks
14
+ ]
15
+ path =
16
+ caller.map{ |line| line.split(/:\d/, 2).first }.find do |file|
17
+ next if ignore.any? { |pattern| file =~ pattern }
18
+ file
19
+ end
20
+ path || $0
21
+ }.call
22
+
23
+ set :run, Proc.new { $0 == app_file }
24
+
25
+ if run? && ARGV.any?
26
+ require 'optparse'
27
+ OptionParser.new { |op|
28
+ op.on('-x') { set :mutex, true }
29
+ op.on('-e env') { |val| set :environment, val.to_sym }
30
+ op.on('-s server') { |val| set :server, val }
31
+ op.on('-p port') { |val| set :port, val.to_i }
32
+ }.parse!(ARGV.dup)
33
+ end
34
+ end
35
+ end
36
+
37
+ include Sinatra::Delegator
38
+
39
+ def mime(ext, type)
40
+ ext = ".#{ext}" unless ext.to_s[0] == ?.
41
+ Rack::Mime::MIME_TYPES[ext.to_s] = type
42
+ end
43
+
44
+ at_exit do
45
+ raise $! if $!
46
+ Sinatra::Application.run! if Sinatra::Application.run?
47
+ end
@@ -0,0 +1,19 @@
1
+ require 'bacon'
2
+ require 'sinatra/test'
3
+
4
+ Sinatra::Test.deprecate('Bacon')
5
+
6
+ Sinatra::Default.set(
7
+ :environment => :test,
8
+ :run => false,
9
+ :raise_errors => true,
10
+ :logging => false
11
+ )
12
+
13
+ module Sinatra::Test
14
+ def should
15
+ @response.should
16
+ end
17
+ end
18
+
19
+ Bacon::Context.send(:include, Sinatra::Test)
@@ -0,0 +1,13 @@
1
+ require 'sinatra/test'
2
+ require 'sinatra/test/unit'
3
+ require 'spec'
4
+ require 'spec/interop/test'
5
+
6
+ Sinatra::Test.deprecate('RSpec')
7
+
8
+ Sinatra::Default.set(
9
+ :environment => :test,
10
+ :run => false,
11
+ :raise_errors => true,
12
+ :logging => false
13
+ )
@@ -0,0 +1,11 @@
1
+ require 'test/spec'
2
+ require 'sinatra/test'
3
+ require 'sinatra/test/unit'
4
+
5
+ Sinatra::Test.deprecate('test/spec')
6
+
7
+ module Sinatra::Test
8
+ def should
9
+ @response.should
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'sinatra/test'
2
+ require 'test/unit'
3
+
4
+ Sinatra::Test.deprecate('test/unit')
5
+
6
+ Test::Unit::TestCase.send :include, Sinatra::Test
7
+
8
+ Sinatra::Default.set(
9
+ :environment => :test,
10
+ :run => false,
11
+ :raise_errors => true,
12
+ :logging => false
13
+ )
@@ -0,0 +1,121 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Test
5
+ include Rack::Utils
6
+
7
+ attr_reader :app, :request, :response
8
+
9
+ def self.deprecate(framework)
10
+ warn <<-EOF
11
+ Warning: support for the #{framework} testing framework is deprecated and
12
+ will be dropped in Sinatra 1.0. See <http://sinatra.github.com/testing.html>
13
+ for more information.
14
+ EOF
15
+ end
16
+
17
+ def make_request(verb, path, data=nil, h=nil)
18
+ @app = Sinatra::Application if @app.nil? && defined?(Sinatra::Application)
19
+ fail "@app not set - cannot make request" if @app.nil?
20
+
21
+ @request = Rack::MockRequest.new(@app)
22
+ options = { :lint => true }
23
+
24
+ case data
25
+ when Hash
26
+ if env = data.delete(:env)
27
+ options = rack_options(env)
28
+ end
29
+ options.merge!(h) if h.is_a?(Hash)
30
+ options[:input] = param_string(data)
31
+ when String
32
+ options = rack_options(h) if h.is_a?(Hash)
33
+ options[:input] = data
34
+ end
35
+
36
+ # TODO: yield @request if block_given?
37
+ @response = @request.request(verb, path, options)
38
+ end
39
+
40
+ def get(path, *args, &b) ; make_request('GET', path, *args, &b) ; end
41
+ def head(path, *args, &b) ; make_request('HEAD', path, *args, &b) ; end
42
+ def post(path, *args, &b) ; make_request('POST', path, *args, &b) ; end
43
+ def put(path, *args, &b) ; make_request('PUT', path, *args, &b) ; end
44
+ def delete(path, *args, &b) ; make_request('DELETE', path, *args, &b) ; end
45
+
46
+ def follow!
47
+ make_request 'GET', @response.location
48
+ end
49
+
50
+ def body ; @response.body ; end
51
+ def status ; @response.status ; end
52
+
53
+ # Delegate other missing methods to @response.
54
+ def method_missing(name, *args, &block)
55
+ if @response && @response.respond_to?(name)
56
+ @response.send(name, *args, &block)
57
+ else
58
+ super
59
+ end
60
+ end
61
+
62
+ # Also check @response since we delegate there.
63
+ def respond_to?(symbol, include_private=false)
64
+ super || (@response && @response.respond_to?(symbol, include_private))
65
+ end
66
+
67
+ private
68
+
69
+ RACK_OPTIONS = {
70
+ :accept => "HTTP_ACCEPT",
71
+ :agent => "HTTP_USER_AGENT",
72
+ :host => "HTTP_HOST",
73
+ :session => "HTTP_COOKIE",
74
+ :cookies => "HTTP_COOKIE",
75
+ :content_type => "CONTENT_TYPE"
76
+ }
77
+
78
+ def rack_options(opts)
79
+ opts.merge(:lint => true).inject({}) do |hash,(key,val)|
80
+ key = RACK_OPTIONS[key] || key
81
+ hash[key] = val
82
+ hash
83
+ end
84
+ end
85
+
86
+ def param_string(value, prefix = nil)
87
+ case value
88
+ when Array
89
+ value.map { |v|
90
+ param_string(v, "#{prefix}[]")
91
+ } * "&"
92
+ when Hash
93
+ value.map { |k, v|
94
+ param_string(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
95
+ } * "&"
96
+ else
97
+ "#{prefix}=#{escape(value)}"
98
+ end
99
+ end
100
+
101
+ if defined? Sinatra::Compat
102
+ # Deprecated. Use: "get" instead of "get_it".
103
+ %w(get head post put delete).each do |verb|
104
+ eval <<-RUBY, binding, __FILE__, __LINE__
105
+ def #{verb}_it(*args, &block)
106
+ sinatra_warn "The #{verb}_it method is deprecated; use #{verb} instead."
107
+ make_request('#{verb.upcase}', *args, &block)
108
+ end
109
+ RUBY
110
+ end
111
+ end
112
+ end
113
+
114
+ class TestHarness
115
+ include Test
116
+
117
+ def initialize(app=nil)
118
+ @app = app || Sinatra::Application
119
+ end
120
+ end
121
+ end