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

108 lines
2.9 KiB
Ruby
Raw Normal View History

Remove use of json gem (#2479) * Add basic JSON serializer For now, it only handles Arrays of Integers, but we'll extend it to support all of the common types * Serialize Strings * Escape quotes in Strings * Escape backslashes in Strings * Serialize Hashes with String keys * Extract method for serializing Strings * Add test coverage for non-Hash non-Array JSON serialization * Add test for unexpected key types * Serialize Hashes with Symbol keys * Raise on unexpected value types * Serialize boolean values * Serialize Floats * Add module comment to Puma::JSON * Update integration test to use fully-qualfied JSON module reference * Remove json gem dependency from /stats status server response Fixes a bug where requesting `/stats` from the status server would cause subsequent phased restarts to fail when upgrading/downgrading the json gem. * Run gc_stats tests on JRuby These were disabled at some point on JRuby, but they seem to run fine. Importantly, this test ensures that a call to `/gc-stats` returns well-formed JSON on JRuby, where the value of `GC.stat` contains nested structures. * Remove json gem dependency from /gc-stats status server response Fixes a bug where requesting `/gc-stats` from the status server would cause subsequent phased restarts to fail when upgrading/downgrading the json gem. * Remove json gem from /thread-backtraces status server response Fixes a bug where requesting `/thread-backtraces` from the status server would cause subsequent phased restarts to fail when upgrading/downgrading the json gem. * Remove json gem dependency from Puma.stats Fixes a bug where accessing `Puma.stats` would cause subsequent phased restarts to fail when upgrading/downgrading the json gem. * Fix test name in json test Co-authored-by: rmacklin <1863540+rmacklin@users.noreply.github.com> * Add History entry * Add test for exceptions on values of unexpected types * Update test name for additional clarity * Reorder cases to match order in ECMA-404 * Allow all serializable inputs in Puma::JSON::serialize The pervious implementation was based on and older JSON standard which defined JSON texts to be either objects or arrays. Modern JSON standands allow all JSON values to be valid JSON texts. * Update JSON tests to test value types directly * Reorder tests to roughly match source order * Add test for serializing integers as JSON * Serialize nil as null * Use block form of gsub instead of hash form * Escape control characters as required by ECMA-404 * Collapse handling of Symbol and String into one case * Extract constants used in string serialization * Remove superflous else case * Use stringio for incremental JSON construction * Extract test helper for testing JSON serialization * Assert that strings generated by Puma::JSON roundtrip when using ::JSON * Use a recent version of the json gem in tests `::JSON.parse` doesn't handle JSON texts other than objects and arrays in old versions * Handle default expected_roundtrip more explicitly for clarity Co-authored-by: rmacklin <1863540+rmacklin@users.noreply.github.com>
2020-11-10 12:16:42 -05:00
require_relative "helper"
require "json"
require "puma/json"
class TestJSON < Minitest::Test
parallelize_me! unless JRUBY_HEAD
def test_json_generates_string_for_hash_with_string_keys
value = { "key" => "value" }
assert_puma_json_generates_string '{"key":"value"}', value
end
def test_json_generates_string_for_hash_with_symbol_keys
value = { key: 'value' }
assert_puma_json_generates_string '{"key":"value"}', value, expected_roundtrip: { "key" => "value" }
end
def test_generate_raises_error_for_unexpected_key_type
value = { [1] => 'b' }
ex = assert_raises Puma::JSON::SerializationError do
Puma::JSON.generate value
end
assert_equal 'Could not serialize object of type Array as object key', ex.message
end
def test_json_generates_string_for_array_of_integers
value = [1, 2, 3]
assert_puma_json_generates_string '[1,2,3]', value
end
def test_json_generates_string_for_array_of_strings
value = ["a", "b", "c"]
assert_puma_json_generates_string '["a","b","c"]', value
end
def test_json_generates_string_for_nested_arrays
value = [1, [2, [3]]]
assert_puma_json_generates_string '[1,[2,[3]]]', value
end
def test_json_generates_string_for_integer
value = 42
assert_puma_json_generates_string '42', value
end
def test_json_generates_string_for_float
value = 1.23
assert_puma_json_generates_string '1.23', value
end
def test_json_escapes_strings_with_quotes
value = 'a"'
assert_puma_json_generates_string '"a\""', value
end
def test_json_escapes_strings_with_backslashes
value = 'a\\'
assert_puma_json_generates_string '"a\\\\"', value
end
def test_json_escapes_strings_with_null_byte
value = "\x00"
assert_puma_json_generates_string '"\u0000"', value
end
def test_json_escapes_strings_with_unicode_information_separator_one
value = "\x1f"
assert_puma_json_generates_string '"\u001F"', value
end
def test_json_generates_string_for_true
value = true
assert_puma_json_generates_string 'true', value
end
def test_json_generates_string_for_false
value = false
assert_puma_json_generates_string 'false', value
end
def test_json_generates_string_for_nil
value = nil
assert_puma_json_generates_string 'null', value
end
def test_generate_raises_error_for_unexpected_value_type
value = /abc/
ex = assert_raises Puma::JSON::SerializationError do
Puma::JSON.generate value
end
assert_equal 'Unexpected value of type Regexp', ex.message
end
private
def assert_puma_json_generates_string(expected_output, value_to_serialize, expected_roundtrip: nil)
actual_output = Puma::JSON.generate(value_to_serialize)
assert_equal expected_output, actual_output
if value_to_serialize.nil?
assert_nil ::JSON.parse(actual_output)
else
expected_roundtrip ||= value_to_serialize
assert_equal expected_roundtrip, ::JSON.parse(actual_output)
end
end
end