scaffolding for message bus diags

This commit is contained in:
Sam Saffron 2013-02-12 18:01:38 +11:00
parent 818b84f02c
commit bdba0a78c4
4 changed files with 67 additions and 6 deletions

View File

@ -10,6 +10,7 @@ require "message_bus/client"
require "message_bus/connection_manager"
require "message_bus/message_handler"
require "message_bus/rack/middleware"
require "message_bus/rack/diagnostics"
# we still need to take care of the logger
if defined?(::Rails)
@ -71,19 +72,28 @@ module MessageBus::Implementation
end
def site_id_lookup(&blk)
@site_id_lookup ||= blk
@site_id_lookup = blk if blk
@site_id_lookup
end
def user_id_lookup(&blk)
@user_id_lookup ||= blk
@user_id_lookup = blk if blk
@user_id_lookup
end
def is_admin_lookup(&blk)
@is_admin_lookup = blk if blk
@is_admin_lookup
end
def on_connect(&blk)
@on_connect ||= blk
@on_connect = blk if blk
@on_connect
end
def on_disconnect(&blk)
@on_disconnect ||= blk
@on_disconnect = blk if blk
@on_disconnect
end
def allow_broadcast=(val)

View File

@ -0,0 +1,35 @@
module MessageBus::Rack; end
class MessageBus::Rack::Diagnostics
def initialize(app, config = {})
@app = app
end
def index
html = <<HTML
<!DOCTYPE html>
<html>
<head></head>
<body>
<h2>Message Bus Diags</h2>
</body>
</html>
HTML
return [200, {"content-type" => "text/html;"}, html]
end
def call(env)
return @app.call(env) unless env['PATH_INFO'].start_with? '/message-bus/_diagnostics'
route = env['PATH_INFO'].split('/message_bus/_diagnostics')[1]
if MessageBus.is_admin_lookup.nil? || !MessageBus.is_admin_lookup.call
return [403, {}, ["not allowed"]]
end
return index unless route
return [404, {}, ["not found"]]
end
end

View File

@ -46,6 +46,11 @@ class MessageBus::Rack::Middleware
return [200,{"Content-Type" => "text/html"},["sent"]]
end
if env['PATH_INFO'].start_with? '/message-bus/_diagnostics'
diags = MessageBus::Rack::Diagnostics.new(@app)
return diags.call(env)
end
client_id = env['PATH_INFO'].split("/")[2]
return [404, {}, ["not found"]] unless client_id

View File

@ -107,11 +107,22 @@ describe MessageBus::Rack::Middleware do
ensure
MessageBus.long_polling_interval = 5000
end
end
end
describe "diagnostics" do
it "should return a 403 if a user attempts to get at the _diagnostics path" do
get "/message-bus/_diagnostics"
last_response.status.should == 403
end
it "should get a 200 with html for an authorized user" do
MessageBus.stub(:is_admin_lookup).and_return(lambda{ true })
get "/message-bus/_diagnostics"
last_response.status.should == 200
end
end
describe "polling" do
before do