sinatra 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sinatra might be problematic. Click here for more details.

Files changed (95) hide show
  1. data/CHANGELOG +1 -8
  2. data/Manifest +42 -49
  3. data/README.rdoc +430 -0
  4. data/Rakefile +22 -28
  5. data/images/404.png +0 -0
  6. data/images/500.png +0 -0
  7. data/index.html +9 -0
  8. data/lib/sinatra.rb +1144 -46
  9. data/lib/sinatra/test/methods.rb +56 -0
  10. data/lib/sinatra/test/spec.rb +10 -0
  11. data/lib/sinatra/test/unit.rb +13 -0
  12. data/sinatra.gemspec +44 -40
  13. data/test/app_test.rb +150 -0
  14. data/test/application_test.rb +175 -0
  15. data/test/builder_test.rb +101 -0
  16. data/test/custom_error_test.rb +67 -0
  17. data/test/diddy_test.rb +41 -0
  18. data/test/erb_test.rb +116 -0
  19. data/test/event_context_test.rb +15 -0
  20. data/test/events_test.rb +50 -0
  21. data/test/haml_test.rb +181 -0
  22. data/test/helper.rb +3 -16
  23. data/test/mapped_error_test.rb +61 -0
  24. data/test/public/foo.xml +1 -0
  25. data/test/rest_test.rb +16 -0
  26. data/test/sass_test.rb +57 -0
  27. data/test/sessions_test.rb +40 -0
  28. data/test/streaming_test.rb +112 -0
  29. data/test/sym_params_test.rb +19 -0
  30. data/test/template_test.rb +30 -0
  31. data/test/use_in_file_templates_test.rb +48 -0
  32. data/test/views/foo.builder +1 -0
  33. data/test/views/foo.erb +1 -0
  34. data/test/views/foo.haml +1 -0
  35. data/test/views/foo.sass +2 -0
  36. data/test/views/foo_layout.erb +2 -0
  37. data/test/views/foo_layout.haml +2 -0
  38. data/test/views/layout_test/foo.builder +1 -0
  39. data/test/views/layout_test/foo.erb +1 -0
  40. data/test/views/layout_test/foo.haml +1 -0
  41. data/test/views/layout_test/foo.sass +2 -0
  42. data/test/views/layout_test/layout.builder +3 -0
  43. data/test/views/layout_test/layout.erb +1 -0
  44. data/test/views/layout_test/layout.haml +1 -0
  45. data/test/views/layout_test/layout.sass +2 -0
  46. data/test/views/no_layout/no_layout.builder +1 -0
  47. data/test/views/no_layout/no_layout.haml +1 -0
  48. metadata +122 -98
  49. data/LICENSE +0 -22
  50. data/README +0 -100
  51. data/RakeFile +0 -35
  52. data/examples/hello/hello.rb +0 -28
  53. data/examples/hello/views/hello.erb +0 -1
  54. data/examples/todo/todo.rb +0 -38
  55. data/files/default_index.erb +0 -42
  56. data/files/error.erb +0 -9
  57. data/files/logo.png +0 -0
  58. data/files/not_found.erb +0 -52
  59. data/lib/sinatra/context.rb +0 -88
  60. data/lib/sinatra/context/renderer.rb +0 -75
  61. data/lib/sinatra/core_ext/array.rb +0 -5
  62. data/lib/sinatra/core_ext/class.rb +0 -49
  63. data/lib/sinatra/core_ext/hash.rb +0 -7
  64. data/lib/sinatra/core_ext/kernel.rb +0 -16
  65. data/lib/sinatra/core_ext/metaid.rb +0 -18
  66. data/lib/sinatra/core_ext/module.rb +0 -11
  67. data/lib/sinatra/core_ext/symbol.rb +0 -5
  68. data/lib/sinatra/dispatcher.rb +0 -27
  69. data/lib/sinatra/dsl.rb +0 -176
  70. data/lib/sinatra/environment.rb +0 -15
  71. data/lib/sinatra/event.rb +0 -238
  72. data/lib/sinatra/irb.rb +0 -56
  73. data/lib/sinatra/loader.rb +0 -31
  74. data/lib/sinatra/logger.rb +0 -22
  75. data/lib/sinatra/options.rb +0 -49
  76. data/lib/sinatra/rack_ext/request.rb +0 -15
  77. data/lib/sinatra/route.rb +0 -65
  78. data/lib/sinatra/server.rb +0 -57
  79. data/lib/sinatra/sessions.rb +0 -21
  80. data/lib/sinatra/test_methods.rb +0 -55
  81. data/site/index.htm +0 -104
  82. data/site/index.html +0 -104
  83. data/site/logo.png +0 -0
  84. data/test/sinatra/dispatcher_test.rb +0 -91
  85. data/test/sinatra/event_test.rb +0 -46
  86. data/test/sinatra/renderer_test.rb +0 -47
  87. data/test/sinatra/request_test.rb +0 -21
  88. data/test/sinatra/route_test.rb +0 -21
  89. data/test/sinatra/static_files/foo.txt +0 -1
  90. data/test/sinatra/static_files_test.rb +0 -48
  91. data/test/sinatra/url_test.rb +0 -18
  92. data/vendor/erb/init.rb +0 -3
  93. data/vendor/erb/lib/erb.rb +0 -41
  94. data/vendor/haml/init.rb +0 -3
  95. data/vendor/haml/lib/haml.rb +0 -41
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "Builder" do
4
+
5
+ setup do
6
+ Sinatra.application = nil
7
+ end
8
+
9
+ context "without layouts" do
10
+
11
+ setup do
12
+ Sinatra.application = nil
13
+ end
14
+
15
+ specify "should render" do
16
+
17
+ get '/no_layout' do
18
+ builder 'xml.instruct!'
19
+ end
20
+
21
+ get_it '/no_layout'
22
+ should.be.ok
23
+ body.should == %(<?xml version="1.0" encoding="UTF-8"?>\n)
24
+
25
+ end
26
+
27
+ specify "should render inline block" do
28
+
29
+ get '/no_layout_and_inlined' do
30
+ @name = "Frank & Mary"
31
+ builder do |xml|
32
+ xml.couple @name
33
+ end
34
+ end
35
+
36
+ get_it '/no_layout_and_inlined'
37
+ should.be.ok
38
+ body.should == %(<couple>Frank &amp; Mary</couple>\n)
39
+
40
+ end
41
+
42
+ end
43
+
44
+
45
+
46
+ context "Templates (in general)" do
47
+
48
+ setup do
49
+ Sinatra.application = nil
50
+ end
51
+
52
+ specify "are read from files if Symbols" do
53
+
54
+ get '/from_file' do
55
+ @name = 'Blue'
56
+ builder :foo, :views_directory => File.dirname(__FILE__) + "/views"
57
+ end
58
+
59
+ get_it '/from_file'
60
+ should.be.ok
61
+ body.should.equal %(<exclaim>You rock Blue!</exclaim>\n)
62
+
63
+ end
64
+
65
+ specify "use layout.ext by default if available" do
66
+
67
+ get '/' do
68
+ builder :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
69
+ end
70
+
71
+ get_it '/'
72
+ should.be.ok
73
+ body.should.equal "<layout>\n<this>is foo!</this>\n</layout>\n"
74
+
75
+ end
76
+
77
+ specify "renders without layout" do
78
+
79
+ get '/' do
80
+ builder :no_layout, :views_directory => File.dirname(__FILE__) + "/views/no_layout"
81
+ end
82
+
83
+ get_it '/'
84
+ should.be.ok
85
+ body.should.equal "<foo>No Layout!</foo>\n"
86
+
87
+ end
88
+
89
+ specify "raises error if template not found" do
90
+
91
+ get '/' do
92
+ builder :not_found
93
+ end
94
+
95
+ lambda { get_it '/' }.should.raise(Errno::ENOENT)
96
+
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "Custom Errors (in general)" do
4
+
5
+ setup do
6
+ Sinatra.application = nil
7
+ end
8
+
9
+ specify "override the default 404" do
10
+
11
+ get_it '/'
12
+ should.be.not_found
13
+ body.should.equal '<h1>Not Found</h1>'
14
+
15
+ error Sinatra::NotFound do
16
+ 'Custom 404'
17
+ end
18
+
19
+ get_it '/'
20
+ should.be.not_found
21
+ body.should.equal 'Custom 404'
22
+
23
+ end
24
+
25
+ specify "override the default 500" do
26
+ Sinatra.application.options.raise_errors = false
27
+
28
+ get '/' do
29
+ raise 'asdf'
30
+ end
31
+
32
+ get_it '/'
33
+ status.should.equal 500
34
+ body.should.equal '<h1>Internal Server Error</h1>'
35
+
36
+
37
+ error do
38
+ 'Custom 500 for ' + request.env['sinatra.error'].message
39
+ end
40
+
41
+ get_it '/'
42
+
43
+ get_it '/'
44
+ status.should.equal 500
45
+ body.should.equal 'Custom 500 for asdf'
46
+
47
+ Sinatra.application.options.raise_errors = true
48
+ end
49
+
50
+ class UnmappedError < RuntimeError; end
51
+
52
+ specify "should bring unmapped error back to the top" do
53
+ get '/' do
54
+ raise UnmappedError, 'test'
55
+ end
56
+
57
+ assert_raises(UnmappedError) do
58
+ get_it '/'
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+
65
+
66
+
67
+
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "Diddy" do
4
+
5
+ setup do
6
+ Sinatra.application = nil
7
+ end
8
+
9
+ specify "should map urls to different apps" do
10
+
11
+ get '/' do
12
+ 'asdf'
13
+ end
14
+
15
+ get_it '/'
16
+ assert ok?
17
+ assert_equal('asdf', body)
18
+
19
+ get '/foo', :host => 'foo.sinatrarb.com' do
20
+ 'in foo!'
21
+ end
22
+
23
+ get '/foo', :host => 'bar.sinatrarb.com' do
24
+ 'in bar!'
25
+ end
26
+
27
+ get_it '/foo', {}, 'HTTP_HOST' => 'foo.sinatrarb.com'
28
+ assert ok?
29
+ assert_equal 'in foo!', body
30
+
31
+ get_it '/foo', {}, 'HTTP_HOST' => 'bar.sinatrarb.com'
32
+ assert ok?
33
+ assert_equal 'in bar!', body
34
+
35
+ get_it '/foo'
36
+ assert not_found?
37
+
38
+ end
39
+
40
+ end
41
+
@@ -0,0 +1,116 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "Erb" do
4
+
5
+ setup do
6
+ Sinatra.application = nil
7
+ end
8
+
9
+ context "without layouts" do
10
+
11
+ setup do
12
+ Sinatra.application = nil
13
+ end
14
+
15
+ specify "should render" do
16
+
17
+ get '/no_layout' do
18
+ erb '<%= 1 + 1 %>'
19
+ end
20
+
21
+ get_it '/no_layout'
22
+ should.be.ok
23
+ body.should == '2'
24
+
25
+ end
26
+ end
27
+
28
+ context "with layouts" do
29
+
30
+ setup do
31
+ Sinatra.application = nil
32
+ end
33
+
34
+ specify "can be inline" do
35
+
36
+ layout do
37
+ %Q{This is <%= yield %>!}
38
+ end
39
+
40
+ get '/lay' do
41
+ erb 'Blake'
42
+ end
43
+
44
+ get_it '/lay'
45
+ should.be.ok
46
+ body.should.equal 'This is Blake!'
47
+
48
+ end
49
+
50
+ specify "can use named layouts" do
51
+
52
+ layout :pretty do
53
+ %Q{<h1><%= yield %></h1>}
54
+ end
55
+
56
+ get '/pretty' do
57
+ erb 'Foo', :layout => :pretty
58
+ end
59
+
60
+ get '/not_pretty' do
61
+ erb 'Bar'
62
+ end
63
+
64
+ get_it '/pretty'
65
+ body.should.equal '<h1>Foo</h1>'
66
+
67
+ get_it '/not_pretty'
68
+ body.should.equal 'Bar'
69
+
70
+ end
71
+
72
+ specify "can be read from a file if they're not inlined" do
73
+
74
+ get '/foo' do
75
+ @title = 'Welcome to the Hello Program'
76
+ erb 'Blake', :layout => :foo_layout,
77
+ :views_directory => File.dirname(__FILE__) + "/views"
78
+ end
79
+
80
+ get_it '/foo'
81
+ body.should.equal "Welcome to the Hello Program\nHi Blake\n"
82
+
83
+ end
84
+
85
+ end
86
+
87
+ context "Templates (in general)" do
88
+
89
+ specify "are read from files if Symbols" do
90
+
91
+ get '/from_file' do
92
+ @name = 'Alena'
93
+ erb :foo, :views_directory => File.dirname(__FILE__) + "/views"
94
+ end
95
+
96
+ get_it '/from_file'
97
+
98
+ body.should.equal 'You rock Alena!'
99
+
100
+ end
101
+
102
+ specify "use layout.ext by default if available" do
103
+
104
+ get '/layout_from_file' do
105
+ erb :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
106
+ end
107
+
108
+ get_it '/layout_from_file'
109
+ should.be.ok
110
+ body.should.equal "x This is foo! x \n"
111
+
112
+ end
113
+
114
+ end
115
+
116
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "EventContext" do
4
+
5
+ specify "DSLified setters" do
6
+
7
+ cx = Sinatra::EventContext.new(stub_everything, Rack::Response.new, {})
8
+ lambda {
9
+ cx.status 404
10
+ }.should.not.raise(ArgumentError)
11
+
12
+ end
13
+
14
+ end
15
+
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + '/../lib/sinatra'
2
+
3
+ require 'rubygems'
4
+ require 'test/spec'
5
+
6
+ context "Simple Events" do
7
+
8
+ def simple_request_hash(method, path)
9
+ Rack::Request.new({
10
+ 'REQUEST_METHOD' => method.to_s.upcase,
11
+ 'PATH_INFO' => path
12
+ })
13
+ end
14
+
15
+ def invoke_simple(path, request_path, &b)
16
+ event = Sinatra::Event.new(path, &b)
17
+ event.invoke(simple_request_hash(:get, request_path))
18
+ end
19
+
20
+ specify "return last value" do
21
+ block = Proc.new { 'Simple' }
22
+ result = invoke_simple('/', '/', &block)
23
+ result.should.not.be.nil
24
+ result.block.should.be block
25
+ result.params.should.equal Hash.new
26
+ end
27
+
28
+ specify "takes params in path" do
29
+ result = invoke_simple('/:foo/:bar', '/a/b')
30
+ result.should.not.be.nil
31
+ result.params.should.equal "foo" => 'a', "bar" => 'b'
32
+
33
+ # unscapes
34
+ result = invoke_simple('/:foo/:bar', '/a/blake%20mizerany')
35
+ result.should.not.be.nil
36
+ result.params.should.equal "foo" => 'a', "bar" => 'blake mizerany'
37
+ end
38
+
39
+ specify "ignores to many /'s" do
40
+ result = invoke_simple('/x/y', '/x//y')
41
+ result.should.not.be.nil
42
+ end
43
+
44
+ specify "understands splat" do
45
+ invoke_simple('/foo/*', '/foo/bar').should.not.be.nil
46
+ invoke_simple('/foo/*', '/foo/bar/baz').should.not.be.nil
47
+ invoke_simple('/foo/*', '/foo/baz').should.not.be.nil
48
+ end
49
+
50
+ end
@@ -0,0 +1,181 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "Haml" do
4
+
5
+ setup do
6
+ Sinatra.application = nil
7
+ end
8
+
9
+ context "without layouts" do
10
+
11
+ setup do
12
+ Sinatra.application = nil
13
+ end
14
+
15
+ specify "should render" do
16
+
17
+ get '/no_layout' do
18
+ haml '== #{1+1}'
19
+ end
20
+
21
+ get_it '/no_layout'
22
+ should.be.ok
23
+ body.should == "2\n"
24
+
25
+ end
26
+ end
27
+
28
+ context "with layouts" do
29
+
30
+ setup do
31
+ Sinatra.application = nil
32
+ end
33
+
34
+ specify "can be inline" do
35
+
36
+ layout do
37
+ '== This is #{yield}!'
38
+ end
39
+
40
+ get '/lay' do
41
+ haml 'Blake'
42
+ end
43
+
44
+ get_it '/lay'
45
+ should.be.ok
46
+ body.should.equal "This is Blake\n!\n"
47
+
48
+ end
49
+
50
+ specify "can use named layouts" do
51
+
52
+ layout :pretty do
53
+ '%h1== #{yield}'
54
+ end
55
+
56
+ get '/pretty' do
57
+ haml 'Foo', :layout => :pretty
58
+ end
59
+
60
+ get '/not_pretty' do
61
+ haml 'Bar'
62
+ end
63
+
64
+ get_it '/pretty'
65
+ body.should.equal "<h1>Foo</h1>\n"
66
+
67
+ get_it '/not_pretty'
68
+ body.should.equal "Bar\n"
69
+
70
+ end
71
+
72
+ specify "can be read from a file if they're not inlined" do
73
+
74
+ get '/foo' do
75
+ @title = 'Welcome to the Hello Program'
76
+ haml 'Blake', :layout => :foo_layout,
77
+ :views_directory => File.dirname(__FILE__) + "/views"
78
+ end
79
+
80
+ get_it '/foo'
81
+ body.should.equal "Welcome to the Hello Program\nHi Blake\n"
82
+
83
+ end
84
+
85
+ specify "can be read from file and layout from text" do
86
+ get '/foo' do
87
+ haml 'Test', :layout => '== Foo #{yield}'
88
+ end
89
+
90
+ get_it '/foo'
91
+
92
+ body.should.equal "Foo Test\n"
93
+ end
94
+
95
+ end
96
+
97
+ context "Templates (in general)" do
98
+
99
+ setup do
100
+ Sinatra.application = nil
101
+ end
102
+
103
+ specify "are read from files if Symbols" do
104
+
105
+ get '/from_file' do
106
+ @name = 'Alena'
107
+ haml :foo, :views_directory => File.dirname(__FILE__) + "/views"
108
+ end
109
+
110
+ get_it '/from_file'
111
+
112
+ body.should.equal "You rock Alena!\n"
113
+
114
+ end
115
+
116
+ specify "use layout.ext by default if available" do
117
+
118
+ get '/' do
119
+ haml :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
120
+ end
121
+
122
+ get_it '/'
123
+ should.be.ok
124
+ body.should.equal "x This is foo!\n x\n"
125
+
126
+ end
127
+
128
+ specify "renders without layout" do
129
+
130
+ get '/' do
131
+ haml :no_layout, :views_directory => File.dirname(__FILE__) + "/views/no_layout"
132
+ end
133
+
134
+ get_it '/'
135
+ should.be.ok
136
+ body.should.equal "<h1>No Layout!</h1>\n"
137
+
138
+ end
139
+
140
+ specify "can render with no layout" do
141
+ layout do
142
+ "X\n= yield\nX"
143
+ end
144
+
145
+ get '/' do
146
+ haml 'blake', :layout => false
147
+ end
148
+
149
+ get_it '/'
150
+
151
+ body.should.equal "blake\n"
152
+ end
153
+
154
+ specify "raises error if template not found" do
155
+ get '/' do
156
+ haml :not_found
157
+ end
158
+
159
+ lambda { get_it '/' }.should.raise(Errno::ENOENT)
160
+ end
161
+
162
+ specify "use layout.ext by default if available" do
163
+
164
+ template :foo do
165
+ 'asdf'
166
+ end
167
+
168
+ get '/' do
169
+ haml :foo, :layout => false,
170
+ :views_directory => File.dirname(__FILE__) + "/views/layout_test"
171
+ end
172
+
173
+ get_it '/'
174
+ should.be.ok
175
+ body.should.equal "asdf\n"
176
+
177
+ end
178
+
179
+ end
180
+
181
+ end