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

Fix: Invalid JSON on gc-stats (#1801)

* Fix: Invalid JSON on gc-stats

Credits of this patch should be for @jdsundberg, who reported the issue
but never sent a patch. Link to his bug report is below.

Closes #1687.

* Update tests so JSON response is correctly parsed

JSON without a whitespace between key and values wasn't being correctly
parsed (for example: `key: 'value'` was processed ok, but `key:'value'`
wasn't).
This commit is contained in:
Jesús Burgos Maciá 2019-05-21 14:43:18 +02:00 committed by Nate Berkopec
parent 73c20266fd
commit 2db1ef10f7
2 changed files with 5 additions and 8 deletions

View file

@ -1,3 +1,5 @@
require 'json'
module Puma
module App
class Status
@ -60,8 +62,7 @@ module Puma
return rack_response(200, OK_STATUS)
when /\/gc-stats$/
json = "{" + GC.stat.map { |k, v| "\"#{k}\": #{v}" }.join(",") + "}"
return rack_response(200, json)
return rack_response(200, GC.stat.to_json)
when /\/stats$/
return rack_response(200, @cli.stats)

View file

@ -1,6 +1,7 @@
require_relative "helper"
require "puma/cli"
require "json"
class TestCLI < Minitest::Test
def setup
@ -195,12 +196,7 @@ class TestCLI < Minitest::Test
lines = body.split("\r\n")
json_line = lines.detect { |l| l[0] == "{" }
pairs = json_line.scan(/\"[^\"]+\": [^,]+/)
gc_stats = {}
pairs.each do |p|
p =~ /\"([^\"]+)\": ([^,]+)/ || raise("Can't parse #{p.inspect}!")
gc_stats[$1] = $2
end
gc_stats = JSON.parse(json_line)
gc_count_after = gc_stats["count"].to_i
# Hitting the /gc route should increment the count by 1