sinatra-sinatra 0.9.0.5 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/options_test.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  describe 'Options' do
4
- before { @app = Class.new(Sinatra::Base) }
4
+ before do
5
+ restore_default_options
6
+ @app = Sinatra.new
7
+ end
5
8
 
6
9
  it 'sets options to literal values' do
7
10
  @app.set(:foo, 'bar')
@@ -96,17 +99,17 @@ describe 'Options' do
96
99
  end
97
100
  end
98
101
 
99
- describe 'Backtrace Cleaning (clean_trace option)' do
100
- before do
101
- @app = Class.new(Sinatra::Base)
102
+ describe_option 'clean_trace' do
103
+ def clean_backtrace(trace)
104
+ @base.new.send(:clean_backtrace, trace)
102
105
  end
103
106
 
104
- def clean_backtrace(trace)
105
- @app.new.send(:clean_backtrace, trace)
107
+ it 'is enabled on Base' do
108
+ assert @base.clean_trace?
106
109
  end
107
110
 
108
- it 'is enabled by default' do
109
- assert @app.clean_trace
111
+ it 'is enabled on Default' do
112
+ assert @default.clean_trace?
110
113
  end
111
114
 
112
115
  it 'does nothing when disabled' do
@@ -115,7 +118,7 @@ describe 'Backtrace Cleaning (clean_trace option)' do
115
118
  "./myapp:42",
116
119
  ("#{Gem.dir}/some/lib.rb" if defined?(Gem))
117
120
  ].compact
118
- @app.set :clean_trace, false
121
+ @base.set :clean_trace, false
119
122
  assert_equal backtrace, clean_backtrace(backtrace)
120
123
  end
121
124
 
@@ -138,3 +141,234 @@ describe 'Backtrace Cleaning (clean_trace option)' do
138
141
  end
139
142
  end
140
143
  end
144
+
145
+ describe_option 'run' do
146
+ it 'is disabled on Base' do
147
+ assert ! @base.run?
148
+ end
149
+
150
+ it 'is enabled on Default when not in test environment' do
151
+ assert @default.development?
152
+ assert @default.run?
153
+
154
+ @default.set :environment, :development
155
+ assert @default.run?
156
+ end
157
+
158
+ # TODO: it 'is enabled when $0 == app_file'
159
+ end
160
+
161
+ describe_option 'raise_errors' do
162
+ it 'is enabled on Base' do
163
+ assert @base.raise_errors?
164
+ end
165
+
166
+ it 'is enabled on Default only in test' do
167
+ @default.set(:environment, :development)
168
+ assert @default.development?
169
+ assert ! @default.raise_errors?, "disabled development"
170
+
171
+ @default.set(:environment, :production)
172
+ assert ! @default.raise_errors?
173
+
174
+ @default.set(:environment, :test)
175
+ assert @default.raise_errors?
176
+ end
177
+ end
178
+
179
+ describe_option 'dump_errors' do
180
+ it 'is disabled on Base' do
181
+ assert ! @base.dump_errors?
182
+ end
183
+
184
+ it 'is enabled on Default' do
185
+ assert @default.dump_errors?
186
+ end
187
+
188
+ it 'dumps exception with backtrace to rack.errors' do
189
+ Sinatra::Default.disable(:raise_errors)
190
+
191
+ mock_app(Sinatra::Default) {
192
+ error do
193
+ error = @env['rack.errors'].instance_variable_get(:@error)
194
+ error.rewind
195
+
196
+ error.read
197
+ end
198
+
199
+ get '/' do
200
+ raise
201
+ end
202
+ }
203
+
204
+ get '/'
205
+ assert body.include?("RuntimeError") && body.include?("options_test.rb")
206
+ end
207
+ end
208
+
209
+ describe_option 'sessions' do
210
+ it 'is disabled on Base' do
211
+ assert ! @base.sessions?
212
+ end
213
+
214
+ it 'is disabled on Default' do
215
+ assert ! @default.sessions?
216
+ end
217
+
218
+ # TODO: it 'uses Rack::Session::Cookie when enabled' do
219
+ end
220
+
221
+ describe_option 'logging' do
222
+ it 'is disabled on Base' do
223
+ assert ! @base.logging?
224
+ end
225
+
226
+ it 'is enabled on Default when not in test environment' do
227
+ assert @default.logging?
228
+
229
+ @default.set :environment, :test
230
+ assert ! @default.logging
231
+ end
232
+
233
+ # TODO: it 'uses Rack::CommonLogger when enabled' do
234
+ end
235
+
236
+ describe_option 'static' do
237
+ it 'is disabled on Base' do
238
+ assert ! @base.static?
239
+ end
240
+
241
+ it 'is enabled on Default' do
242
+ assert @default.static?
243
+ end
244
+
245
+ # TODO: it setup static routes if public is enabled
246
+ # TODO: however, that's already tested in static_test so...
247
+ end
248
+
249
+ describe_option 'host' do
250
+ it 'defaults to 0.0.0.0' do
251
+ assert_equal '0.0.0.0', @base.host
252
+ assert_equal '0.0.0.0', @default.host
253
+ end
254
+ end
255
+
256
+ describe_option 'port' do
257
+ it 'defaults to 4567' do
258
+ assert_equal 4567, @base.port
259
+ assert_equal 4567, @default.port
260
+ end
261
+ end
262
+
263
+ describe_option 'server' do
264
+ it 'is one of thin, mongrel, webrick' do
265
+ assert_equal %w[thin mongrel webrick], @base.server
266
+ assert_equal %w[thin mongrel webrick], @default.server
267
+ end
268
+ end
269
+
270
+ describe_option 'app_file' do
271
+ it 'is nil' do
272
+ assert @base.app_file.nil?
273
+ assert @default.app_file.nil?
274
+ end
275
+ end
276
+
277
+ describe_option 'root' do
278
+ it 'is nil if app_file is not set' do
279
+ assert @base.root.nil?
280
+ assert @default.root.nil?
281
+ end
282
+
283
+ it 'is equal to the expanded basename of app_file' do
284
+ @base.app_file = __FILE__
285
+ assert_equal File.expand_path(File.dirname(__FILE__)), @base.root
286
+
287
+ @default.app_file = __FILE__
288
+ assert_equal File.expand_path(File.dirname(__FILE__)), @default.root
289
+ end
290
+ end
291
+
292
+ describe_option 'views' do
293
+ it 'is nil if root is not set' do
294
+ assert @base.views.nil?
295
+ assert @default.views.nil?
296
+ end
297
+
298
+ it 'is set to root joined with views/' do
299
+ @base.root = File.dirname(__FILE__)
300
+ assert_equal File.dirname(__FILE__) + "/views", @base.views
301
+
302
+ @default.root = File.dirname(__FILE__)
303
+ assert_equal File.dirname(__FILE__) + "/views", @default.views
304
+ end
305
+ end
306
+
307
+ describe_option 'public' do
308
+ it 'is nil if root is not set' do
309
+ assert @base.public.nil?
310
+ assert @default.public.nil?
311
+ end
312
+
313
+ it 'is set to root joined with public/' do
314
+ @base.root = File.dirname(__FILE__)
315
+ assert_equal File.dirname(__FILE__) + "/public", @base.public
316
+
317
+ @default.root = File.dirname(__FILE__)
318
+ assert_equal File.dirname(__FILE__) + "/public", @default.public
319
+ end
320
+ end
321
+
322
+ describe_option 'reload' do
323
+ it 'is enabled when
324
+ app_file is set,
325
+ is not a rackup file,
326
+ and we are in development' do
327
+ @base.app_file = __FILE__
328
+ @base.set(:environment, :development)
329
+ assert @base.reload?
330
+
331
+ @default.app_file = __FILE__
332
+ @default.set(:environment, :development)
333
+ assert @default.reload?
334
+ end
335
+
336
+ it 'is disabled if app_file is not set' do
337
+ assert ! @base.reload?
338
+ assert ! @default.reload?
339
+ end
340
+
341
+ it 'is disabled if app_file is a rackup file' do
342
+ @base.app_file = 'config.ru'
343
+ assert ! @base.reload?
344
+
345
+ @default.app_file = 'config.ru'
346
+ assert ! @base.reload?
347
+ end
348
+
349
+ it 'is disabled if we are not in development' do
350
+ @base.set(:environment, :foo)
351
+ assert ! @base.reload
352
+
353
+ @default.set(:environment, :bar)
354
+ assert ! @default.reload
355
+ end
356
+ end
357
+
358
+ describe_option 'lock' do
359
+ it 'is enabled when reload is enabled' do
360
+ @base.enable(:reload)
361
+ assert @base.lock?
362
+
363
+ @default.enable(:reload)
364
+ assert @default.lock?
365
+ end
366
+
367
+ it 'is disabled when reload is disabled' do
368
+ @base.disable(:reload)
369
+ assert ! @base.lock?
370
+
371
+ @default.disable(:reload)
372
+ assert ! @default.lock?
373
+ end
374
+ end
data/test/routing_test.rb CHANGED
@@ -44,6 +44,27 @@ describe "Routing" do
44
44
  assert_equal 404, status
45
45
  end
46
46
 
47
+ it 'takes multiple definitions of a route' do
48
+ mock_app {
49
+ user_agent(/Foo/)
50
+ get '/foo' do
51
+ 'foo'
52
+ end
53
+
54
+ get '/foo' do
55
+ 'not foo'
56
+ end
57
+ }
58
+
59
+ get '/foo', {}, 'HTTP_USER_AGENT' => 'Foo'
60
+ assert ok?
61
+ assert_equal 'foo', body
62
+
63
+ get '/foo'
64
+ assert ok?
65
+ assert_equal 'not foo', body
66
+ end
67
+
47
68
  it "exposes params with indifferent hash" do
48
69
  mock_app {
49
70
  get '/:foo' do
@@ -139,6 +160,32 @@ describe "Routing" do
139
160
  assert ok?
140
161
  end
141
162
 
163
+ it "matches a dot ('.') as part of a named param" do
164
+ mock_app {
165
+ get '/:foo/:bar' do
166
+ params[:foo]
167
+ end
168
+ }
169
+
170
+ get '/user@example.com/name'
171
+ assert_equal 200, response.status
172
+ assert_equal 'user@example.com', body
173
+ end
174
+
175
+ it "matches a literal dot ('.') outside of named params" do
176
+ mock_app {
177
+ get '/:file.:ext' do
178
+ assert_equal 'pony', params[:file]
179
+ assert_equal 'jpg', params[:ext]
180
+ 'right on'
181
+ end
182
+ }
183
+
184
+ get '/pony.jpg'
185
+ assert_equal 200, response.status
186
+ assert_equal 'right on', body
187
+ end
188
+
142
189
  it "literally matches . in paths" do
143
190
  route_def '/test.bar'
144
191
 
@@ -158,7 +205,7 @@ describe "Routing" do
158
205
  it "literally matches + in paths" do
159
206
  route_def '/te+st/'
160
207
 
161
- get '/te+st/'
208
+ get '/te%2Bst/'
162
209
  assert ok?
163
210
  get '/teeeeeeest/'
164
211
  assert not_found?
@@ -243,7 +290,7 @@ describe "Routing" do
243
290
  assert_equal 'looks good', body
244
291
  end
245
292
 
246
- it "supports paths that include spaces" do
293
+ it "matches paths that include spaces encoded with %20" do
247
294
  mock_app {
248
295
  get '/path with spaces' do
249
296
  'looks good'
@@ -255,6 +302,18 @@ describe "Routing" do
255
302
  assert_equal 'looks good', body
256
303
  end
257
304
 
305
+ it "matches paths that include spaces encoded with +" do
306
+ mock_app {
307
+ get '/path with spaces' do
308
+ 'looks good'
309
+ end
310
+ }
311
+
312
+ get '/path+with+spaces'
313
+ assert ok?
314
+ assert_equal 'looks good', body
315
+ end
316
+
258
317
  it "URL decodes named parameters and splats" do
259
318
  mock_app {
260
319
  get '/:foo/*' do
data/test/test_test.rb CHANGED
@@ -1,21 +1,138 @@
1
+ require 'yaml'
1
2
  require File.dirname(__FILE__) + '/helper'
2
3
 
3
- describe "Sinatra::Test" do
4
- it "support nested parameters" do
4
+ describe 'Sinatra::Test' do
5
+ def request
6
+ YAML.load(body)
7
+ end
8
+
9
+ def request_body
10
+ request['test.body']
11
+ end
12
+
13
+ def request_params
14
+ YAML.load(request['test.params'])
15
+ end
16
+
17
+ before do
5
18
  mock_app {
19
+ %w[get head post put delete].each { |verb|
20
+ send(verb, '/') do
21
+ redirect '/redirected' if params[:redirect]
22
+ env.update('test.body' => request.body.read)
23
+ env.update('test.params' => params.to_yaml)
24
+ env.to_yaml
25
+ end
26
+ }
27
+
28
+ get '/redirected' do
29
+ "you've been redirected"
30
+ end
31
+ }
32
+ end
33
+
34
+ it 'allows GET/HEAD/POST/PUT/DELETE' do
35
+ get '/'
36
+ assert_equal('GET', request['REQUEST_METHOD'])
37
+
38
+ post '/'
39
+ assert_equal('POST', request['REQUEST_METHOD'])
40
+
41
+ put '/'
42
+ assert_equal('PUT', request['REQUEST_METHOD'])
43
+
44
+ delete '/'
45
+ assert_equal('DELETE', request['REQUEST_METHOD'])
46
+
47
+ head '/'
48
+ assert_equal('596', response.headers['Content-Length'])
49
+ assert_equal('', response.body)
50
+ end
51
+
52
+ it 'allows to specify a body' do
53
+ post '/', '42'
54
+ assert_equal '42', request_body
55
+ end
56
+
57
+ it 'allows to specify params' do
58
+ get '/', :foo => 'bar'
59
+ assert_equal 'bar', request_params['foo']
60
+ end
61
+
62
+ it 'supports nested params' do
63
+ get '/', :foo => { :x => 'y', :chunky => 'bacon' }
64
+ assert_equal "y", request_params['foo']['x']
65
+ assert_equal "bacon", request_params['foo']['chunky']
66
+ end
67
+
68
+ it 'provides easy access to response status and body' do
69
+ get '/'
70
+ assert_equal 200, status
71
+ assert body =~ /^---/
72
+ end
73
+
74
+ it 'delegates methods to @response' do
75
+ get '/'
76
+ assert ok?
77
+ end
78
+
79
+ it 'follows redirect' do
80
+ get '/', :redirect => true
81
+ follow!
82
+ assert_equal "you've been redirected", body
83
+ end
84
+
85
+ it 'provides sugar for common HTTP headers' do
86
+ get '/', :env => { :accept => 'text/plain' }
87
+ assert_equal 'text/plain', request['HTTP_ACCEPT']
88
+
89
+ get '/', :env => { :agent => 'TATFT' }
90
+ assert_equal 'TATFT', request['HTTP_USER_AGENT']
91
+
92
+ get '/', :env => { :host => '1.2.3.4' }
93
+ assert_equal '1.2.3.4', request['HTTP_HOST']
94
+
95
+ get '/', :env => { :session => 'foo' }
96
+ assert_equal 'foo', request['rack.session']
97
+
98
+ get '/', :env => { :cookies => 'foo' }
99
+ assert_equal 'foo', request['HTTP_COOKIE']
100
+
101
+ get '/', :env => { :content_type => 'text/plain' }
102
+ assert_equal 'text/plain', request['CONTENT_TYPE']
103
+ end
104
+
105
+ it 'allow to test session easily' do
106
+ app = mock_app(Sinatra::Default) {
6
107
  get '/' do
7
- params[:post][:title]
108
+ session['foo'] = 'bar'
109
+ 200
8
110
  end
9
111
 
10
112
  post '/' do
11
- params[:post][:content]
113
+ assert_equal 'bar', session['foo']
114
+ session['foo'] || "blah"
12
115
  end
13
116
  }
14
117
 
15
- get '/', :post => { :title => 'My Post Title' }
16
- assert_equal 'My Post Title', body
118
+ browser = Sinatra::TestHarness.new(app)
119
+ browser.get '/'
120
+ browser.post '/', {}, :session => { 'foo' => 'bar' }
121
+ assert_equal 'bar', browser.response.body
122
+ end
123
+
124
+ it 'yields the request object to the block before invoking the application' do
125
+ called = false
126
+ get '/' do |req|
127
+ called = true
128
+ assert req.kind_of?(Rack::MockRequest)
129
+ end
130
+ assert called
131
+ end
17
132
 
18
- post '/', :post => { :content => 'Post Content' }
19
- assert_equal 'Post Content', body
133
+ def test_TestHarness
134
+ session = Sinatra::TestHarness.new(@app)
135
+ response = session.get('/')
136
+ assert_equal 200, response.status
20
137
  end
21
138
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0.5
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Mizerany
@@ -9,17 +9,21 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-10 00:00:00 -08:00
12
+ date: 2009-03-01 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
20
21
  - - ">="
21
22
  - !ruby/object:Gem::Version
22
23
  version: 0.9.1
24
+ - - <
25
+ - !ruby/object:Gem::Version
26
+ version: "1.0"
23
27
  version:
24
28
  description: Classy web-development dressed in a DSL
25
29
  email: sinatrarb@googlegroups.com