From 2db1ef10f71b674f391549659734d7c5976e67cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Burgos=20Maci=C3=A1?= Date: Tue, 21 May 2019 14:43:18 +0200 Subject: [PATCH] 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). --- lib/puma/app/status.rb | 5 +++-- test/test_cli.rb | 8 ++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/puma/app/status.rb b/lib/puma/app/status.rb index 6f68c6b8..58615d7d 100644 --- a/lib/puma/app/status.rb +++ b/lib/puma/app/status.rb @@ -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) diff --git a/test/test_cli.rb b/test/test_cli.rb index ceffef7e..cadefd09 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -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