2011-12-05 12:01:19 -05:00
|
|
|
require 'test/unit'
|
2012-04-29 15:18:21 -04:00
|
|
|
require 'rack'
|
2011-12-05 12:01:19 -05:00
|
|
|
require 'puma/app/status'
|
|
|
|
|
|
|
|
class TestAppStatus < Test::Unit::TestCase
|
|
|
|
class FakeServer
|
|
|
|
def initialize
|
|
|
|
@status = :running
|
|
|
|
@backlog = 0
|
|
|
|
@running = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :status
|
|
|
|
attr_accessor :backlog, :running
|
|
|
|
|
|
|
|
def stop
|
|
|
|
@status = :stop
|
|
|
|
end
|
|
|
|
|
|
|
|
def halt
|
|
|
|
@status = :halt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@server = FakeServer.new
|
2011-12-05 18:58:23 -05:00
|
|
|
@app = Puma::App::Status.new(@server, @server)
|
2011-12-06 19:22:02 -05:00
|
|
|
@app.auth_token = nil
|
|
|
|
end
|
|
|
|
|
2012-06-29 20:37:05 -04:00
|
|
|
def lint(uri)
|
2012-04-29 15:18:21 -04:00
|
|
|
app = Rack::Lint.new @app
|
2012-06-29 20:37:05 -04:00
|
|
|
mock_env = Rack::MockRequest.env_for uri
|
2012-04-29 15:18:21 -04:00
|
|
|
app.call mock_env
|
|
|
|
end
|
|
|
|
|
2011-12-06 19:22:02 -05:00
|
|
|
def test_bad_token
|
|
|
|
@app.auth_token = "abcdef"
|
|
|
|
|
2012-06-29 20:37:05 -04:00
|
|
|
status, _, _ = lint('/whatever')
|
2011-12-06 19:22:02 -05:00
|
|
|
|
|
|
|
assert_equal 403, status
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_good_token
|
|
|
|
@app.auth_token = "abcdef"
|
|
|
|
|
2012-06-29 20:37:05 -04:00
|
|
|
status, _, _ = lint('/whatever?token=abcdef')
|
2011-12-06 19:22:02 -05:00
|
|
|
|
|
|
|
assert_equal 404, status
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unsupported
|
2012-06-29 20:37:05 -04:00
|
|
|
status, _, _ = lint('/not-real')
|
2011-12-05 12:01:19 -05:00
|
|
|
|
|
|
|
assert_equal 404, status
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stop
|
2012-06-29 20:37:05 -04:00
|
|
|
status, _ , app = lint('/stop')
|
2011-12-05 12:01:19 -05:00
|
|
|
|
|
|
|
assert_equal :stop, @server.status
|
|
|
|
assert_equal 200, status
|
2012-06-29 20:37:05 -04:00
|
|
|
assert_equal ['{ "status": "ok" }'], app.enum_for.to_a
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_halt
|
2012-06-29 20:37:05 -04:00
|
|
|
status, _ , app = lint('/halt')
|
2011-12-05 12:01:19 -05:00
|
|
|
|
|
|
|
assert_equal :halt, @server.status
|
|
|
|
assert_equal 200, status
|
2012-06-29 20:37:05 -04:00
|
|
|
assert_equal ['{ "status": "ok" }'], app.enum_for.to_a
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_stats
|
|
|
|
@server.backlog = 1
|
|
|
|
@server.running = 9
|
|
|
|
|
2012-06-29 20:37:05 -04:00
|
|
|
status, _ , app = lint('/stats')
|
2011-12-05 12:01:19 -05:00
|
|
|
|
|
|
|
assert_equal 200, status
|
2012-06-29 20:37:05 -04:00
|
|
|
assert_equal ['{ "backlog": 1, "running": 9 }'], app.enum_for.to_a
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
end
|