1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Return valid Rack responses from status server

Use Rack::Lint to check the response is valid. Existing code really just
needed Content-Length & Content-Type headers.
This commit is contained in:
jc00ke 2012-04-29 12:18:21 -07:00
parent 208c2f56fb
commit ede98dadfd
3 changed files with 37 additions and 7 deletions

View file

@ -1,3 +1,14 @@
=== HEAD
1 minor feature:
Return valid Rack responses (passes Lint) from status
server.
1 new contributer:
* Jesse Cooke
=== 1.2.2 / 2012-04-28
* 4 bug fixes:

View file

@ -6,6 +6,7 @@ module Puma
@cli = cli
@auth_token = nil
end
OK_STATUS = '{ "status": "ok" }'.freeze
attr_accessor :auth_token
@ -16,34 +17,39 @@ module Puma
def call(env)
unless authenticate(env)
return [403, {}, ["Invalid auth token"]]
return rack_response(403, "Invalid auth token")
end
case env['PATH_INFO']
when "/stop"
@server.stop
return [200, {}, ['{ "status": "ok" }']]
return rack_response(200, OK_STATUS)
when "/halt"
@server.halt
return [200, {}, ['{ "status": "ok" }']]
return rack_response(200, OK_STATUS)
when "/restart"
if @cli and @cli.restart_on_stop!
@server.begin_restart
return [200, {}, ['{ "status": "ok" }']]
return rack_response(200, OK_STATUS)
else
return [200, {}, ['{ "status": "not configured" }']]
return rack_response(200, '{ "status": "not configured" }')
end
when "/stats"
b = @server.backlog
r = @server.running
return [200, {}, ["{ \"backlog\": #{b}, \"running\": #{r} }"]]
return rack_response(200, %Q!{ "backlog": #{b}, "running": #{r} }!)
end
[404, {}, ["Unsupported action"]]
rack_response 404, "Unsupported action"
end
private
def rack_response(status, body)
[status, { 'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }, [body]]
end
end
end

View file

@ -1,4 +1,5 @@
require 'test/unit'
require 'rack'
require 'puma/app/status'
class TestAppStatus < Test::Unit::TestCase
@ -27,6 +28,12 @@ class TestAppStatus < Test::Unit::TestCase
@app.auth_token = nil
end
def lint(env)
app = Rack::Lint.new @app
mock_env = Rack::MockRequest.env_for env['PATH_INFO']
app.call mock_env
end
def test_bad_token
@app.auth_token = "abcdef"
@ -35,6 +42,7 @@ class TestAppStatus < Test::Unit::TestCase
status, _, _ = @app.call env
assert_equal 403, status
lint(env)
end
def test_good_token
@ -48,6 +56,7 @@ class TestAppStatus < Test::Unit::TestCase
status, _, _ = @app.call env
assert_equal 404, status
lint(env)
end
def test_unsupported
@ -56,6 +65,7 @@ class TestAppStatus < Test::Unit::TestCase
status, _, _ = @app.call env
assert_equal 404, status
lint(env)
end
def test_stop
@ -66,6 +76,7 @@ class TestAppStatus < Test::Unit::TestCase
assert_equal :stop, @server.status
assert_equal 200, status
assert_equal ['{ "status": "ok" }'], body
lint(env)
end
def test_halt
@ -76,6 +87,7 @@ class TestAppStatus < Test::Unit::TestCase
assert_equal :halt, @server.status
assert_equal 200, status
assert_equal ['{ "status": "ok" }'], body
lint(env)
end
def test_stats
@ -88,6 +100,7 @@ class TestAppStatus < Test::Unit::TestCase
assert_equal 200, status
assert_equal ['{ "backlog": 1, "running": 9 }'], body
lint(env)
end
end