yajl-ruby 1.2.3 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of yajl-ruby might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.codeclimate.yml +40 -0
- data/.travis.yml +3 -7
- data/LICENSE +21 -0
- data/examples/encoding/chunked_encoding.rb +1 -1
- data/examples/encoding/one_shot.rb +1 -1
- data/examples/encoding/to_an_io.rb +1 -1
- data/examples/http/twitter_search_api.rb +1 -1
- data/examples/http/twitter_stream_api.rb +1 -1
- data/examples/parsing/from_file.rb +1 -1
- data/examples/parsing/from_stdin.rb +1 -1
- data/examples/parsing/from_string.rb +1 -1
- data/ext/yajl/yajl_encode.c +2 -2
- data/lib/yajl.rb +2 -2
- data/lib/yajl/version.rb +1 -1
- data/spec/encoding/encoding_spec.rb +45 -45
- data/spec/global/global_spec.rb +10 -10
- data/spec/http/http_delete_spec.rb +15 -15
- data/spec/http/http_error_spec.rb +7 -8
- data/spec/http/http_get_spec.rb +17 -17
- data/spec/http/http_post_spec.rb +19 -19
- data/spec/http/http_put_spec.rb +16 -16
- data/spec/http/http_stream_options_spec.rb +4 -4
- data/spec/json_gem_compatibility/compatibility_spec.rb +70 -70
- data/spec/parsing/active_support_spec.rb +10 -10
- data/spec/parsing/chunked_spec.rb +18 -18
- data/spec/parsing/fixtures_spec.rb +8 -8
- data/spec/parsing/large_number_spec.rb +1 -1
- data/spec/parsing/one_off_spec.rb +22 -29
- data/tasks/rspec.rake +1 -1
- data/yajl-ruby.gemspec +2 -2
- metadata +7 -6
- data/MIT-LICENSE +0 -20
@@ -8,20 +8,20 @@ describe "Passing options to HttpStream instance methods" do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should not create a new socket it one is provided" do
|
11
|
-
TCPSocket.
|
11
|
+
expect(TCPSocket).not_to receive(:new)
|
12
12
|
options = {:socket => :my_provided_socket}
|
13
13
|
|
14
14
|
@stream.send(:initialize_socket, URI.parse("http://google.com"), options)
|
15
15
|
|
16
|
-
options[:socket].
|
16
|
+
expect(options[:socket]).to eq(:my_provided_socket)
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should create a new socket if one is not provided" do
|
20
|
-
TCPSocket.
|
20
|
+
expect(TCPSocket).to receive(:new).with("google.com", 80).and_return( :tcp_socket )
|
21
21
|
options = {}
|
22
22
|
|
23
23
|
@stream.send(:initialize_socket, URI.parse("http://google.com"), options)
|
24
24
|
|
25
|
-
options[:socket].
|
25
|
+
expect(options[:socket]).to eq(:tcp_socket)
|
26
26
|
end
|
27
27
|
end
|
@@ -7,109 +7,109 @@ describe "JSON Gem compatability API" do
|
|
7
7
|
it "shoud not mixin #to_json on base objects until compatability has been enabled" do
|
8
8
|
d = Dummy.new
|
9
9
|
|
10
|
-
d.respond_to?(:to_json).
|
11
|
-
"".respond_to?(:to_json).
|
12
|
-
1.respond_to?(:to_json).
|
13
|
-
"1.5".to_f.respond_to?(:to_json).
|
14
|
-
[].respond_to?(:to_json).
|
15
|
-
{:foo => "bar"}.respond_to?(:to_json).
|
16
|
-
true.respond_to?(:to_json).
|
17
|
-
false.respond_to?(:to_json).
|
18
|
-
nil.respond_to?(:to_json).
|
10
|
+
expect(d.respond_to?(:to_json)).not_to be_truthy
|
11
|
+
expect("".respond_to?(:to_json)).not_to be_truthy
|
12
|
+
expect(1.respond_to?(:to_json)).not_to be_truthy
|
13
|
+
expect("1.5".to_f.respond_to?(:to_json)).not_to be_truthy
|
14
|
+
expect([].respond_to?(:to_json)).not_to be_truthy
|
15
|
+
expect({:foo => "bar"}.respond_to?(:to_json)).not_to be_truthy
|
16
|
+
expect(true.respond_to?(:to_json)).not_to be_truthy
|
17
|
+
expect(false.respond_to?(:to_json)).not_to be_truthy
|
18
|
+
expect(nil.respond_to?(:to_json)).not_to be_truthy
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should mixin #to_json on base objects after compatability has been enabled" do
|
22
22
|
require 'yajl/json_gem'
|
23
23
|
d = Dummy.new
|
24
24
|
|
25
|
-
d.respond_to?(:to_json).
|
26
|
-
"".respond_to?(:to_json).
|
27
|
-
1.respond_to?(:to_json).
|
28
|
-
"1.5".to_f.respond_to?(:to_json).
|
29
|
-
[].respond_to?(:to_json).
|
30
|
-
{:foo => "bar"}.respond_to?(:to_json).
|
31
|
-
true.respond_to?(:to_json).
|
32
|
-
false.respond_to?(:to_json).
|
33
|
-
nil.respond_to?(:to_json).
|
25
|
+
expect(d.respond_to?(:to_json)).to be_truthy
|
26
|
+
expect("".respond_to?(:to_json)).to be_truthy
|
27
|
+
expect(1.respond_to?(:to_json)).to be_truthy
|
28
|
+
expect("1.5".to_f.respond_to?(:to_json)).to be_truthy
|
29
|
+
expect([].respond_to?(:to_json)).to be_truthy
|
30
|
+
expect({:foo => "bar"}.respond_to?(:to_json)).to be_truthy
|
31
|
+
expect(true.respond_to?(:to_json)).to be_truthy
|
32
|
+
expect(false.respond_to?(:to_json)).to be_truthy
|
33
|
+
expect(nil.respond_to?(:to_json)).to be_truthy
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should require yajl/json_gem to enable the compatability API" do
|
37
|
-
defined?(JSON).
|
37
|
+
expect(defined?(JSON)).to be_truthy
|
38
38
|
|
39
|
-
JSON.respond_to?(:parse).
|
40
|
-
JSON.respond_to?(:generate).
|
41
|
-
JSON.respond_to?(:pretty_generate).
|
42
|
-
JSON.respond_to?(:load).
|
43
|
-
JSON.respond_to?(:dump).
|
39
|
+
expect(JSON.respond_to?(:parse)).to be_truthy
|
40
|
+
expect(JSON.respond_to?(:generate)).to be_truthy
|
41
|
+
expect(JSON.respond_to?(:pretty_generate)).to be_truthy
|
42
|
+
expect(JSON.respond_to?(:load)).to be_truthy
|
43
|
+
expect(JSON.respond_to?(:dump)).to be_truthy
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should allow default parsing options be set with JSON.default_options" do
|
47
47
|
default = JSON.default_options[:symbolize_keys]
|
48
|
-
JSON.parse('{"foo": 1234}').
|
48
|
+
expect(JSON.parse('{"foo": 1234}')).to be === {"foo" => 1234}
|
49
49
|
JSON.default_options[:symbolize_keys] = true
|
50
|
-
JSON.parse('{"foo": 1234}').
|
50
|
+
expect(JSON.parse('{"foo": 1234}')).to be === {:foo => 1234}
|
51
51
|
JSON.default_options[:symbolize_keys] = default # ensure the rest of the test cases expect the default
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should also allow the json gem's symbolize_names key" do
|
55
|
-
JSON.parse('{"foo": 1234}', :symbolize_names => true).
|
55
|
+
expect(JSON.parse('{"foo": 1234}', :symbolize_names => true)).to be === {:foo => 1234}
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should encode arbitrary classes via their default to_json method" do
|
59
59
|
d = Dummy.new
|
60
|
-
d.to_json.
|
60
|
+
expect(d.to_json).to eq("\"#{d.to_s}\"")
|
61
61
|
|
62
62
|
t = Time.now
|
63
|
-
t.to_json.
|
63
|
+
expect(t.to_json).to eq("\"#{t.to_s}\"")
|
64
64
|
|
65
65
|
da = Date.today
|
66
|
-
da.to_json.
|
66
|
+
expect(da.to_json).to eq("\"#{da.to_s}\"")
|
67
67
|
|
68
68
|
dt = DateTime.new
|
69
|
-
dt.to_json.
|
69
|
+
expect(dt.to_json).to eq("\"#{dt.to_s}\"")
|
70
70
|
end
|
71
71
|
|
72
72
|
it "should have the standard parsing and encoding exceptions mapped" do
|
73
|
-
JSON::JSONError.new.is_a?(StandardError).
|
74
|
-
JSON::ParserError.new.is_a?(JSON::JSONError).
|
75
|
-
JSON::GeneratorError.new.is_a?(JSON::JSONError).
|
73
|
+
expect(JSON::JSONError.new.is_a?(StandardError)).to be_truthy
|
74
|
+
expect(JSON::ParserError.new.is_a?(JSON::JSONError)).to be_truthy
|
75
|
+
expect(JSON::GeneratorError.new.is_a?(JSON::JSONError)).to be_truthy
|
76
76
|
|
77
|
-
|
77
|
+
expect {
|
78
78
|
JSON.parse("blah")
|
79
|
-
}.
|
79
|
+
}.to raise_error(JSON::ParserError)
|
80
80
|
|
81
|
-
|
81
|
+
expect {
|
82
82
|
JSON.generate(0.0/0.0)
|
83
|
-
}.
|
83
|
+
}.to raise_error(JSON::GeneratorError)
|
84
84
|
end
|
85
85
|
|
86
86
|
context "ported tests for Unicode" do
|
87
87
|
it "should be able to encode and parse unicode" do
|
88
|
-
'""'.
|
89
|
-
'"\\b"'.
|
90
|
-
'"\u0001"'.
|
91
|
-
'"\u001F"'.
|
92
|
-
'" "'.
|
93
|
-
"\"#{0x7f.chr}\"".
|
88
|
+
expect('""').to eql(''.to_json)
|
89
|
+
expect('"\\b"').to eql("\b".to_json)
|
90
|
+
expect('"\u0001"').to eql(0x1.chr.to_json)
|
91
|
+
expect('"\u001F"').to eql(0x1f.chr.to_json)
|
92
|
+
expect('" "').to eql(' '.to_json)
|
93
|
+
expect("\"#{0x7f.chr}\"").to eql(0x7f.chr.to_json)
|
94
94
|
utf8 = [ "© ≠ €! \01" ]
|
95
95
|
json = "[\"© ≠ €! \\u0001\"]"
|
96
|
-
json.
|
97
|
-
utf8.
|
96
|
+
expect(json).to eql(utf8.to_json)
|
97
|
+
expect(utf8).to eql(JSON.parse(json))
|
98
98
|
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
|
99
99
|
json = "[\"あいうえお\"]"
|
100
|
-
json.
|
101
|
-
utf8.
|
100
|
+
expect(json).to eql(utf8.to_json)
|
101
|
+
expect(utf8).to eql(JSON.parse(json))
|
102
102
|
utf8 = ['საქართველო']
|
103
103
|
json = "[\"საქართველო\"]"
|
104
|
-
json.
|
105
|
-
utf8.
|
106
|
-
'["Ã"]'.
|
107
|
-
["€"].
|
104
|
+
expect(json).to eql(utf8.to_json)
|
105
|
+
expect(utf8).to eql(JSON.parse(json))
|
106
|
+
expect('["Ã"]').to eql(JSON.generate(["Ã"]))
|
107
|
+
expect(["€"]).to eql(JSON.parse('["\u20ac"]'))
|
108
108
|
utf8_str = "\xf0\xa0\x80\x81"
|
109
109
|
utf8 = [utf8_str]
|
110
110
|
json = "[\"#{utf8_str}\"]"
|
111
|
-
json.
|
112
|
-
utf8.
|
111
|
+
expect(json).to eql(JSON.generate(utf8))
|
112
|
+
expect(utf8).to eql(JSON.parse(json))
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
@@ -144,25 +144,25 @@ describe "JSON Gem compatability API" do
|
|
144
144
|
|
145
145
|
it "should be able to unparse" do
|
146
146
|
json = JSON.generate(@hash)
|
147
|
-
JSON.parse(@json2).
|
147
|
+
expect(JSON.parse(@json2)).to eq(JSON.parse(json))
|
148
148
|
parsed_json = JSON.parse(json)
|
149
|
-
@hash.
|
149
|
+
expect(@hash).to eq(parsed_json)
|
150
150
|
json = JSON.generate({1=>2})
|
151
|
-
'{"1":2}'.
|
151
|
+
expect('{"1":2}').to eql(json)
|
152
152
|
parsed_json = JSON.parse(json)
|
153
|
-
{"1"=>2}.
|
153
|
+
expect({"1"=>2}).to eq(parsed_json)
|
154
154
|
end
|
155
155
|
|
156
156
|
it "should be able to unparse pretty" do
|
157
157
|
json = JSON.pretty_generate(@hash)
|
158
|
-
JSON.parse(@json3).
|
158
|
+
expect(JSON.parse(@json3)).to eq(JSON.parse(json))
|
159
159
|
parsed_json = JSON.parse(json)
|
160
|
-
@hash.
|
160
|
+
expect(@hash).to eq(parsed_json)
|
161
161
|
json = JSON.pretty_generate({1=>2})
|
162
162
|
test = "{\n \"1\": 2\n}".chomp
|
163
|
-
test.
|
163
|
+
expect(test).to eq(json)
|
164
164
|
parsed_json = JSON.parse(json)
|
165
|
-
{"1"=>2}.
|
165
|
+
expect({"1"=>2}).to eq(parsed_json)
|
166
166
|
end
|
167
167
|
end
|
168
168
|
|
@@ -174,33 +174,33 @@ describe "JSON Gem compatability API" do
|
|
174
174
|
|
175
175
|
JSON_FAILED.each do |name, source|
|
176
176
|
it "should not be able to parse #{File.basename(name)} as an IO" do
|
177
|
-
|
177
|
+
expect {
|
178
178
|
JSON.parse(StringIO.new(source))
|
179
|
-
}.
|
179
|
+
}.to raise_error(JSON::ParserError)
|
180
180
|
end
|
181
181
|
end
|
182
182
|
|
183
183
|
JSON_FAILED.each do |name, source|
|
184
184
|
it "should not be able to parse #{File.basename(name)} as a string" do
|
185
|
-
|
185
|
+
expect {
|
186
186
|
JSON.parse(source)
|
187
|
-
}.
|
187
|
+
}.to raise_error(JSON::ParserError)
|
188
188
|
end
|
189
189
|
end
|
190
190
|
|
191
191
|
JSON_PASSED.each do |name, source|
|
192
192
|
it "should be able to parse #{File.basename(name)} as an IO" do
|
193
|
-
|
193
|
+
expect {
|
194
194
|
JSON.parse(StringIO.new(source))
|
195
|
-
}.
|
195
|
+
}.not_to raise_error
|
196
196
|
end
|
197
197
|
end
|
198
198
|
|
199
199
|
JSON_PASSED.each do |name, source|
|
200
200
|
it "should be able to parse #{File.basename(name)} as a string" do
|
201
|
-
|
201
|
+
expect {
|
202
202
|
JSON.parse(source)
|
203
|
-
}.
|
203
|
+
}.not_to raise_error
|
204
204
|
end
|
205
205
|
end
|
206
206
|
end
|
@@ -36,29 +36,29 @@ describe "ActiveSupport test cases" do
|
|
36
36
|
|
37
37
|
TESTS.each do |json, expected|
|
38
38
|
it "should be able to parse #{json} as an IO" do
|
39
|
-
|
40
|
-
Yajl::Parser.parse(StringIO.new(json)).
|
41
|
-
}.
|
39
|
+
expect {
|
40
|
+
expect(Yajl::Parser.parse(StringIO.new(json))).to eq(expected)
|
41
|
+
}.not_to raise_error
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
TESTS.each do |json, expected|
|
46
46
|
it "should be able to parse #{json} as a string" do
|
47
|
-
|
48
|
-
Yajl::Parser.parse(json).
|
49
|
-
}.
|
47
|
+
expect {
|
48
|
+
expect(Yajl::Parser.parse(json)).to eq(expected)
|
49
|
+
}.not_to raise_error
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should fail parsing {: 1} as an IO" do
|
54
|
-
|
54
|
+
expect {
|
55
55
|
Yajl::Parser.parse(StringIO.new("{: 1}"))
|
56
|
-
}.
|
56
|
+
}.to raise_error(Yajl::ParseError)
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should fail parsing {: 1} as a string" do
|
60
|
-
|
60
|
+
expect {
|
61
61
|
Yajl::Parser.parse("{: 1}")
|
62
|
-
}.
|
62
|
+
}.to raise_error(Yajl::ParseError)
|
63
63
|
end
|
64
64
|
end
|
@@ -14,25 +14,25 @@ describe "Chunked parser" do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should parse a single chunk" do
|
17
|
-
@callback.
|
17
|
+
expect(@callback).to receive(:call).with(@final)
|
18
18
|
@parser << '[{"abc": 123},{"def": 456}]'
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should parse a single chunk, 3 times" do
|
22
|
-
@callback.
|
22
|
+
expect(@callback).to receive(:call).with(@final).exactly(3).times
|
23
23
|
@parser << '[{"abc": 123},{"def": 456}]'
|
24
24
|
@parser << '[{"abc": 123},{"def": 456}]'
|
25
25
|
@parser << '[{"abc": 123},{"def": 456}]'
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should parse in two chunks" do
|
29
|
-
@callback.
|
29
|
+
expect(@callback).to receive(:call).with(@final)
|
30
30
|
@parser << '[{"abc": 123},'
|
31
31
|
@parser << '{"def": 456}]'
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should parse in 2 chunks, twice" do
|
35
|
-
@callback.
|
35
|
+
expect(@callback).to receive(:call).with(@final).exactly(2).times
|
36
36
|
@parser << '[{"abc": 123},'
|
37
37
|
@parser << '{"def": 456}]'
|
38
38
|
@parser << '[{"abc": 123},'
|
@@ -40,57 +40,57 @@ describe "Chunked parser" do
|
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should parse 2 JSON strings, in 3 chunks" do
|
43
|
-
@callback.
|
43
|
+
expect(@callback).to receive(:call).with(@final).exactly(2).times
|
44
44
|
@parser << '[{"abc": 123},'
|
45
45
|
@parser << '{"def": 456}][{"abc": 123},{"def":'
|
46
46
|
@parser << ' 456}]'
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should parse 2 JSON strings in 1 chunk" do
|
50
|
-
@callback.
|
50
|
+
expect(@callback).to receive(:call).with(@final).exactly(2).times
|
51
51
|
@parser << '[{"abc": 123},{"def": 456}][{"abc": 123},{"def": 456}]'
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should parse 2 JSON strings from an IO" do
|
55
|
-
@callback.
|
55
|
+
expect(@callback).to receive(:call).with(@final).exactly(2).times
|
56
56
|
@parser.parse(StringIO.new('[{"abc": 123},{"def": 456}][{"abc": 123},{"def": 456}]'))
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should parse a JSON string an IO and fire callback once" do
|
60
|
-
@callback.
|
60
|
+
expect(@callback).to receive(:call).with(@final)
|
61
61
|
@parser.parse(StringIO.new('[{"abc": 123},{"def": 456}]'))
|
62
62
|
end
|
63
63
|
|
64
64
|
it "should parse twitter_stream.json and fire callback 430 times" do
|
65
65
|
path = File.expand_path(File.dirname(__FILE__) + '/../../benchmark/subjects/twitter_stream.json')
|
66
66
|
json = File.new(path, 'r')
|
67
|
-
@callback.
|
68
|
-
|
67
|
+
expect(@callback).to receive(:call).exactly(430).times
|
68
|
+
expect {
|
69
69
|
@parser.parse(json)
|
70
|
-
}.
|
70
|
+
}.not_to raise_error
|
71
71
|
end
|
72
72
|
|
73
73
|
it "should parse twitter_stream.json and fire callback 430 times, with a block as the callback" do
|
74
74
|
path = File.expand_path(File.dirname(__FILE__) + '/../../benchmark/subjects/twitter_stream.json')
|
75
75
|
json = File.new(path, 'r')
|
76
|
-
@callback.
|
76
|
+
expect(@callback).to receive(:call).exactly(0).times
|
77
77
|
@parser.on_parse_complete = nil
|
78
|
-
|
78
|
+
expect {
|
79
79
|
times = 0
|
80
80
|
@parser.parse(json) do |hsh|
|
81
81
|
times += 1
|
82
82
|
end
|
83
|
-
times.
|
84
|
-
}.
|
83
|
+
expect(times).to eql(430)
|
84
|
+
}.not_to raise_error
|
85
85
|
end
|
86
86
|
|
87
87
|
it "should raise a Yajl::ParseError error if multiple JSON strings were found when no on_parse_complete callback assigned" do
|
88
88
|
path = File.expand_path(File.dirname(__FILE__) + '/../../benchmark/subjects/twitter_stream.json')
|
89
89
|
json = File.new(path, 'r')
|
90
90
|
@parser.on_parse_complete = nil
|
91
|
-
@callback.
|
92
|
-
|
91
|
+
expect(@callback).to receive(:call).exactly(0).times
|
92
|
+
expect {
|
93
93
|
@parser.parse(json)
|
94
|
-
}.
|
94
|
+
}.to raise_error(Yajl::ParseError)
|
95
95
|
end
|
96
96
|
end
|
@@ -8,33 +8,33 @@ describe "Parsing JSON Fixtures" do
|
|
8
8
|
|
9
9
|
FAILED.each do |name, source|
|
10
10
|
it "should not be able to parse #{File.basename(name)} as an IO" do
|
11
|
-
|
11
|
+
expect {
|
12
12
|
Yajl::Parser.parse(StringIO.new(source))
|
13
|
-
}.
|
13
|
+
}.to raise_error(Yajl::ParseError)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
FAILED.each do |name, source|
|
18
18
|
it "should not be able to parse #{File.basename(name)} as a string" do
|
19
|
-
|
19
|
+
expect {
|
20
20
|
Yajl::Parser.parse(source)
|
21
|
-
}.
|
21
|
+
}.to raise_error(Yajl::ParseError)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
PASSED.each do |name, source|
|
26
26
|
it "should be able to parse #{File.basename(name)} as an IO" do
|
27
|
-
|
27
|
+
expect {
|
28
28
|
Yajl::Parser.parse(StringIO.new(source))
|
29
|
-
}.
|
29
|
+
}.not_to raise_error
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
PASSED.each do |name, source|
|
34
34
|
it "should be able to parse #{File.basename(name)} as a string" do
|
35
|
-
|
35
|
+
expect {
|
36
36
|
Yajl::Parser.parse(source)
|
37
|
-
}.
|
37
|
+
}.not_to raise_error
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|