sinatra-acd 1.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +5 -0
  3. data/AUTHORS +61 -0
  4. data/CHANGES +1293 -0
  5. data/Gemfile +76 -0
  6. data/LICENSE +23 -0
  7. data/README.de.md +2864 -0
  8. data/README.es.md +2786 -0
  9. data/README.fr.md +2924 -0
  10. data/README.hu.md +694 -0
  11. data/README.ja.md +2726 -0
  12. data/README.ko.md +2832 -0
  13. data/README.md +2980 -0
  14. data/README.pt-br.md +965 -0
  15. data/README.pt-pt.md +791 -0
  16. data/README.ru.md +2799 -0
  17. data/README.zh.md +2158 -0
  18. data/Rakefile +199 -0
  19. data/examples/chat.rb +61 -0
  20. data/examples/simple.rb +3 -0
  21. data/examples/stream.ru +26 -0
  22. data/lib/sinatra.rb +4 -0
  23. data/lib/sinatra/base.rb +2044 -0
  24. data/lib/sinatra/images/404.png +0 -0
  25. data/lib/sinatra/images/500.png +0 -0
  26. data/lib/sinatra/main.rb +34 -0
  27. data/lib/sinatra/show_exceptions.rb +345 -0
  28. data/lib/sinatra/version.rb +3 -0
  29. data/sinatra.gemspec +19 -0
  30. data/test/asciidoctor_test.rb +72 -0
  31. data/test/base_test.rb +171 -0
  32. data/test/builder_test.rb +91 -0
  33. data/test/coffee_test.rb +90 -0
  34. data/test/compile_test.rb +183 -0
  35. data/test/contest.rb +100 -0
  36. data/test/creole_test.rb +65 -0
  37. data/test/delegator_test.rb +160 -0
  38. data/test/encoding_test.rb +20 -0
  39. data/test/erb_test.rb +116 -0
  40. data/test/extensions_test.rb +98 -0
  41. data/test/filter_test.rb +487 -0
  42. data/test/haml_test.rb +109 -0
  43. data/test/helper.rb +131 -0
  44. data/test/helpers_test.rb +1917 -0
  45. data/test/integration/app.rb +79 -0
  46. data/test/integration_helper.rb +236 -0
  47. data/test/integration_test.rb +104 -0
  48. data/test/less_test.rb +69 -0
  49. data/test/liquid_test.rb +77 -0
  50. data/test/mapped_error_test.rb +285 -0
  51. data/test/markaby_test.rb +80 -0
  52. data/test/markdown_test.rb +82 -0
  53. data/test/mediawiki_test.rb +68 -0
  54. data/test/middleware_test.rb +68 -0
  55. data/test/nokogiri_test.rb +67 -0
  56. data/test/public/favicon.ico +0 -0
  57. data/test/rabl_test.rb +89 -0
  58. data/test/rack_test.rb +45 -0
  59. data/test/radius_test.rb +59 -0
  60. data/test/rdoc_test.rb +66 -0
  61. data/test/readme_test.rb +130 -0
  62. data/test/request_test.rb +97 -0
  63. data/test/response_test.rb +63 -0
  64. data/test/result_test.rb +76 -0
  65. data/test/route_added_hook_test.rb +59 -0
  66. data/test/routing_test.rb +1412 -0
  67. data/test/sass_test.rb +115 -0
  68. data/test/scss_test.rb +88 -0
  69. data/test/server_test.rb +48 -0
  70. data/test/settings_test.rb +582 -0
  71. data/test/sinatra_test.rb +12 -0
  72. data/test/slim_test.rb +102 -0
  73. data/test/static_test.rb +236 -0
  74. data/test/streaming_test.rb +149 -0
  75. data/test/stylus_test.rb +90 -0
  76. data/test/templates_test.rb +382 -0
  77. data/test/textile_test.rb +65 -0
  78. data/test/views/a/in_a.str +1 -0
  79. data/test/views/ascii.erb +2 -0
  80. data/test/views/b/in_b.str +1 -0
  81. data/test/views/calc.html.erb +1 -0
  82. data/test/views/error.builder +3 -0
  83. data/test/views/error.erb +3 -0
  84. data/test/views/error.haml +3 -0
  85. data/test/views/error.sass +2 -0
  86. data/test/views/explicitly_nested.str +1 -0
  87. data/test/views/foo/hello.test +1 -0
  88. data/test/views/hello.asciidoc +1 -0
  89. data/test/views/hello.builder +1 -0
  90. data/test/views/hello.coffee +1 -0
  91. data/test/views/hello.creole +1 -0
  92. data/test/views/hello.erb +1 -0
  93. data/test/views/hello.haml +1 -0
  94. data/test/views/hello.less +5 -0
  95. data/test/views/hello.liquid +1 -0
  96. data/test/views/hello.mab +1 -0
  97. data/test/views/hello.md +1 -0
  98. data/test/views/hello.mediawiki +1 -0
  99. data/test/views/hello.nokogiri +1 -0
  100. data/test/views/hello.rabl +2 -0
  101. data/test/views/hello.radius +1 -0
  102. data/test/views/hello.rdoc +1 -0
  103. data/test/views/hello.sass +2 -0
  104. data/test/views/hello.scss +3 -0
  105. data/test/views/hello.slim +1 -0
  106. data/test/views/hello.str +1 -0
  107. data/test/views/hello.styl +2 -0
  108. data/test/views/hello.test +1 -0
  109. data/test/views/hello.textile +1 -0
  110. data/test/views/hello.wlang +1 -0
  111. data/test/views/hello.yajl +1 -0
  112. data/test/views/layout2.builder +3 -0
  113. data/test/views/layout2.erb +2 -0
  114. data/test/views/layout2.haml +2 -0
  115. data/test/views/layout2.liquid +2 -0
  116. data/test/views/layout2.mab +2 -0
  117. data/test/views/layout2.nokogiri +3 -0
  118. data/test/views/layout2.rabl +3 -0
  119. data/test/views/layout2.radius +2 -0
  120. data/test/views/layout2.slim +3 -0
  121. data/test/views/layout2.str +2 -0
  122. data/test/views/layout2.test +1 -0
  123. data/test/views/layout2.wlang +2 -0
  124. data/test/views/nested.str +1 -0
  125. data/test/views/utf8.erb +2 -0
  126. data/test/wlang_test.rb +87 -0
  127. data/test/yajl_test.rb +86 -0
  128. metadata +280 -0
@@ -0,0 +1,115 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ begin
4
+ require 'sass'
5
+
6
+ class SassTest < Test::Unit::TestCase
7
+ def sass_app(options = {}, &block)
8
+ mock_app do
9
+ set :views, File.dirname(__FILE__) + '/views'
10
+ set options
11
+ get('/', &block)
12
+ end
13
+ get '/'
14
+ end
15
+
16
+ it 'renders inline Sass strings' do
17
+ sass_app { sass "#sass\n :background-color white\n" }
18
+ assert ok?
19
+ assert_equal "#sass {\n background-color: white; }\n", body
20
+ end
21
+
22
+ it 'defaults content type to css' do
23
+ sass_app { sass "#sass\n :background-color white\n" }
24
+ assert ok?
25
+ assert_equal "text/css;charset=utf-8", response['Content-Type']
26
+ end
27
+
28
+ it 'defaults allows setting content type per route' do
29
+ sass_app do
30
+ content_type :html
31
+ sass "#sass\n :background-color white\n"
32
+ end
33
+ assert ok?
34
+ assert_equal "text/html;charset=utf-8", response['Content-Type']
35
+ end
36
+
37
+ it 'defaults allows setting content type globally' do
38
+ sass_app(:sass => { :content_type => 'html' }) {
39
+ sass "#sass\n :background-color white\n"
40
+ }
41
+ assert ok?
42
+ assert_equal "text/html;charset=utf-8", response['Content-Type']
43
+ end
44
+
45
+ it 'renders .sass files in views path' do
46
+ sass_app { sass :hello }
47
+ assert ok?
48
+ assert_equal "#sass {\n background-color: white; }\n", body
49
+ end
50
+
51
+ it 'ignores the layout option' do
52
+ sass_app { sass :hello, :layout => :layout2 }
53
+ assert ok?
54
+ assert_equal "#sass {\n background-color: white; }\n", body
55
+ end
56
+
57
+ it "raises error if template not found" do
58
+ mock_app { get('/') { sass :no_such_template } }
59
+ assert_raise(Errno::ENOENT) { get('/') }
60
+ end
61
+
62
+ it "passes SASS options to the Sass engine" do
63
+ sass_app do
64
+ sass(
65
+ "#sass\n :background-color white\n :color black\n",
66
+ :style => :compact
67
+ )
68
+ end
69
+ assert ok?
70
+ assert_equal("#sass { background-color: white; color: black; }\n", body)
71
+ end
72
+
73
+ it "passes default SASS options to the Sass engine" do
74
+ mock_app do
75
+ set :sass, {:style => :compact} # default Sass style is :nested
76
+ get('/') { sass("#sass\n :background-color white\n :color black\n") }
77
+ end
78
+ get '/'
79
+ assert ok?
80
+ assert_equal "#sass { background-color: white; color: black; }\n", body
81
+ end
82
+
83
+ it "merges the default SASS options with the overrides" do
84
+ mock_app do
85
+ # default Sass attribute_syntax is :normal (with : in front)
86
+ set :sass, {:style => :compact, :attribute_syntax => :alternate }
87
+ get('/') { sass("#sass\n background-color: white\n color: black\n") }
88
+ get('/raised') do
89
+ # retains global attribute_syntax settings
90
+ sass(
91
+ "#sass\n :background-color white\n :color black\n",
92
+ :style => :expanded
93
+ )
94
+ end
95
+ get('/expanded_normal') do
96
+ sass(
97
+ "#sass\n :background-color white\n :color black\n",
98
+ :style => :expanded, :attribute_syntax => :normal
99
+ )
100
+ end
101
+ end
102
+ get '/'
103
+ assert ok?
104
+ assert_equal "#sass { background-color: white; color: black; }\n", body
105
+ assert_raise(Sass::SyntaxError) { get('/raised') }
106
+ get '/expanded_normal'
107
+ assert ok?
108
+ assert_equal "#sass {\n background-color: white;\n color: black;\n}\n",
109
+ body
110
+ end
111
+ end
112
+
113
+ rescue LoadError
114
+ warn "#{$!.to_s}: skipping sass tests"
115
+ end
@@ -0,0 +1,88 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ begin
4
+ require 'sass'
5
+
6
+ class ScssTest < Test::Unit::TestCase
7
+ def scss_app(options = {}, &block)
8
+ mock_app do
9
+ set :views, File.dirname(__FILE__) + '/views'
10
+ set options
11
+ get('/', &block)
12
+ end
13
+ get '/'
14
+ end
15
+
16
+ it 'renders inline Scss strings' do
17
+ scss_app { scss "#scss {\n background-color: white; }\n" }
18
+ assert ok?
19
+ assert_equal "#scss {\n background-color: white; }\n", body
20
+ end
21
+
22
+ it 'defaults content type to css' do
23
+ scss_app { scss "#scss {\n background-color: white; }\n" }
24
+ assert ok?
25
+ assert_equal "text/css;charset=utf-8", response['Content-Type']
26
+ end
27
+
28
+ it 'defaults allows setting content type per route' do
29
+ scss_app do
30
+ content_type :html
31
+ scss "#scss {\n background-color: white; }\n"
32
+ end
33
+ assert ok?
34
+ assert_equal "text/html;charset=utf-8", response['Content-Type']
35
+ end
36
+
37
+ it 'defaults allows setting content type globally' do
38
+ scss_app(:scss => { :content_type => 'html' }) {
39
+ scss "#scss {\n background-color: white; }\n"
40
+ }
41
+ assert ok?
42
+ assert_equal "text/html;charset=utf-8", response['Content-Type']
43
+ end
44
+
45
+ it 'renders .scss files in views path' do
46
+ scss_app { scss :hello }
47
+ assert ok?
48
+ assert_equal "#scss {\n background-color: white; }\n", body
49
+ end
50
+
51
+ it 'ignores the layout option' do
52
+ scss_app { scss :hello, :layout => :layout2 }
53
+ assert ok?
54
+ assert_equal "#scss {\n background-color: white; }\n", body
55
+ end
56
+
57
+ it "raises error if template not found" do
58
+ mock_app { get('/') { scss(:no_such_template) } }
59
+ assert_raise(Errno::ENOENT) { get('/') }
60
+ end
61
+
62
+ it "passes scss options to the scss engine" do
63
+ scss_app do
64
+ scss(
65
+ "#scss {\n background-color: white;\n color: black\n}",
66
+ :style => :compact
67
+ )
68
+ end
69
+ assert ok?
70
+ assert_equal "#scss { background-color: white; color: black; }\n", body
71
+ end
72
+
73
+ it "passes default scss options to the scss engine" do
74
+ mock_app do
75
+ set :scss, {:style => :compact} # default scss style is :nested
76
+ get('/') {
77
+ scss("#scss {\n background-color: white;\n color: black;\n}")
78
+ }
79
+ end
80
+ get '/'
81
+ assert ok?
82
+ assert_equal "#scss { background-color: white; color: black; }\n", body
83
+ end
84
+ end
85
+
86
+ rescue LoadError
87
+ warn "#{$!.to_s}: skipping scss tests"
88
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+ require 'stringio'
3
+
4
+ module Rack::Handler
5
+ class Mock
6
+ extend Test::Unit::Assertions
7
+
8
+ def self.run(app, options={})
9
+ assert(app < Sinatra::Base)
10
+ assert_equal 9001, options[:Port]
11
+ assert_equal 'foo.local', options[:Host]
12
+ yield new
13
+ end
14
+
15
+ def stop
16
+ end
17
+ end
18
+
19
+ register 'mock', 'Rack::Handler::Mock'
20
+ end
21
+
22
+ class ServerTest < Test::Unit::TestCase
23
+ setup do
24
+ mock_app do
25
+ set :server, 'mock'
26
+ set :bind, 'foo.local'
27
+ set :port, 9001
28
+ end
29
+ $stderr = StringIO.new
30
+ end
31
+
32
+ def teardown
33
+ $stderr = STDERR
34
+ end
35
+
36
+ it "locates the appropriate Rack handler and calls ::run" do
37
+ @app.run!
38
+ end
39
+
40
+ it "sets options on the app before running" do
41
+ @app.run! :sessions => true
42
+ assert @app.sessions?
43
+ end
44
+
45
+ it "falls back on the next server handler when not found" do
46
+ @app.run! :server => %w[foo bar mock]
47
+ end
48
+ end
@@ -0,0 +1,582 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class SettingsTest < Test::Unit::TestCase
4
+ setup do
5
+ @base = Sinatra.new(Sinatra::Base)
6
+ @base.set :environment => :foo, :app_file => nil
7
+
8
+ @application = Sinatra.new(Sinatra::Application)
9
+ @application.set :environment => :foo, :app_file => nil
10
+ end
11
+
12
+ it 'sets settings to literal values' do
13
+ @base.set(:foo, 'bar')
14
+ assert @base.respond_to?(:foo)
15
+ assert_equal 'bar', @base.foo
16
+ end
17
+
18
+ it 'sets settings to Procs' do
19
+ @base.set(:foo, Proc.new { 'baz' })
20
+ assert @base.respond_to?(:foo)
21
+ assert_equal 'baz', @base.foo
22
+ end
23
+
24
+ it 'sets settings using a block' do
25
+ @base.set(:foo){ 'baz' }
26
+ assert @base.respond_to?(:foo)
27
+ assert_equal 'baz', @base.foo
28
+ end
29
+
30
+ it 'raises an error with a value and a block' do
31
+ assert_raise ArgumentError do
32
+ @base.set(:fiz, 'boom!'){ 'baz' }
33
+ end
34
+ assert !@base.respond_to?(:fiz)
35
+ end
36
+
37
+ it 'raises an error without value and block' do
38
+ assert_raise(ArgumentError) { @base.set(:fiz) }
39
+ assert !@base.respond_to?(:fiz)
40
+ end
41
+
42
+ it 'allows setting a value to the app class' do
43
+ @base.set :base, @base
44
+ assert @base.respond_to?(:base)
45
+ assert_equal @base, @base.base
46
+ end
47
+
48
+ it 'raises an error with the app class as value and a block' do
49
+ assert_raise ArgumentError do
50
+ @base.set(:fiz, @base) { 'baz' }
51
+ end
52
+ assert !@base.respond_to?(:fiz)
53
+ end
54
+
55
+ it "sets multiple settings with a Hash" do
56
+ @base.set :foo => 1234,
57
+ :bar => 'Hello World',
58
+ :baz => Proc.new { 'bizzle' }
59
+ assert_equal 1234, @base.foo
60
+ assert_equal 'Hello World', @base.bar
61
+ assert_equal 'bizzle', @base.baz
62
+ end
63
+
64
+ it 'sets multiple settings using #each' do
65
+ @base.set [["foo", "bar"]]
66
+ assert_equal "bar", @base.foo
67
+ end
68
+
69
+ it 'inherits settings methods when subclassed' do
70
+ @base.set :foo, 'bar'
71
+ @base.set :biz, Proc.new { 'baz' }
72
+
73
+ sub = Class.new(@base)
74
+ assert sub.respond_to?(:foo)
75
+ assert_equal 'bar', sub.foo
76
+ assert sub.respond_to?(:biz)
77
+ assert_equal 'baz', sub.biz
78
+ end
79
+
80
+ it 'overrides settings in subclass' do
81
+ @base.set :foo, 'bar'
82
+ @base.set :biz, Proc.new { 'baz' }
83
+ sub = Class.new(@base)
84
+ sub.set :foo, 'bling'
85
+ assert_equal 'bling', sub.foo
86
+ assert_equal 'bar', @base.foo
87
+ end
88
+
89
+ it 'creates setter methods when first defined' do
90
+ @base.set :foo, 'bar'
91
+ assert @base.respond_to?('foo=')
92
+ @base.foo = 'biz'
93
+ assert_equal 'biz', @base.foo
94
+ end
95
+
96
+ it 'creates predicate methods when first defined' do
97
+ @base.set :foo, 'hello world'
98
+ assert @base.respond_to?(:foo?)
99
+ assert @base.foo?
100
+ @base.set :foo, nil
101
+ assert !@base.foo?
102
+ end
103
+
104
+ it 'uses existing setter methods if detected' do
105
+ class << @base
106
+ def foo
107
+ @foo
108
+ end
109
+ def foo=(value)
110
+ @foo = 'oops'
111
+ end
112
+ end
113
+
114
+ @base.set :foo, 'bam'
115
+ assert_equal 'oops', @base.foo
116
+ end
117
+
118
+ it 'merges values of multiple set calls if those are hashes' do
119
+ @base.set :foo, :a => 1
120
+ sub = Class.new(@base)
121
+ sub.set :foo, :b => 2
122
+ assert_equal({:a => 1, :b => 2}, sub.foo)
123
+ end
124
+
125
+ it 'merging does not affect the superclass' do
126
+ @base.set :foo, :a => 1
127
+ sub = Class.new(@base)
128
+ sub.set :foo, :b => 2
129
+ assert_equal({:a => 1}, @base.foo)
130
+ end
131
+
132
+ it 'is possible to change a value from a hash to something else' do
133
+ @base.set :foo, :a => 1
134
+ @base.set :foo, :bar
135
+ assert_equal(:bar, @base.foo)
136
+ end
137
+
138
+ it 'merges values with values of the superclass if those are hashes' do
139
+ @base.set :foo, :a => 1
140
+ @base.set :foo, :b => 2
141
+ assert_equal({:a => 1, :b => 2}, @base.foo)
142
+ end
143
+
144
+ it "sets multiple settings to true with #enable" do
145
+ @base.enable :sessions, :foo, :bar
146
+ assert @base.sessions
147
+ assert @base.foo
148
+ assert @base.bar
149
+ end
150
+
151
+ it "sets multiple settings to false with #disable" do
152
+ @base.disable :sessions, :foo, :bar
153
+ assert !@base.sessions
154
+ assert !@base.foo
155
+ assert !@base.bar
156
+ end
157
+
158
+ it 'is accessible from instances via #settings' do
159
+ assert_equal :foo, @base.new!.settings.environment
160
+ end
161
+
162
+ it 'is accessible from class via #settings' do
163
+ assert_equal :foo, @base.settings.environment
164
+ end
165
+
166
+ describe 'methodoverride' do
167
+ it 'is disabled on Base' do
168
+ assert ! @base.method_override?
169
+ end
170
+
171
+ it 'is enabled on Application' do
172
+ assert @application.method_override?
173
+ end
174
+
175
+ it 'enables MethodOverride middleware' do
176
+ @base.set :method_override, true
177
+ @base.put('/') { 'okay' }
178
+ @app = @base
179
+ post '/', {'_method'=>'PUT'}, {}
180
+ assert_equal 200, status
181
+ assert_equal 'okay', body
182
+ end
183
+
184
+ it 'is backward compatible with methodoverride' do
185
+ assert ! @base.methodoverride?
186
+ @base.enable :methodoverride
187
+ assert @base.methodoverride?
188
+ end
189
+ end
190
+
191
+ describe 'run' do
192
+ it 'is disabled on Base' do
193
+ assert ! @base.run?
194
+ end
195
+
196
+ it 'is enabled on Application except in test environment' do
197
+ assert @application.run?
198
+
199
+ @application.set :environment, :test
200
+ assert ! @application.run?
201
+ end
202
+ end
203
+
204
+ describe 'raise_errors' do
205
+ it 'is enabled on Base only in test' do
206
+ assert ! @base.raise_errors?
207
+
208
+ @base.set(:environment, :test)
209
+ assert @base.raise_errors?
210
+ end
211
+
212
+ it 'is enabled on Application only in test' do
213
+ assert ! @application.raise_errors?
214
+
215
+ @application.set(:environment, :test)
216
+ assert @application.raise_errors?
217
+ end
218
+ end
219
+
220
+ describe 'show_exceptions' do
221
+ it 'is disabled on Base except under development' do
222
+ assert ! @base.show_exceptions?
223
+ @base.environment = :development
224
+ assert @base.show_exceptions?
225
+ end
226
+
227
+ it 'is disabled on Application except in development' do
228
+ assert ! @application.show_exceptions?
229
+
230
+ @application.set(:environment, :development)
231
+ assert @application.show_exceptions?
232
+ end
233
+
234
+ it 'returns a friendly 500' do
235
+ klass = Sinatra.new(Sinatra::Application)
236
+ mock_app(klass) {
237
+ enable :show_exceptions
238
+
239
+ get '/' do
240
+ raise StandardError
241
+ end
242
+ }
243
+
244
+ get '/'
245
+ assert_equal 500, status
246
+ assert body.include?("StandardError")
247
+ assert body.include?("<code>show_exceptions</code> setting")
248
+ end
249
+
250
+ it 'does not override app-specified error handling when set to :after_handler' do
251
+ ran = false
252
+ mock_app do
253
+ set :show_exceptions, :after_handler
254
+ error(RuntimeError) { ran = true }
255
+ get('/') { raise RuntimeError }
256
+ end
257
+
258
+ get '/'
259
+ assert_equal 500, status
260
+ assert ran
261
+ end
262
+
263
+ it 'does catch any other exceptions when set to :after_handler' do
264
+ ran = false
265
+ mock_app do
266
+ set :show_exceptions, :after_handler
267
+ error(RuntimeError) { ran = true }
268
+ get('/') { raise ArgumentError }
269
+ end
270
+
271
+ get '/'
272
+ assert_equal 500, status
273
+ assert !ran
274
+ end
275
+ end
276
+
277
+ describe 'dump_errors' do
278
+ it 'is disabled on Base in test' do
279
+ @base.environment = :test
280
+ assert ! @base.dump_errors?
281
+ @base.environment = :development
282
+ assert @base.dump_errors?
283
+ @base.environment = :production
284
+ assert @base.dump_errors?
285
+ end
286
+
287
+ it 'dumps exception with backtrace to rack.errors' do
288
+ klass = Sinatra.new(Sinatra::Application)
289
+
290
+ mock_app(klass) {
291
+ enable :dump_errors
292
+ disable :raise_errors
293
+
294
+ error do
295
+ error = @env['rack.errors'].instance_variable_get(:@error)
296
+ error.rewind
297
+
298
+ error.read
299
+ end
300
+
301
+ get '/' do
302
+ raise
303
+ end
304
+ }
305
+
306
+ get '/'
307
+ assert body.include?("RuntimeError") && body.include?("settings_test.rb")
308
+ end
309
+
310
+ it 'does not dump 404 errors' do
311
+ klass = Sinatra.new(Sinatra::Application)
312
+
313
+ mock_app(klass) {
314
+ enable :dump_errors
315
+ disable :raise_errors
316
+
317
+ error do
318
+ error = @env['rack.errors'].instance_variable_get(:@error)
319
+ error.rewind
320
+
321
+ error.read
322
+ end
323
+
324
+ get '/' do
325
+ raise Sinatra::NotFound
326
+ end
327
+ }
328
+
329
+ get '/'
330
+ assert !body.include?("NotFound") && !body.include?("settings_test.rb")
331
+ end
332
+ end
333
+
334
+ describe 'sessions' do
335
+ it 'is disabled on Base' do
336
+ assert ! @base.sessions?
337
+ end
338
+
339
+ it 'is disabled on Application' do
340
+ assert ! @application.sessions?
341
+ end
342
+ end
343
+
344
+ describe 'logging' do
345
+ it 'is disabled on Base' do
346
+ assert ! @base.logging?
347
+ end
348
+
349
+ it 'is enabled on Application except in test environment' do
350
+ assert @application.logging?
351
+
352
+ @application.set :environment, :test
353
+ assert ! @application.logging
354
+ end
355
+ end
356
+
357
+ describe 'static' do
358
+ it 'is disabled on Base by default' do
359
+ assert ! @base.static?
360
+ end
361
+
362
+ it 'is enabled on Base when public_folder is set and exists' do
363
+ @base.set :environment, :development
364
+ @base.set :public_folder, File.dirname(__FILE__)
365
+ assert @base.static?
366
+ end
367
+
368
+ it 'is enabled on Base when root is set and root/public_folder exists' do
369
+ @base.set :environment, :development
370
+ @base.set :root, File.dirname(__FILE__)
371
+ assert @base.static?
372
+ end
373
+
374
+ it 'is disabled on Application by default' do
375
+ assert ! @application.static?
376
+ end
377
+
378
+ it 'is enabled on Application when public_folder is set and exists' do
379
+ @application.set :environment, :development
380
+ @application.set :public_folder, File.dirname(__FILE__)
381
+ assert @application.static?
382
+ end
383
+
384
+ it 'is enabled on Application when root is set and root/public_folder exists' do
385
+ @application.set :environment, :development
386
+ @application.set :root, File.dirname(__FILE__)
387
+ assert @application.static?
388
+ end
389
+
390
+ it 'is possible to use Module#public' do
391
+ @base.send(:define_method, :foo) { }
392
+ @base.send(:private, :foo)
393
+ assert !@base.public_method_defined?(:foo)
394
+ @base.send(:public, :foo)
395
+ assert @base.public_method_defined?(:foo)
396
+ end
397
+
398
+ it 'is possible to use the keyword public in a sinatra app' do
399
+ app = Sinatra.new do
400
+ private
401
+ def priv; end
402
+ public
403
+ def pub; end
404
+ end
405
+ assert !app.public_method_defined?(:priv)
406
+ assert app.public_method_defined?(:pub)
407
+ end
408
+ end
409
+
410
+ describe 'bind' do
411
+ it 'defaults to 0.0.0.0' do
412
+ assert_equal '0.0.0.0', @base.bind
413
+ assert_equal '0.0.0.0', @application.bind
414
+ end
415
+ end
416
+
417
+ describe 'port' do
418
+ it 'defaults to 4567' do
419
+ assert_equal 4567, @base.port
420
+ assert_equal 4567, @application.port
421
+ end
422
+ end
423
+
424
+ describe 'server' do
425
+ it 'includes webrick' do
426
+ assert @base.server.include?('webrick')
427
+ assert @application.server.include?('webrick')
428
+ end
429
+
430
+ it 'includes puma' do
431
+ assert @base.server.include?('puma')
432
+ assert @application.server.include?('puma')
433
+ end
434
+
435
+ it 'includes thin' do
436
+ next if RUBY_ENGINE == 'jruby'
437
+ assert @base.server.include?('thin')
438
+ assert @application.server.include?('thin')
439
+ end
440
+ end
441
+
442
+ describe 'app_file' do
443
+ it 'is nil for base classes' do
444
+ assert_nil Sinatra::Base.app_file
445
+ assert_nil Sinatra::Application.app_file
446
+ end
447
+
448
+ it 'defaults to the file subclassing' do
449
+ assert_equal File.expand_path(__FILE__), Sinatra.new.app_file
450
+ end
451
+ end
452
+
453
+ describe 'root' do
454
+ it 'is nil if app_file is not set' do
455
+ assert @base.root.nil?
456
+ assert @application.root.nil?
457
+ end
458
+
459
+ it 'is equal to the expanded basename of app_file' do
460
+ @base.app_file = __FILE__
461
+ assert_equal File.expand_path(File.dirname(__FILE__)), @base.root
462
+
463
+ @application.app_file = __FILE__
464
+ assert_equal File.expand_path(File.dirname(__FILE__)), @application.root
465
+ end
466
+ end
467
+
468
+ describe 'views' do
469
+ it 'is nil if root is not set' do
470
+ assert @base.views.nil?
471
+ assert @application.views.nil?
472
+ end
473
+
474
+ it 'is set to root joined with views/' do
475
+ @base.root = File.dirname(__FILE__)
476
+ assert_equal File.dirname(__FILE__) + "/views", @base.views
477
+
478
+ @application.root = File.dirname(__FILE__)
479
+ assert_equal File.dirname(__FILE__) + "/views", @application.views
480
+ end
481
+ end
482
+
483
+ describe 'public_folder' do
484
+ it 'is nil if root is not set' do
485
+ assert @base.public_folder.nil?
486
+ assert @application.public_folder.nil?
487
+ end
488
+
489
+ it 'is set to root joined with public/' do
490
+ @base.root = File.dirname(__FILE__)
491
+ assert_equal File.dirname(__FILE__) + "/public", @base.public_folder
492
+
493
+ @application.root = File.dirname(__FILE__)
494
+ assert_equal File.dirname(__FILE__) + "/public", @application.public_folder
495
+ end
496
+ end
497
+
498
+ describe 'public_dir' do
499
+ it 'is an alias for public_folder' do
500
+ @base.public_dir = File.dirname(__FILE__)
501
+ assert_equal File.dirname(__FILE__), @base.public_dir
502
+ assert_equal @base.public_folder, @base.public_dir
503
+
504
+ @application.public_dir = File.dirname(__FILE__)
505
+ assert_equal File.dirname(__FILE__), @application.public_dir
506
+ assert_equal @application.public_folder, @application.public_dir
507
+ end
508
+ end
509
+
510
+ describe 'lock' do
511
+ it 'is disabled by default' do
512
+ assert ! @base.lock?
513
+ assert ! @application.lock?
514
+ end
515
+ end
516
+
517
+ describe 'protection' do
518
+ class MiddlewareTracker < Rack::Builder
519
+ def self.track
520
+ Rack.send :remove_const, :Builder
521
+ Rack.const_set :Builder, MiddlewareTracker
522
+ MiddlewareTracker.used.clear
523
+ yield
524
+ ensure
525
+ Rack.send :remove_const, :Builder
526
+ Rack.const_set :Builder, MiddlewareTracker.superclass
527
+ end
528
+
529
+ def self.used
530
+ @used ||= []
531
+ end
532
+
533
+ def use(middleware, *)
534
+ MiddlewareTracker.used << middleware
535
+ super
536
+ end
537
+ end
538
+
539
+ it 'sets up Rack::Protection' do
540
+ MiddlewareTracker.track do
541
+ Sinatra::Base.new
542
+ assert_include MiddlewareTracker.used, Rack::Protection
543
+ end
544
+ end
545
+
546
+ it 'sets up Rack::Protection::PathTraversal' do
547
+ MiddlewareTracker.track do
548
+ Sinatra::Base.new
549
+ assert_include MiddlewareTracker.used, Rack::Protection::PathTraversal
550
+ end
551
+ end
552
+
553
+ it 'does not set up Rack::Protection::PathTraversal when disabling it' do
554
+ MiddlewareTracker.track do
555
+ Sinatra.new { set :protection, :except => :path_traversal }.new
556
+ assert_include MiddlewareTracker.used, Rack::Protection
557
+ assert !MiddlewareTracker.used.include?(Rack::Protection::PathTraversal)
558
+ end
559
+ end
560
+
561
+ it 'sets up RemoteToken if sessions are enabled' do
562
+ MiddlewareTracker.track do
563
+ Sinatra.new { enable :sessions }.new
564
+ assert_include MiddlewareTracker.used, Rack::Protection::RemoteToken
565
+ end
566
+ end
567
+
568
+ it 'does not set up RemoteToken if sessions are disabled' do
569
+ MiddlewareTracker.track do
570
+ Sinatra.new.new
571
+ assert !MiddlewareTracker.used.include?(Rack::Protection::RemoteToken)
572
+ end
573
+ end
574
+
575
+ it 'sets up RemoteToken if it is configured to' do
576
+ MiddlewareTracker.track do
577
+ Sinatra.new { set :protection, :session => true }.new
578
+ assert_include MiddlewareTracker.used, Rack::Protection::RemoteToken
579
+ end
580
+ end
581
+ end
582
+ end