nyny 2.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/app_spec.rb CHANGED
@@ -1,200 +1,248 @@
1
- require 'spec_helper'
2
-
3
- describe App do
4
- let (:app) { mock_app {} }
5
-
6
- it 'should return a rack response on call' do
7
- response = app.get '/'
8
- response.should be_a(Rack::Response)
9
- end
10
-
11
- it 'should return 404 for non-matched routes' do
12
- response = app.get random_url
13
- response.status.should == 404
14
- end
15
-
16
- it 'should able to register a extension' do
17
- module Foo
18
- def foo
19
- end
20
- end
21
-
22
- kls = mock_app_class {}
23
- kls.register(Foo)
24
- kls.should respond_to(:foo)
25
- end
26
-
27
- it 'should call registered method on extension' do
28
- module Foo
29
- def self.registered app
30
- #
31
- end
32
- end
33
-
34
- class SomeApp < NYNY::App
35
- end
36
-
37
- Foo.should_receive(:registered).with(SomeApp)
38
- SomeApp.register(Foo)
39
- end
40
-
41
- it 'should match a route for any supported verbs' do
42
- url = random_url
43
- verb = App::HTTP_VERBS.sample
44
-
45
- app = mock_app do
46
- send verb, url do
47
- 'foo'
48
- end
49
- end
50
-
51
- res = app.send verb, url
52
- res.body.should == 'foo'
53
- end
54
-
55
- it 'should support route patterns' do
56
- app = mock_app do
57
- get '/some/:name' do
58
- 'foo'
59
- end
60
-
61
- get '/:name' do
62
- "hello #{params[:name]}"
63
- end
64
- end
65
-
66
- res = app.get '/foo'
67
- res.body.should == "hello foo"
68
- end
69
-
70
- it 'should support adding before filers' do
71
- app = mock_app do
72
- before do
73
- request.should_not == nil
74
- end
75
-
76
- get '/' do
77
- "hello"
78
- end
79
- end
80
-
81
- app.get('/')
82
- end
83
-
84
- it 'does not maintain state between requests' do
85
- app = mock_app do
86
- get '/state' do
87
- @foo ||= "new"
88
- body = "Foo: #{@foo}"
89
- @foo = 'discard'
90
- body
91
- end
92
- end
93
-
94
- 2.times do
95
- response = app.get('/state')
96
- response.should be_ok
97
- 'Foo: new'.should == response.body
98
- end
99
- end
100
-
101
- it 'acts well as a middleware' do
102
- app = lambda do |env|
103
- [210, {}, ['Hello from downstream']]
104
- end
105
-
106
- app_class = mock_app_class do
107
- get '/' do
108
- 'hello'
109
- end
110
- end
111
-
112
- frankie = app_class.new(app)
113
- req = Rack::MockRequest.new frankie
114
- res = req.get '/'
115
- res.body.should == 'hello'
116
-
117
- res2 = req.get '/ither'
118
- res2.body.should == 'Hello from downstream'
119
- end
120
-
121
- it 'should support adding after filers' do
122
- app = mock_app do
123
- after do
124
- response.should_not == nil
125
- end
126
-
127
- get '/' do
128
- "hello"
129
- end
130
- end
131
- app.get '/'
132
- end
133
-
134
- it 'should be able to set cookies' do
135
- app = mock_app do
136
- post '/write' do
137
- cookies.merge! params
138
- end
139
- end
140
-
141
- res = app.post '/write?foo=bar'
142
- res.headers['Set-Cookie'].should == 'foo=bar'
143
- end
144
-
145
- it 'works with empty path' do
146
- kls = mock_app_class do
147
- get '/' do
148
- 'Hello'
149
- end
150
- end
151
-
152
- env = Rack::MockRequest.env_for '/'
153
- env['PATH_INFO'] = ''
154
- kls.new.call(env).body.first.should == 'Hello'
155
- end
156
-
157
- describe 'Class level api' do
158
- let (:app_class) { Class.new(App) }
159
- describe 'middlewares' do
160
- let (:app_class) do
161
- mock_app_class do
162
- use NullMiddleware
163
- end
164
- end
165
-
166
- it 'should allow to add a middleware' do
167
- app_class.middlewares.last.first.should == NullMiddleware
168
- end
169
- end
170
-
171
- describe 'helpers' do
172
- it 'should allow to include a helper in request scope' do
173
- app_class.helpers NullHelper
174
- RequestScope.ancestors.should include(NullHelper)
175
- end
176
-
177
- it 'should allow to include multiple helpers modules' do
178
- module NullHelper2
179
- end
180
-
181
- app_class.helpers NullHelper, NullHelper2
182
- RequestScope.ancestors.should include(NullHelper, NullHelper2)
183
- end
184
-
185
- it 'should allow to define helpers with a block' do
186
- app_class.helpers do
187
- def foo
188
- 'bar'
189
- end
190
- end
191
-
192
- app_class.get '/' do
193
- foo.should == 'bar'
194
- end
195
- req = Rack::MockRequest.env_for '/'
196
- res = app_class.new.call(req)
197
- end
198
- end
199
- end
200
- end
1
+ require 'spec_helper'
2
+
3
+ describe App do
4
+ let (:app) { mock_app {} }
5
+
6
+ it 'should return a rack response on call' do
7
+ response = app.get '/'
8
+ response.should be_a(Rack::Response)
9
+ end
10
+
11
+ it 'should return 404 for non-matched routes' do
12
+ response = app.get random_url
13
+ response.status.should == 404
14
+ end
15
+
16
+ it 'should able to register a extension' do
17
+ module Foo
18
+ def foo
19
+ end
20
+ end
21
+
22
+ kls = mock_app_class {}
23
+ kls.register(Foo)
24
+ kls.should respond_to(:foo)
25
+ end
26
+
27
+ describe 'namespace' do
28
+ let (:app) do
29
+ mock_app do
30
+ namespace '/foo' do
31
+ get '/' do
32
+ 'bar'
33
+ end
34
+ end
35
+
36
+ namespace '/nested' do
37
+ namespace '/space' do
38
+ get '/' do
39
+ 'caramba'
40
+ end
41
+ end
42
+ end
43
+
44
+ get '/' do
45
+ 'no namespace here'
46
+ end
47
+ end
48
+ end
49
+
50
+ it 'allows to specify stuff in namespaces' do
51
+ app.get('/foo').body.should == 'bar'
52
+ end
53
+
54
+ it 'does not break the main app' do
55
+ app.get('/').body.should == 'no namespace here'
56
+ end
57
+
58
+ it 'can be nested as well' do
59
+ app.get('/nested/space/').body.should == 'caramba'
60
+ end
61
+ end
62
+
63
+ it 'should call registered method on extension' do
64
+ module Foo
65
+ def self.registered app
66
+ #
67
+ end
68
+ end
69
+
70
+ class SomeApp < NYNY::App
71
+ end
72
+
73
+ Foo.should_receive(:registered).with(SomeApp)
74
+ SomeApp.register(Foo)
75
+ end
76
+
77
+ it 'should match a route for any supported verbs' do
78
+ url = random_url
79
+ verb = App::HTTP_VERBS.sample
80
+
81
+ app = mock_app do
82
+ send verb, url do
83
+ 'foo'
84
+ end
85
+ end
86
+
87
+ res = app.send verb, url
88
+ res.body.should == 'foo'
89
+ end
90
+
91
+ it 'should support route patterns' do
92
+ app = mock_app do
93
+ get '/some/:name' do
94
+ 'foo'
95
+ end
96
+
97
+ get '/:name' do
98
+ "hello #{params[:name]}"
99
+ end
100
+ end
101
+
102
+ res = app.get '/foo'
103
+ res.body.should == "hello foo"
104
+ end
105
+
106
+ it 'should support adding before filers' do
107
+ app = mock_app do
108
+ before do
109
+ request.should_not == nil
110
+ end
111
+
112
+ get '/' do
113
+ "hello"
114
+ end
115
+ end
116
+
117
+ app.get('/')
118
+ end
119
+
120
+ it 'does not maintain state between requests' do
121
+ app = mock_app do
122
+ get '/state' do
123
+ @foo ||= "new"
124
+ body = "Foo: #{@foo}"
125
+ @foo = 'discard'
126
+ body
127
+ end
128
+ end
129
+
130
+ 2.times do
131
+ response = app.get('/state')
132
+ response.should be_ok
133
+ 'Foo: new'.should == response.body
134
+ end
135
+ end
136
+
137
+ it 'acts well as a middleware' do
138
+ app = lambda do |env|
139
+ [210, {}, ['Hello from downstream']]
140
+ end
141
+
142
+ app_class = mock_app_class do
143
+ get '/' do
144
+ 'hello'
145
+ end
146
+ end
147
+
148
+ frankie = app_class.new(app)
149
+ req = Rack::MockRequest.new frankie
150
+ res = req.get '/'
151
+ res.body.should == 'hello'
152
+
153
+ res2 = req.get '/ither'
154
+ res2.body.should == 'Hello from downstream'
155
+ end
156
+
157
+ it 'should support adding after filers' do
158
+ app = mock_app do
159
+ after do
160
+ response.should_not == nil
161
+ end
162
+
163
+ get '/' do
164
+ "hello"
165
+ end
166
+ end
167
+ app.get '/'
168
+ end
169
+
170
+ describe 'cookies' do
171
+ let (:app) do
172
+ mock_app do
173
+ post '/cookie' do
174
+ cookies['foo'] = 'bar'
175
+ end
176
+
177
+ delete '/cookie' do
178
+ cookies.delete 'foo'
179
+ end
180
+ end
181
+ end
182
+
183
+ it 'sets a cookie' do
184
+ res = app.post '/cookie'
185
+ res.headers['Set-Cookie'].should == 'foo=bar; path=/'
186
+ end
187
+
188
+ it 'deletes a cookie' do
189
+ app.post '/cookie'
190
+ res = app.delete '/cookie'
191
+ res.headers['Set-Cookie'].should_not include('foo=bar')
192
+ end
193
+ end
194
+
195
+ it 'works with empty path' do
196
+ kls = mock_app_class do
197
+ get '/' do
198
+ 'Hello'
199
+ end
200
+ end
201
+
202
+ env = Rack::MockRequest.env_for '/'
203
+ env['PATH_INFO'] = ''
204
+ kls.new.call(env)[2].body.first.should == 'Hello'
205
+ end
206
+
207
+ describe 'Class level api' do
208
+ let (:app_class) { Class.new(App) }
209
+
210
+ describe 'middlewares' do
211
+
212
+ it 'delegates to builder' do
213
+ kls = mock_app_class
214
+ kls.builder.should_receive(:use).with(NullMiddleware)
215
+ kls.use(NullMiddleware)
216
+ end
217
+ end
218
+
219
+ describe 'helpers' do
220
+ it 'should allow to include a helper in request scope' do
221
+ app_class.helpers NullHelper
222
+ app_class.scope_class.ancestors.should include(NullHelper)
223
+ end
224
+
225
+ it 'should allow to include multiple helpers modules' do
226
+ module NullHelper2
227
+ end
228
+
229
+ app_class.helpers NullHelper, NullHelper2
230
+ app_class.scope_class.ancestors.should include(NullHelper, NullHelper2)
231
+ end
232
+
233
+ it 'should allow to define helpers with a block' do
234
+ app_class.helpers do
235
+ def foo
236
+ 'bar'
237
+ end
238
+ end
239
+
240
+ app_class.get '/' do
241
+ foo.should == 'bar'
242
+ end
243
+ req = Rack::MockRequest.env_for '/'
244
+ res = app_class.new.call(req)
245
+ end
246
+ end
247
+ end
248
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe NYNY::App do
4
+ describe 'inheritance' do
5
+ class Parent < NYNY::App
6
+ helpers do
7
+ def parent_helper; :parent; end
8
+ end
9
+
10
+ before do
11
+ headers['parent before'] = 'true'
12
+ end
13
+
14
+ after do
15
+ headers['parent after'] = 'true'
16
+ end
17
+
18
+ get '/helpers' do
19
+ parent_helper.should == :parent
20
+ end
21
+
22
+ get '/parent' do
23
+ 'parent'
24
+ end
25
+ end
26
+
27
+ class Child < Parent
28
+ helpers do
29
+ def child_helper; :child; end
30
+ end
31
+
32
+ before do
33
+ headers['child before'] = 'true'
34
+ end
35
+
36
+ after do
37
+ headers['child after'] = 'true'
38
+ end
39
+
40
+ get '/helpers' do
41
+ child_helper.should == :child
42
+ end
43
+
44
+ get '/child' do
45
+ 'child'
46
+ end
47
+ end
48
+
49
+ let (:parent) { Rack::MockRequest.new Parent.new }
50
+ let (:child) { Rack::MockRequest.new Child.new }
51
+
52
+ it 'works correctly for routes' do
53
+ parent.get('/parent').body.should == 'parent'
54
+ parent.get('/child').status.should == 404
55
+ child.get('/parent').body.should == 'parent'
56
+ child.get('/child').body.should == 'child'
57
+ end
58
+
59
+ it 'works correctly for before filters' do
60
+ parent.get('/parent').headers['child before'].should be_nil
61
+ child.get('/parent').headers['child before'].should_not be_nil
62
+ child.get('/parent').headers['parent before'].should_not be_nil
63
+ end
64
+
65
+ it 'works correctly for after filters' do
66
+ parent.get('/parent').headers['child after'].should be_nil
67
+ child.get('/parent').headers['child after'].should_not be_nil
68
+ child.get('/parent').headers['parent after'].should_not be_nil
69
+ end
70
+
71
+ it 'works correctly for helpers' do
72
+ parent.get('/helpers')
73
+ child.get('/helpers')
74
+ end
75
+ end
76
+ end