goliath 0.9.4 → 1.0.0.beta.1

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

Potentially problematic release.


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

Files changed (102) hide show
  1. data/.gitignore +3 -0
  2. data/Guardfile +8 -0
  3. data/HISTORY.md +10 -0
  4. data/LICENSE +1 -1
  5. data/README.md +28 -29
  6. data/Rakefile +10 -2
  7. data/examples/activerecord/config/srv.rb +2 -1
  8. data/examples/activerecord/srv.rb +14 -5
  9. data/examples/api_proxy.rb +3 -7
  10. data/examples/around.rb +38 -0
  11. data/examples/async_aroundware_demo.rb +2 -2
  12. data/examples/async_upload.rb +1 -1
  13. data/examples/auth_and_rate_limit.rb +1 -1
  14. data/examples/clone.rb +26 -0
  15. data/examples/config/websocket.rb +1 -0
  16. data/examples/custom_logs.rb +21 -0
  17. data/examples/custom_server.rb +6 -44
  18. data/examples/early_abort.rb +6 -3
  19. data/examples/echo.rb +1 -1
  20. data/examples/fiber_pool.rb +35 -0
  21. data/examples/grape/config/apiserver.rb +8 -0
  22. data/examples/grape/server.rb +67 -0
  23. data/examples/gziped.rb +1 -1
  24. data/examples/params.rb +36 -0
  25. data/examples/rasterize/rasterize.rb +1 -2
  26. data/examples/router.rb +15 -0
  27. data/examples/template.rb +14 -7
  28. data/examples/test.rb +31 -0
  29. data/examples/upload.rb +17 -0
  30. data/examples/views/joke.markdown +4 -4
  31. data/examples/websocket.rb +39 -0
  32. data/examples/ws/favicon.ico +0 -0
  33. data/examples/ws/index.erb +50 -0
  34. data/goliath.gemspec +24 -6
  35. data/lib/goliath/api.rb +15 -82
  36. data/lib/goliath/application.rb +3 -17
  37. data/lib/goliath/connection.rb +16 -9
  38. data/lib/goliath/constants.rb +2 -0
  39. data/lib/goliath/env.rb +2 -3
  40. data/lib/goliath/goliath.rb +24 -34
  41. data/lib/goliath/plugins/latency.rb +7 -3
  42. data/lib/goliath/rack/builder.rb +1 -37
  43. data/lib/goliath/rack/favicon.rb +31 -0
  44. data/lib/goliath/rack/formatters/json.rb +1 -1
  45. data/lib/goliath/rack/heartbeat.rb +1 -0
  46. data/lib/goliath/rack/jsonp.rb +1 -0
  47. data/lib/goliath/rack/params.rb +2 -17
  48. data/lib/goliath/rack/render.rb +0 -1
  49. data/lib/goliath/rack/templates.rb +18 -7
  50. data/lib/goliath/rack/types/base.rb +24 -0
  51. data/lib/goliath/rack/types/boolean.rb +18 -0
  52. data/lib/goliath/rack/types/core.rb +19 -0
  53. data/lib/goliath/rack/types/symbol.rb +16 -0
  54. data/lib/goliath/rack/types.rb +10 -0
  55. data/lib/goliath/rack/validation/coerce.rb +48 -0
  56. data/lib/goliath/rack/validation/param.rb +113 -0
  57. data/lib/goliath/rack/validation/request_method.rb +1 -1
  58. data/lib/goliath/rack/validation/required.rb +47 -0
  59. data/lib/goliath/rack/validation/required_param.rb +42 -17
  60. data/lib/goliath/rack/validation.rb +3 -0
  61. data/lib/goliath/rack.rb +2 -2
  62. data/lib/goliath/request.rb +41 -9
  63. data/lib/goliath/response.rb +0 -2
  64. data/lib/goliath/runner.rb +55 -4
  65. data/lib/goliath/server.rb +7 -3
  66. data/lib/goliath/test_helper.rb +70 -16
  67. data/lib/goliath/test_helper_ws.rb +42 -0
  68. data/lib/goliath/version.rb +1 -1
  69. data/lib/goliath/websocket.rb +80 -0
  70. data/pkg/goliath-0.9.4.gem +0 -0
  71. data/pkg/goliath-1.0.0.beta.1.gem +0 -0
  72. data/spec/integration/async_request_processing.rb +1 -1
  73. data/spec/integration/early_abort_spec.rb +3 -10
  74. data/spec/integration/echo_spec.rb +8 -8
  75. data/spec/integration/jsonp_spec.rb +31 -0
  76. data/spec/integration/keepalive_spec.rb +2 -2
  77. data/spec/integration/template_spec.rb +10 -5
  78. data/spec/integration/test_helper_spec.rb +33 -0
  79. data/spec/integration/valid_spec.rb +35 -5
  80. data/spec/integration/websocket_spec.rb +44 -0
  81. data/spec/unit/api_spec.rb +2 -19
  82. data/spec/unit/connection_spec.rb +3 -0
  83. data/spec/unit/rack/formatters/json_spec.rb +3 -3
  84. data/spec/unit/rack/heartbeat_spec.rb +13 -0
  85. data/spec/unit/rack/params_spec.rb +2 -8
  86. data/spec/unit/rack/validation/param_spec.rb +443 -0
  87. data/spec/unit/rack/validation/request_method_spec.rb +5 -0
  88. data/spec/unit/rack/validation/required_param_spec.rb +71 -1
  89. data/spec/unit/runner_spec.rb +21 -7
  90. data/test/echo_test.rb +25 -0
  91. data/test/test_helper.rb +5 -0
  92. metadata +316 -78
  93. data/examples/env_use_statements.rb +0 -20
  94. data/examples/favicon.rb +0 -40
  95. data/examples/rack_routes.rb +0 -125
  96. data/examples/rasterize/thumb/f7ad4cb03e5bfd0e2c43db8e598fb3cd.png +0 -0
  97. data/examples/valid.rb +0 -17
  98. data/lib/goliath/deprecated/async_aroundware.rb +0 -133
  99. data/lib/goliath/deprecated/mongo_receiver.rb +0 -84
  100. data/lib/goliath/deprecated/response_receiver.rb +0 -97
  101. data/spec/integration/rack_routes_spec.rb +0 -169
  102. data/spec/unit/rack/builder_spec.rb +0 -40
@@ -0,0 +1,443 @@
1
+ require 'spec_helper'
2
+ require 'goliath/rack/validation/param'
3
+
4
+ describe Goliath::Rack::Validation::Param do
5
+ before do
6
+ @app = mock('app').as_null_object
7
+ @env = {'params' => {}}
8
+ end
9
+
10
+ it "should not allow invalid options" do
11
+ lambda {
12
+ Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => Class.new})
13
+ }.should raise_error
14
+ end
15
+
16
+ it "raises if key is not supplied" do
17
+ lambda {
18
+ Goliath::Rack::Validation::Param.new(@app)
19
+ }.should raise_error(Exception)
20
+ end
21
+
22
+ it "uses a default value if optional is not supplied" do
23
+ cv = Goliath::Rack::Validation::Param.new(@app, :key => 'key')
24
+ cv.optional.should be_false
25
+ end
26
+
27
+ it "should have default and message be optional" do
28
+ cv = nil
29
+ lambda {
30
+ cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'flag',
31
+ :as => Goliath::Rack::Types::Boolean})
32
+ }.should_not raise_error
33
+
34
+ cv.default.should be_nil
35
+ cv.message.should_not be_nil
36
+ end
37
+
38
+ it "should fail if given an invalid option" do
39
+ cv = nil
40
+ lambda {
41
+ cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'flag',
42
+ :as => Goliath::Rack::Types::Boolean, :animal => :monkey})
43
+ }.should raise_error
44
+ end
45
+
46
+ context "fetch_key" do
47
+ before do
48
+ @cv = Goliath::Rack::Validation::Param.new(@app,
49
+ :key => ['data', 'credentials', 'login'])
50
+ end
51
+
52
+ it "should return a valid value given the correct params" do
53
+ params = {
54
+ 'data' => {
55
+ 'credentials' => {
56
+ 'login' => "mike"
57
+ }
58
+ }
59
+ }
60
+ @cv.fetch_key(params).should == "mike"
61
+ end
62
+
63
+ it "should return nil given an incorrect params" do
64
+ params = {
65
+ 'data' => {
66
+ 'credentials' => {
67
+ 'login2' => "mike"
68
+ }
69
+ }
70
+ }
71
+ @cv.fetch_key(params).should be_nil
72
+ end
73
+
74
+ it "should set value if given" do
75
+ params = {
76
+ 'data' => {
77
+ 'credentials' => {
78
+ 'login' => "mike"
79
+ }
80
+ }
81
+ }
82
+ @cv.fetch_key(params, "tim").should == "tim"
83
+ params['data']['credentials']['login'].should == "tim"
84
+ end
85
+ end
86
+
87
+ context "Required" do
88
+
89
+ it 'defaults type and message' do
90
+ @rp = Goliath::Rack::Validation::Param.new('app', :key => 'key')
91
+ @rp.type.should_not be_nil
92
+ @rp.type.should_not =~ /^\s*$/
93
+ @rp.message.should == 'identifier missing'
94
+ end
95
+
96
+
97
+ context 'with middleware' do
98
+ before(:each) do
99
+ @app = mock('app').as_null_object
100
+ @env = {'params' => {}}
101
+ @rp = Goliath::Rack::Validation::Param.new(@app, {:type => 'Monkey',
102
+ :key => 'mk', :message => 'is required'})
103
+ end
104
+
105
+ it 'stores type and key options' do
106
+ @rp.type.should == 'Monkey'
107
+ @rp.key.should == 'mk'
108
+ end
109
+
110
+ it 'calls validation_error with a custom message' do
111
+ @rp.should_receive(:validation_error).with(anything, 'Monkey is required')
112
+ @rp.call(@env)
113
+ end
114
+
115
+ it 'returns the app status, headers and body' do
116
+ app_headers = {'Content-Type' => 'app'}
117
+ app_body = {'b' => 'c'}
118
+ @app.should_receive(:call).and_return([201, app_headers, app_body])
119
+
120
+ @env['params']['mk'] = 'monkey'
121
+
122
+ status, headers, body = @rp.call(@env)
123
+ status.should == 201
124
+ headers.should == app_headers
125
+ body.should == app_body
126
+ end
127
+
128
+ context 'key_valid?' do
129
+ it 'raises exception if the key is not provided' do
130
+ @rp.key_valid?(@env['params']).should be_false
131
+ end
132
+
133
+ it 'raises exception if the key is blank' do
134
+ @env['params']['mk'] = ''
135
+ @rp.key_valid?(@env['params']).should be_false
136
+ end
137
+
138
+ it 'raises exception if the key is nil' do
139
+ @env['params']['mk'] = nil
140
+ @rp.key_valid?(@env['params']).should be_false
141
+ end
142
+
143
+ it 'handles an empty array' do
144
+ @env['params']['mk'] = []
145
+ @rp.key_valid?(@env['params']).should be_false
146
+ end
147
+
148
+ it 'handles an array of nils' do
149
+ @env['params']['mk'] = [nil, nil, nil]
150
+ @rp.key_valid?(@env['params']).should be_false
151
+ end
152
+
153
+ it 'handles an array of blanks' do
154
+ @env['params']['mk'] = ['', '', '']
155
+ @rp.key_valid?(@env['params']).should be_false
156
+ end
157
+
158
+ it "doesn't raise if the key provided" do
159
+ @env['params']['mk'] = 'my value'
160
+ @rp.key_valid?(@env['params']).should be_true
161
+ end
162
+
163
+ it "doesn't raise if the array contains valid data" do
164
+ @env['params']['mk'] = [1, 2, 3, 4]
165
+ @rp.key_valid?(@env['params']).should be_true
166
+ end
167
+
168
+ it "doesn't raise if the key provided is multiline and has blanks" do
169
+ @env['params']['mk'] = "my\n \nvalue"
170
+ @rp.key_valid?(@env['params']).should be_true
171
+ end
172
+
173
+ it "doesn't raise if the key provided is an array and contains multiline with blanks" do
174
+ @env['params']['mk'] = ["my\n \nvalue", "my\n \nother\n \nvalue"]
175
+ @rp.key_valid?(@env['params']).should be_true
176
+ end
177
+ end
178
+ end
179
+
180
+ context 'Nested keys tests' do
181
+ before do
182
+ @app = mock('app').as_null_object
183
+ @env = {'params' => {}}
184
+ @rp = Goliath::Rack::Validation::Param.new(@app, :type => 'Monkey',
185
+ :key => ['data', 'credentials', 'login'],
186
+ :message => 'is required')
187
+ end
188
+
189
+ it "return false if key's missing" do
190
+ @env['params'] = {'data' => {
191
+ 'credentials' => {
192
+ 'login2' => "user",
193
+ 'pass' => "password"}
194
+ }
195
+ }
196
+
197
+ @rp.key_valid?(@env['params']).should be_false
198
+ end
199
+
200
+ it "return true if key is present" do
201
+ @env['params'] = {'data' => {
202
+ 'credentials' => {
203
+ 'login' => "user",
204
+ 'pass' => "password"}
205
+ }
206
+ }
207
+
208
+ @rp.key_valid?(@env['params']).should be_true
209
+ end
210
+ end
211
+
212
+ context 'Nested keys tests (with string)' do
213
+ before do
214
+ @app = mock('app').as_null_object
215
+ @env = {'params' => {}}
216
+ @rp = Goliath::Rack::Validation::Param.new(@app, :type => 'Monkey',
217
+ :key => 'data.credentials.login', :message => 'is required')
218
+ end
219
+
220
+ it "return false if key's missing" do
221
+ @env['params'] = {'data' => {
222
+ 'credentials' => {
223
+ 'login2' => "user",
224
+ 'pass' => "password"}
225
+ }
226
+ }
227
+
228
+ @rp.key_valid?(@env['params']).should be_false
229
+ end
230
+
231
+ it "return true if key is present" do
232
+ @env['params'] = {'data' => {
233
+ 'credentials' => {
234
+ 'login' => "user",
235
+ 'pass' => "password"}
236
+ }
237
+ }
238
+
239
+ @rp.key_valid?(@env['params']).should be_true
240
+ end
241
+ end
242
+
243
+ end
244
+
245
+ context "Coerce" do
246
+
247
+ it "should only accept a class in the :as" do
248
+ lambda {
249
+ Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => "not a class"})
250
+ }.should raise_error
251
+ end
252
+
253
+ context 'with middleware' do
254
+ {
255
+ Goliath::Rack::Types::Boolean => [['t', true], ['true', true], ['f', false],
256
+ ['false', false], ['1', true],
257
+ ['0', false], ['TRUE', true], ['FALSE', false],
258
+ ['T', true], ['F', false]
259
+ ],
260
+ Goliath::Rack::Types::Integer => [['24', 24]],
261
+ Goliath::Rack::Types::Float => [['24.3', 24.3]],
262
+ Goliath::Rack::Types::Symbol => [['hi', :hi]],
263
+ }.each do |type, values|
264
+ values.each do |value|
265
+ it "should coerce #{type} from #{value.first} to #{value[1]}" do
266
+ @env['params']['user'] = value.first
267
+ cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => type})
268
+ cv.call(@env)
269
+ @env['params']['user'].should == value[1]
270
+ end
271
+ end
272
+ end
273
+
274
+ {
275
+ Goliath::Rack::Types::Boolean => ["235", "hi", "3"],
276
+ Goliath::Rack::Types::Integer => ["hi", "false", "true"],
277
+ Goliath::Rack::Types::Symbol => [nil],
278
+ Goliath::Rack::Types::Float => [nil],
279
+ }.each do |type, values|
280
+ values.each do |value|
281
+ it "should not coerce #{type} with #{value}" do
282
+ @env['params']['user'] = value
283
+ cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'user', :as => type})
284
+ result = cv.call(@env)
285
+ result.should be_an_instance_of(Array)
286
+ result.first.should == 400
287
+ result.last.should have_key(:error)
288
+ end
289
+ end
290
+ end
291
+
292
+ it "should not fail with a invalid input, given a default value" do
293
+ cv = nil
294
+ @env['params']['user'] = "boo"
295
+ lambda {
296
+ cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'user',
297
+ :as => Goliath::Rack::Types::Boolean , :default => 'default'})
298
+ }.should_not raise_error
299
+ @env['params']['user'] = 'default'
300
+ end
301
+
302
+ it "should be able to take a custom fail message" do
303
+ @env['params']['user'] = "boo"
304
+ cv = Goliath::Rack::Validation::Param.new(@app, {:key => 'user',
305
+ :as => Goliath::Rack::Types::Integer, :message => "custom message"})
306
+
307
+ result = cv.call(@env)
308
+ result.should be_an_instance_of(Array)
309
+ result.first.should == 400
310
+ result.last.should have_key(:error)
311
+ result.last[:error].should == "custom message"
312
+ end
313
+ end
314
+
315
+ end
316
+
317
+ context "Integration" do
318
+ it "should do required param + coerce (not nested)" do
319
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login",
320
+ :as => Goliath::Rack::Types::Integer
321
+ @env['params']['login'] = "3"
322
+ cv.call(@env)
323
+ @env['params']['login'].should == 3
324
+
325
+ @env['params']['login'] = nil
326
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login",
327
+ :as => Goliath::Rack::Types::Integer
328
+ result = cv.call(@env)
329
+ result.should be_an_instance_of(Array)
330
+ result.first.should == 400
331
+ result.last.should have_key(:error)
332
+ end
333
+
334
+ it "should do required param + coerce (nested)" do
335
+ cv = Goliath::Rack::Validation::Param.new @app, :key => ['login', 'other'],
336
+ :as => Goliath::Rack::Types::Integer
337
+ @env['params']['login'] = {}
338
+ @env['params']['login']['other'] = "3"
339
+ cv.call(@env)
340
+ @env['params']['login']['other'].should == 3
341
+
342
+ @env['params']['login'] = {}
343
+ @env['params']['login']['other'] = nil
344
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login.other",
345
+ :as => Goliath::Rack::Types::Integer
346
+ result = cv.call(@env)
347
+ result.should be_an_instance_of(Array)
348
+ result.first.should == 400
349
+ result.last.should have_key(:error)
350
+ end
351
+
352
+ it "should do required param + not coerce (not nested)" do
353
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login"
354
+ @env['params']['login'] = "3"
355
+ cv.call(@env)
356
+ @env['params']['login'].should == "3"
357
+
358
+ @env['params']['login'] = nil
359
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login"
360
+ result = cv.call(@env)
361
+ result.should be_an_instance_of(Array)
362
+ result.first.should == 400
363
+ result.last.should have_key(:error)
364
+ end
365
+
366
+ it "should do required param + not coerce (nested)" do
367
+ cv = Goliath::Rack::Validation::Param.new @app, :key => ['login', 'other']
368
+ @env['params']['login'] = {}
369
+ @env['params']['login']['other'] = "3"
370
+ cv.call(@env)
371
+ @env['params']['login']['other'].should == "3"
372
+
373
+ @env['params']['login'] = {}
374
+ @env['params']['login']['other'] = nil
375
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login.other"
376
+ result = cv.call(@env)
377
+ result.should be_an_instance_of(Array)
378
+ result.first.should == 400
379
+ result.last.should have_key(:error)
380
+ end
381
+
382
+ it "should do optional param + coerce (not nested)" do
383
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login",
384
+ :as => Goliath::Rack::Types::Integer, :optional => true
385
+ @env['params']['login'] = "3"
386
+ cv.call(@env)
387
+ @env['params']['login'].should == 3
388
+
389
+ @env['params']['login'] = nil
390
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login",
391
+ :as => Goliath::Rack::Types::Integer, :optional => true
392
+ result = cv.call(@env)
393
+ result.should_not be_an_instance_of(Array) #implying its OK
394
+ end
395
+
396
+ it "should do optional param + coerce (nested)" do
397
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login.other",
398
+ :as => Goliath::Rack::Types::Integer, :optional => true
399
+ @env['params']['login'] = {}
400
+ @env['params']['login']['other'] = "3"
401
+ cv.call(@env)
402
+ @env['params']['login']['other'].should == 3
403
+
404
+ @env['params']['login'] = {}
405
+ @env['params']['login']['other'] = nil
406
+ cv = Goliath::Rack::Validation::Param.new @app, :key => ['login', 'other'],
407
+ :as => Goliath::Rack::Types::Integer, :optional => true
408
+ result = cv.call(@env)
409
+ result.should_not be_an_instance_of(Array) #implying its OK
410
+ end
411
+
412
+ it "should do optional param and not coerce (not nested)" do
413
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login",
414
+ :optional => true
415
+ @env['params']['login'] = "3"
416
+ cv.call(@env)
417
+ @env['params']['login'].should == "3"
418
+
419
+ @env['params']['login'] = nil
420
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login",
421
+ :optional => true
422
+ result = cv.call(@env)
423
+ result.should_not be_an_instance_of(Array) #implying its OK
424
+ end
425
+
426
+ it "should do optional param and not coerce (nested)" do
427
+ cv = Goliath::Rack::Validation::Param.new @app, :key => ['login', 'other'],
428
+ :optional => true
429
+ @env['params']['login'] = {}
430
+ @env['params']['login']['other'] = "3"
431
+ cv.call(@env)
432
+ @env['params']['login']['other'].should == "3"
433
+
434
+ @env['params']['login'] = {}
435
+ @env['params']['login']['other'] = nil
436
+ cv = Goliath::Rack::Validation::Param.new @app, :key => "login.other",
437
+ :optional => true
438
+ result = cv.call(@env)
439
+ result.should_not be_an_instance_of(Array) #implying its OK
440
+ end
441
+ end
442
+ end
443
+
@@ -44,4 +44,9 @@ describe Goliath::Rack::Validation::RequestMethod do
44
44
  rm = Goliath::Rack::Validation::RequestMethod.new('my app', ['GET', 'DELETE', 'HEAD'])
45
45
  rm.methods.should == ['GET', 'DELETE', 'HEAD']
46
46
  end
47
+
48
+ it 'accepts string method on initialize' do
49
+ rm = Goliath::Rack::Validation::RequestMethod.new('my app', 'GET')
50
+ rm.methods.should == ['GET']
51
+ end
47
52
  end
@@ -100,7 +100,77 @@ describe Goliath::Rack::Validation::RequiredParam do
100
100
  it "doesn't raise if the key provided is an array and contains multiline with blanks" do
101
101
  @env['params']['mk'] = ["my\n \nvalue", "my\n \nother\n \nvalue"]
102
102
  @rp.key_valid?(@env['params']).should be_true
103
- end
103
+ end
104
+ end
105
+ end
106
+
107
+ describe 'Nested keys tests' do
108
+ before do
109
+ @app = mock('app').as_null_object
110
+ @env = {'params' => {}}
111
+ @rp = Goliath::Rack::Validation::RequiredParam.new(@app,
112
+ :type => 'Monkey',
113
+ :key => ['data', 'credentials', 'login'],
114
+ :message => 'is required'
115
+ )
116
+ end
117
+
118
+ it "return false if key's missing" do
119
+ @env['params'] = {'data' => {
120
+ 'credentials' => {
121
+ 'login2' => "user",
122
+ 'pass' => "password"}
123
+ }
124
+ }
125
+
126
+ @rp.key_valid?(@env['params']).should be_false
127
+ end
128
+
129
+ it "return true if key is present" do
130
+ @env['params'] = {'data' => {
131
+ 'credentials' => {
132
+ 'login' => "user",
133
+ 'pass' => "password"}
134
+ }
135
+ }
136
+
137
+ @rp.key_valid?(@env['params']).should be_true
138
+ end
139
+ end
140
+
141
+ describe 'Nested keys tests (with string)' do
142
+ before do
143
+ @app = mock('app').as_null_object
144
+ @env = {'params' => {}}
145
+ @rp = Goliath::Rack::Validation::RequiredParam.new(@app,
146
+ :type => 'Monkey',
147
+ :key => 'data.credentials.login',
148
+ :message => 'is required'
149
+ )
150
+ end
151
+
152
+ it "return false if key's missing" do
153
+ @env['params'] = {'data' => {
154
+ 'credentials' => {
155
+ 'login2' => "user",
156
+ 'pass' => "password"}
157
+ }
158
+ }
159
+
160
+ @rp.key_valid?(@env['params']).should be_false
161
+ end
162
+
163
+ it "return true if key is present" do
164
+ @env['params'] = {'data' => {
165
+ 'credentials' => {
166
+ 'login' => "user",
167
+ 'pass' => "password"}
168
+ }
169
+ }
170
+
171
+ @rp.key_valid?(@env['params']).should be_true
104
172
  end
105
173
  end
174
+
175
+
106
176
  end
@@ -10,13 +10,6 @@ describe Goliath::Runner do
10
10
  @r.stub!(:setup_logger).and_return(@log_mock)
11
11
  end
12
12
 
13
- after(:each) do
14
- # Runner default env is development.
15
- # We do need to revert to test
16
- Goliath.env = :test
17
- end
18
-
19
-
20
13
  describe 'server execution' do
21
14
  describe 'daemonization' do
22
15
  it 'daemonizes if specified' do
@@ -140,3 +133,24 @@ describe Goliath::Runner do
140
133
  end
141
134
  end
142
135
  end
136
+
137
+ describe Goliath::EnvironmentParser do
138
+ before(:each) do
139
+ ENV['RACK_ENV'] = nil
140
+ end
141
+
142
+ it 'returns the default environment if no other options are set' do
143
+ Goliath::EnvironmentParser.parse.should == Goliath::DEFAULT_ENV
144
+ end
145
+
146
+ it 'gives precendence to RACK_ENV over the default' do
147
+ ENV['RACK_ENV'] = 'rack_env'
148
+ Goliath::EnvironmentParser.parse.should == :rack_env
149
+ end
150
+
151
+ it 'gives precendence to command-line flag over RACK_ENV' do
152
+ ENV['RACK_ENV'] = 'rack_env'
153
+ args = %w{ -e flag_env }
154
+ Goliath::EnvironmentParser.parse(args).should == :flag_env
155
+ end
156
+ end
data/test/echo_test.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class Echo < Goliath::API
4
+ use Goliath::Rack::Params
5
+
6
+ def response(env)
7
+ [200, {}, env.params['echo']]
8
+ end
9
+ end
10
+
11
+ class EchoTest < Test::Unit::TestCase
12
+ include Goliath::TestHelper
13
+
14
+ def setup
15
+ @err = Proc.new { assert false, "API request failed" }
16
+ end
17
+
18
+ def test_query_is_echoed_back
19
+ with_api(Echo) do
20
+ get_request({:query => {:echo => 'test'}}, @err) do |c|
21
+ assert_equal 'test', c.response
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+
2
+ require 'test/unit'
3
+ require 'goliath/test_helper'
4
+
5
+ Goliath.env = :test