plezi 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,14 +11,14 @@ Root ||= Pathname.new(File.dirname(__FILE__)).expand_path
11
11
  Dir.chdir Root.to_s
12
12
 
13
13
  # ensure development mode? (comment before production, environment dependent)
14
- ENV["RACK_ENV"] ||= "development"
14
+ ENV['ENV'] ||= ENV["RACK_ENV"] ||= "development"
15
15
 
16
16
  # save the process id (pid) to file - notice Heroku doesn't allow to write files.
17
17
  (IO.write File.expand_path(File.join 'tmp','pid'), Process.pid unless ENV["DYNO"]) rescue true
18
18
 
19
19
  # using bundler to load gems (including the plezi gem)
20
20
  require 'bundler'
21
- Bundler.require
21
+ Bundler.require(:default, ENV['ENV'].to_s.to_sym)
22
22
 
23
23
  # require tilt/sass in a thread safe way (before multi-threading cycle begins)
24
24
  require 'tilt/sass' if defined?(::Slim) && defined?(::Sass)
@@ -16,8 +16,8 @@
16
16
  # #
17
17
  # require 'plezi/oauth'
18
18
  # #
19
- # # Last, but not least, remember to add the authentication route in the correct placement using:
20
- # create_auth_shared_route do |service_name, remote_user_id, remote_user_email, remote_response|
19
+ # # Last, but not least, remember to add the authentication route in the 'routes.rb' (remember path priority placement):
20
+ # create_auth_shared_route do |service_name, auth_token, remote_user_id, remote_user_email, remote_response|
21
21
  # # ...callback for authentication.
22
22
  # # This callback should return the app user object or false
23
23
  # # This callback hass access to the magic controller methods (request, cookies, etc')
data/resources/rakefile CHANGED
@@ -5,8 +5,8 @@ load ::File.expand_path(File.join("..", "environment.rb"), __FILE__)
5
5
  NO_PLEZI_AUTO_START = true
6
6
 
7
7
  # create a default task
8
- desc "The default task does nothing... it's waiting for you to tell it what to do."
8
+ desc "The default task will simply remind you to call 'rake -T'."
9
9
  task :default do
10
- puts "I don't know what to do."
11
- puts `rake -T`
10
+ puts "I don't know what to do. Please choose a task..."
11
+ puts "You can print a list of all avaiulable tasks using: rake -T"
12
12
  end
data/test/plezi_tests.rb CHANGED
@@ -4,14 +4,31 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'open-uri'
5
5
  require 'plezi'
6
6
 
7
+ def report_before_filter(result= true)
8
+ return true if $before_tested
9
+ puts(" * Before filter test: #{PleziTestTasks::RESULTS[result]}")
10
+ $before_tested = true
11
+ true
12
+ end
13
+ def report_after_filter(result= true)
14
+ return true if $after_tested
15
+ puts(" * After filter test: #{PleziTestTasks::RESULTS[result]}")
16
+ $after_tested = true
17
+ true
18
+ end
19
+
7
20
  class TestCtrl
8
21
 
22
+
23
+
9
24
  # this will be called before every request.
10
25
  def before
26
+ report_before_filter
11
27
  end
12
28
 
13
29
  # this will be called after every request.
14
30
  def after
31
+ report_after_filter
15
32
  end
16
33
 
17
34
  # shouldn't be available (return 404).
@@ -21,7 +38,15 @@ class TestCtrl
21
38
  def index
22
39
  "test"
23
40
  end
41
+ def headers
42
+ "HTTP request: #{request[:method]} #{request[:query]} - version: #{request[:version]}\n" + (request.headers.map {|k, v| "#{k}: #{v}"} .join "\n")
43
+ end
24
44
 
45
+ # returns the url used to access this method
46
+ def my_url
47
+ dest = params.dup
48
+ url_for dest
49
+ end
25
50
  # should return a 500 internal server error message.
26
51
  def fail
27
52
  raise "Hell!"
@@ -103,19 +128,23 @@ end
103
128
  module PleziTestTasks
104
129
  module_function
105
130
 
106
- RESULTS = {true => "passed", false => 'FAILED!'}
131
+
132
+ RESULTS = {true => "\e[32mpassed\e[0m"}
133
+ RESULTS.default = "\e[31mFAILED!\e[0m"
107
134
 
108
135
  def run_tests
109
136
  (public_methods(false)).each {|m| method(m).call if m.to_s.match /^test_/}
137
+ report_before_filter false
138
+ report_after_filter false
110
139
  true
111
140
  end
112
141
  def test_sleep
113
142
  Plezi.run_async do
114
143
  begin
115
- puts "Sleeper test: #{RESULTS[URI.parse("http://localhost:3000/sleeper").read == 'slept']}"
116
- puts "ASync tasks test: #{RESULTS[true]}"
144
+ puts " * Sleeper test: #{RESULTS[URI.parse("http://localhost:3000/sleeper").read == 'slept']}"
145
+ puts " * ASync tasks test: #{RESULTS[true]}"
117
146
  rescue => e
118
- puts "Sleeper test FAILED TO RUN!!!"
147
+ puts " **** Sleeper test FAILED TO RUN!!!"
119
148
  puts e
120
149
  end
121
150
  end
@@ -123,84 +152,126 @@ module PleziTestTasks
123
152
 
124
153
  def test_index
125
154
  begin
126
- puts "index test: #{RESULTS[URI.parse("http://localhost:3000/").read == 'test']}"
155
+ puts " * Index test: #{RESULTS[URI.parse("http://localhost:3000/").read == 'test']}"
127
156
  rescue => e
128
- puts "Index test FAILED TO RUN!!!"
157
+ puts " **** Index test FAILED TO RUN!!!"
129
158
  puts e
130
159
  end
131
160
  end
132
161
  def test_ssl
133
- puts "Connection to non-ssl and unique route test: #{RESULTS[URI.parse("http://localhost:3000/ssl").read == 'false']}"
162
+ puts " * Connection to non-ssl and unique route test: #{RESULTS[URI.parse("http://localhost:3000/ssl").read == 'false']}"
134
163
  uri = URI.parse("https://localhost:3030/ssl")
135
164
  Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == "https"), verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
136
- puts "Connection to ssl and unique ssl route test: #{RESULTS[ http.request(Net::HTTP::Get.new(uri)).body == 'true' ]}"
165
+ puts " * Connection to ssl and unique ssl route test: #{RESULTS[ http.request(Net::HTTP::Get.new(uri)).body == 'true' ]}"
137
166
  end
138
167
  rescue => e
139
- puts "SSL Tests FAILED to complete!!!"
168
+ puts " **** SSL Tests FAILED to complete!!!"
140
169
  puts e
141
170
  end
142
171
  def test_new
143
- puts "New RESTful path test: #{RESULTS[URI.parse("http://localhost:3000/new").read == 'new']}"
172
+ puts " * New RESTful path test: #{RESULTS[URI.parse("http://localhost:3000/new").read == 'new']}"
144
173
 
145
174
  rescue => e
146
- puts "New RESTful path test FAILED TO RUN!!!"
175
+ puts " **** New RESTful path test FAILED TO RUN!!!"
147
176
  puts e
148
177
  end
149
178
  def test_show
150
- puts "Show RESTful path test: #{RESULTS[URI.parse("http://localhost:3000/3").read == 'show 3']}"
179
+ puts " * Show RESTful path test: #{RESULTS[URI.parse("http://localhost:3000/3").read == 'show 3']}"
151
180
 
152
181
  rescue => e
153
- puts "Show RESTful path test FAILED TO RUN!!!"
182
+ puts " **** Show RESTful path test FAILED TO RUN!!!"
154
183
  puts e
155
184
  end
156
185
  def test_update
157
- puts "Update RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/"), id: 3).body == 'update 3']}"
186
+ puts " * Update RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/"), id: 3).body == 'update 3']}"
158
187
 
159
188
  rescue => e
160
- puts "Update RESTful path test FAILED TO RUN!!!"
189
+ puts " **** Update RESTful path test FAILED TO RUN!!!"
161
190
  puts e
162
191
  end
163
192
  def test_delete
164
- puts "Delete RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/"), id: 3, _method: :delete).body == 'delete 3']}"
193
+ puts " * Delete RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/"), id: 3, _method: :delete).body == 'delete 3']}"
165
194
 
166
195
  rescue => e
167
- puts "Delete RESTful path test FAILED TO RUN!!!"
196
+ puts " **** Delete RESTful path test FAILED TO RUN!!!"
168
197
  puts e
169
198
  end
170
199
  def test_save
171
- puts "Save RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/new"), data: "passed").body == 'passed']}"
200
+ puts " * Save RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/new"), data: "passed").body == 'passed']}"
172
201
 
173
202
  rescue => e
174
- puts "Save RESTful path test FAILED TO RUN!!!"
203
+ puts " **** Save RESTful path test FAILED TO RUN!!!"
175
204
  puts e
176
205
  end
177
206
  def test_streamed
178
207
  begin
179
- puts "Streaming test: #{RESULTS[URI.parse("http://localhost:3000/streamer").read == 'streamed']}"
208
+ puts " * Streaming test: #{RESULTS[URI.parse("http://localhost:3000/streamer").read == 'streamed']}"
180
209
  rescue => e
181
- puts "Streaming test FAILED TO RUN!!!"
210
+ puts " **** Streaming test FAILED TO RUN!!!"
182
211
  puts e
183
212
  end
184
213
  end
214
+ def test_url_for
215
+ test_url = "/some/path/test/my_url/ask/"
216
+ puts " * simple #url_for test: #{RESULTS[URI.parse("http://localhost:3000" + test_url).read == test_url]}"
217
+ test_url = "/some/another_path/my_url/ask/"
218
+ puts " * missing arguments #url_for test: #{RESULTS[URI.parse("http://localhost:3000" + test_url).read == test_url]}"
219
+
220
+ rescue => e
221
+ puts " **** #url_for test FAILED TO RUN!!!"
222
+ puts e
223
+ end
185
224
  def test_404
186
- puts "404 not found and router continuity tests: #{RESULTS[ Net::HTTP.get_response(URI.parse "http://localhost:3000/get404" ).code == '404' ]}"
225
+ puts " * 404 not found and router continuity tests: #{RESULTS[ Net::HTTP.get_response(URI.parse "http://localhost:3000/get404" ).code == '404' ]}"
187
226
 
188
227
  rescue => e
189
- puts "404 not found test FAILED TO RUN!!!"
228
+ puts " **** 404 not found test FAILED TO RUN!!!"
190
229
  puts e
191
230
  end
192
231
  def test_500
193
232
  workers = Plezi::EventMachine.count_living_workers
194
- puts "500 internal error test: #{RESULTS[ Net::HTTP.get_response(URI.parse "http://localhost:3000/fail" ).code == '500' ]}"
233
+ puts " * 500 internal error test: #{RESULTS[ Net::HTTP.get_response(URI.parse "http://localhost:3000/fail" ).code == '500' ]}"
195
234
  # cause 10 more exceptions to be raised... testing thread survival.
196
235
  10.times { Net::HTTP.get_response(URI.parse "http://localhost:3000/fail" ).code }
197
236
  workers_after_test = Plezi::EventMachine.count_living_workers
198
- puts "Worker survival test: #{RESULTS[workers_after_test == workers]} (#{workers_after_test} out of #{workers})"
237
+ puts " * Worker survival test: #{RESULTS[workers_after_test == workers]} (#{workers_after_test} out of #{workers})"
199
238
 
200
239
  rescue => e
201
- puts "404 not found test FAILED TO RUN!!!"
240
+ puts " **** 500 internal error test FAILED TO RUN!!!"
202
241
  puts e
203
242
  end
243
+ def test_websocket
244
+ connection_test = broadcast_test = echo_test = false
245
+ begin
246
+ ws2 = Plezi::WebsocketClient.connect_to("wss://localhost:3030") do |msg|
247
+ break unless @is_connected || !(@is_connected = true)
248
+ puts " * Websocket broadcast message test: #{RESULTS[broadcast_test = (msg == 'echo test')]}"
249
+ response.close
250
+ go_test = false
251
+ end
252
+ puts " * Websocket SSL client test: #{RESULTS[ws2 && true]}"
253
+ ws1 = Plezi::WebsocketClient.connect_to("ws://localhost:3000") do |msg|
254
+ unless @connected
255
+ puts " * Websocket connection message test: #{RESULTS[connection_test = (msg == 'connected')]}"
256
+ @connected = true
257
+ response << "echo test"
258
+ break
259
+ end
260
+ puts " * Websocket echo message test: #{RESULTS[echo_test = (msg == 'echo test')]}"
261
+ response.close
262
+ end
263
+
264
+ rescue => e
265
+ puts " **** Websocket tests FAILED TO RUN!!!"
266
+ puts e.message
267
+ end
268
+ remote = Plezi::WebsocketClient.connect_to("wss://echo.websocket.org/") {|msg| puts " * Extra Websocket Remote test (SSL: echo.websocket.org): #{RESULTS[msg == 'Hello websockets!']}"; response.close}
269
+ remote << "Hello websockets!"
270
+ sleep 0.3
271
+ PL.on_shutdown {puts " * Websocket connection message test: #{RESULTS[connection_test]}" unless connection_test}
272
+ PL.on_shutdown {puts " * Websocket echo message test: #{RESULTS[echo_test]}" unless echo_test}
273
+ PL.on_shutdown {puts " * Websocket broadcast message test: #{RESULTS[broadcast_test]}" unless broadcast_test}
274
+ end
204
275
  end
205
276
 
206
277
  NO_PLEZI_AUTO_START = true
@@ -213,6 +284,7 @@ route("/ssl") {|req, res| res << "false" }
213
284
  listen port: 3030, ssl: true
214
285
  route("/ssl") {|req, res| res << "true" }
215
286
 
287
+ shared_route '/some/:multi{path|another_path}/(:option){route|test}/(:id)/(:optional)', TestCtrl
216
288
  shared_route '/', TestCtrl
217
289
 
218
290
 
@@ -221,15 +293,20 @@ Plezi::EventMachine.start Plezi.max_threads
221
293
  shoutdown_test = false
222
294
  Plezi.on_shutdown { shoutdown_test = true }
223
295
 
296
+ puts " --- Starting tests"
297
+ puts " --- Failed tests should read: #{PleziTestTasks::RESULTS[false]}"
224
298
  PleziTestTasks.run_tests
225
299
 
300
+
226
301
  Plezi::EventMachine.clear_timers
227
302
 
303
+ sleep PLEZI_TEST_TIME if defined? PLEZI_TEST_TIME
304
+
228
305
  Plezi::DSL.stop_services
229
306
 
230
307
  Plezi::EventMachine.shutdown
231
308
 
232
309
 
233
- puts "Shutdown test: #{ PleziTestTasks::RESULTS[shoutdown_test] }"
310
+ puts " * Shutdown test: #{ PleziTestTasks::RESULTS[shoutdown_test] }"
234
311
 
235
312
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plezi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boaz Segev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-06 00:00:00.000000000 Z
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,6 @@ files:
53
53
  - LICENSE.txt
54
54
  - README.md
55
55
  - Rakefile
56
- - TODO.md
57
56
  - bin/plezi
58
57
  - lib/plezi.rb
59
58
  - lib/plezi/common/cache.rb
@@ -82,6 +81,7 @@ files:
82
81
  - lib/plezi/server/http_response.rb
83
82
  - lib/plezi/server/mime_types.rb
84
83
  - lib/plezi/server/websocket.rb
84
+ - lib/plezi/server/websocket_client.rb
85
85
  - lib/plezi/server/ws_response.rb
86
86
  - lib/plezi/version.rb
87
87
  - plezi.gemspec
@@ -97,6 +97,7 @@ files:
97
97
  - resources/code.rb
98
98
  - resources/config.ru
99
99
  - resources/controller.rb
100
+ - resources/database.yml
100
101
  - resources/db_ac_config.rb
101
102
  - resources/db_dm_config.rb
102
103
  - resources/db_sequel_config.rb
data/TODO.md DELETED
@@ -1,15 +0,0 @@
1
- # HTTPHost
2
-
3
- Folder Listing:: add folder listing option?
4
-
5
- # Template
6
-
7
- update gem file (remove erb).
8
-
9
- # HTTPProtocol
10
-
11
- XML::
12
- support for XML HTTP body types?
13
-
14
- Charset::
15
- parse chareset for incoming content-type in the multipart request body? (or leave if binary?)