benofsky-yajl-ruby 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/.gitignore +9 -0
  2. data/CHANGELOG.md +281 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +320 -0
  5. data/Rakefile +40 -0
  6. data/VERSION.yml +5 -0
  7. data/benchmark/encode.rb +58 -0
  8. data/benchmark/encode_json_and_marshal.rb +42 -0
  9. data/benchmark/encode_json_and_yaml.rb +53 -0
  10. data/benchmark/http.rb +32 -0
  11. data/benchmark/parse.rb +59 -0
  12. data/benchmark/parse_json_and_marshal.rb +50 -0
  13. data/benchmark/parse_json_and_yaml.rb +55 -0
  14. data/benchmark/parse_stream.rb +54 -0
  15. data/benchmark/subjects/item.json +1 -0
  16. data/benchmark/subjects/ohai.json +1216 -0
  17. data/benchmark/subjects/ohai.marshal_dump +0 -0
  18. data/benchmark/subjects/ohai.yml +975 -0
  19. data/benchmark/subjects/twitter_search.json +1 -0
  20. data/benchmark/subjects/twitter_stream.json +430 -0
  21. data/benchmark/subjects/unicode.json +1 -0
  22. data/examples/encoding/chunked_encoding.rb +27 -0
  23. data/examples/encoding/one_shot.rb +13 -0
  24. data/examples/encoding/to_an_io.rb +12 -0
  25. data/examples/http/twitter_search_api.rb +12 -0
  26. data/examples/http/twitter_stream_api.rb +26 -0
  27. data/examples/parsing/from_file.rb +14 -0
  28. data/examples/parsing/from_stdin.rb +9 -0
  29. data/examples/parsing/from_string.rb +13 -0
  30. data/ext/api/yajl_common.h +85 -0
  31. data/ext/api/yajl_gen.h +159 -0
  32. data/ext/api/yajl_parse.h +196 -0
  33. data/ext/extconf.rb +9 -0
  34. data/ext/yajl.c +164 -0
  35. data/ext/yajl_alloc.c +65 -0
  36. data/ext/yajl_alloc.h +50 -0
  37. data/ext/yajl_buf.c +119 -0
  38. data/ext/yajl_buf.h +73 -0
  39. data/ext/yajl_bytestack.h +85 -0
  40. data/ext/yajl_encode.c +188 -0
  41. data/ext/yajl_encode.h +50 -0
  42. data/ext/yajl_ext.c +911 -0
  43. data/ext/yajl_ext.h +128 -0
  44. data/ext/yajl_gen.c +317 -0
  45. data/ext/yajl_lex.c +747 -0
  46. data/ext/yajl_lex.h +135 -0
  47. data/ext/yajl_parser.c +450 -0
  48. data/ext/yajl_parser.h +82 -0
  49. data/lib/yajl/bzip2/stream_reader.rb +32 -0
  50. data/lib/yajl/bzip2/stream_writer.rb +15 -0
  51. data/lib/yajl/bzip2.rb +11 -0
  52. data/lib/yajl/deflate/stream_reader.rb +44 -0
  53. data/lib/yajl/deflate/stream_writer.rb +21 -0
  54. data/lib/yajl/deflate.rb +6 -0
  55. data/lib/yajl/gzip/stream_reader.rb +31 -0
  56. data/lib/yajl/gzip/stream_writer.rb +14 -0
  57. data/lib/yajl/gzip.rb +6 -0
  58. data/lib/yajl/http_stream.rb +197 -0
  59. data/lib/yajl/json_gem/encoding.rb +50 -0
  60. data/lib/yajl/json_gem/parsing.rb +27 -0
  61. data/lib/yajl/json_gem.rb +14 -0
  62. data/lib/yajl.rb +93 -0
  63. data/spec/encoding/encoding_spec.rb +234 -0
  64. data/spec/global/global_spec.rb +55 -0
  65. data/spec/http/fixtures/http.bzip2.dump +0 -0
  66. data/spec/http/fixtures/http.chunked.dump +11 -0
  67. data/spec/http/fixtures/http.deflate.dump +0 -0
  68. data/spec/http/fixtures/http.error.dump +12 -0
  69. data/spec/http/fixtures/http.gzip.dump +0 -0
  70. data/spec/http/fixtures/http.html.dump +1220 -0
  71. data/spec/http/fixtures/http.raw.dump +1226 -0
  72. data/spec/http/http_delete_spec.rb +99 -0
  73. data/spec/http/http_error_spec.rb +33 -0
  74. data/spec/http/http_get_spec.rb +110 -0
  75. data/spec/http/http_post_spec.rb +124 -0
  76. data/spec/http/http_put_spec.rb +106 -0
  77. data/spec/json_gem_compatibility/compatibility_spec.rb +203 -0
  78. data/spec/parsing/active_support_spec.rb +64 -0
  79. data/spec/parsing/chunked_spec.rb +98 -0
  80. data/spec/parsing/fixtures/fail.15.json +1 -0
  81. data/spec/parsing/fixtures/fail.16.json +1 -0
  82. data/spec/parsing/fixtures/fail.17.json +1 -0
  83. data/spec/parsing/fixtures/fail.26.json +1 -0
  84. data/spec/parsing/fixtures/fail11.json +1 -0
  85. data/spec/parsing/fixtures/fail12.json +1 -0
  86. data/spec/parsing/fixtures/fail13.json +1 -0
  87. data/spec/parsing/fixtures/fail14.json +1 -0
  88. data/spec/parsing/fixtures/fail19.json +1 -0
  89. data/spec/parsing/fixtures/fail20.json +1 -0
  90. data/spec/parsing/fixtures/fail21.json +1 -0
  91. data/spec/parsing/fixtures/fail22.json +1 -0
  92. data/spec/parsing/fixtures/fail23.json +1 -0
  93. data/spec/parsing/fixtures/fail24.json +1 -0
  94. data/spec/parsing/fixtures/fail25.json +1 -0
  95. data/spec/parsing/fixtures/fail27.json +2 -0
  96. data/spec/parsing/fixtures/fail28.json +2 -0
  97. data/spec/parsing/fixtures/fail3.json +1 -0
  98. data/spec/parsing/fixtures/fail4.json +1 -0
  99. data/spec/parsing/fixtures/fail5.json +1 -0
  100. data/spec/parsing/fixtures/fail6.json +1 -0
  101. data/spec/parsing/fixtures/fail9.json +1 -0
  102. data/spec/parsing/fixtures/pass.array.json +6 -0
  103. data/spec/parsing/fixtures/pass.codepoints_from_unicode_org.json +1 -0
  104. data/spec/parsing/fixtures/pass.contacts.json +1 -0
  105. data/spec/parsing/fixtures/pass.db100.xml.json +1 -0
  106. data/spec/parsing/fixtures/pass.db1000.xml.json +1 -0
  107. data/spec/parsing/fixtures/pass.dc_simple_with_comments.json +11 -0
  108. data/spec/parsing/fixtures/pass.deep_arrays.json +1 -0
  109. data/spec/parsing/fixtures/pass.difficult_json_c_test_case.json +1 -0
  110. data/spec/parsing/fixtures/pass.difficult_json_c_test_case_with_comments.json +1 -0
  111. data/spec/parsing/fixtures/pass.doubles.json +1 -0
  112. data/spec/parsing/fixtures/pass.empty_array.json +1 -0
  113. data/spec/parsing/fixtures/pass.empty_string.json +1 -0
  114. data/spec/parsing/fixtures/pass.escaped_bulgarian.json +4 -0
  115. data/spec/parsing/fixtures/pass.escaped_foobar.json +1 -0
  116. data/spec/parsing/fixtures/pass.item.json +1 -0
  117. data/spec/parsing/fixtures/pass.json-org-sample1.json +23 -0
  118. data/spec/parsing/fixtures/pass.json-org-sample2.json +11 -0
  119. data/spec/parsing/fixtures/pass.json-org-sample3.json +26 -0
  120. data/spec/parsing/fixtures/pass.json-org-sample4-nows.json +88 -0
  121. data/spec/parsing/fixtures/pass.json-org-sample4.json +89 -0
  122. data/spec/parsing/fixtures/pass.json-org-sample5.json +27 -0
  123. data/spec/parsing/fixtures/pass.map-spain.xml.json +1 -0
  124. data/spec/parsing/fixtures/pass.ns-invoice100.xml.json +1 -0
  125. data/spec/parsing/fixtures/pass.ns-soap.xml.json +1 -0
  126. data/spec/parsing/fixtures/pass.numbers-fp-4k.json +6 -0
  127. data/spec/parsing/fixtures/pass.numbers-fp-64k.json +61 -0
  128. data/spec/parsing/fixtures/pass.numbers-int-4k.json +11 -0
  129. data/spec/parsing/fixtures/pass.numbers-int-64k.json +154 -0
  130. data/spec/parsing/fixtures/pass.twitter-search.json +1 -0
  131. data/spec/parsing/fixtures/pass.twitter-search2.json +1 -0
  132. data/spec/parsing/fixtures/pass.unicode.json +3315 -0
  133. data/spec/parsing/fixtures/pass.yelp.json +1 -0
  134. data/spec/parsing/fixtures/pass1.json +56 -0
  135. data/spec/parsing/fixtures/pass2.json +1 -0
  136. data/spec/parsing/fixtures/pass3.json +6 -0
  137. data/spec/parsing/fixtures_spec.rb +41 -0
  138. data/spec/parsing/one_off_spec.rb +81 -0
  139. data/spec/rcov.opts +3 -0
  140. data/spec/spec.opts +2 -0
  141. data/spec/spec_helper.rb +16 -0
  142. data/yajl-ruby.gemspec +203 -0
  143. metadata +232 -0
@@ -0,0 +1,99 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
+ begin
4
+ require 'yajl/bzip2'
5
+ rescue
6
+ warn "Couldn't load yajl/bzip2, maybe you don't have bzip2-ruby installed? Continuing without running bzip2 specs."
7
+ end
8
+ require 'yajl/gzip'
9
+ require 'yajl/deflate'
10
+ require 'yajl/http_stream'
11
+
12
+ def parse_off_headers(io)
13
+ io.each_line do |line|
14
+ if line == "\r\n" # end of the headers
15
+ break
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "Yajl HTTP DELETE request" do
21
+ before(:all) do
22
+ raw = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.raw.dump'), 'r')
23
+ parse_off_headers(raw)
24
+ @template_hash = Yajl::Parser.parse(raw)
25
+
26
+ raw.rewind
27
+ parse_off_headers(raw)
28
+ @template_hash_symbolized = Yajl::Parser.parse(raw, :symbolize_keys => true)
29
+
30
+ @deflate = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.deflate.dump'), 'r')
31
+ @gzip = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.gzip.dump'), 'r')
32
+ end
33
+
34
+ after(:each) do
35
+ @file_path = nil
36
+ end
37
+
38
+ def prepare_mock_request_dump(format=:raw)
39
+ @request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.#{format}.dump"), 'r')
40
+ @uri = 'file://'+File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.#{format}.dump")
41
+ TCPSocket.should_receive(:new).and_return(@request)
42
+ @request.should_receive(:write)
43
+ end
44
+
45
+ it "should parse a raw response" do
46
+ prepare_mock_request_dump :raw
47
+ @template_hash.should == Yajl::HttpStream.delete(@uri)
48
+ end
49
+
50
+ it "should parse a raw response using instance method" do
51
+ prepare_mock_request_dump :raw
52
+ @uri.should_receive(:host)
53
+ @uri.should_receive(:port)
54
+ stream = Yajl::HttpStream.new
55
+ @template_hash.should == stream.delete(@uri)
56
+ end
57
+
58
+ it "should parse a raw response and symbolize keys" do
59
+ prepare_mock_request_dump :raw
60
+ @template_hash_symbolized.should == Yajl::HttpStream.delete(@uri, :symbolize_keys => true)
61
+ end
62
+
63
+ if defined?(Yajl::Bzip2::StreamReader)
64
+ it "should parse a bzip2 compressed response" do
65
+ prepare_mock_request_dump :bzip2
66
+ @template_hash.should == Yajl::HttpStream.delete(@uri)
67
+ end
68
+
69
+ it "should parse a bzip2 compressed response and symbolize keys" do
70
+ prepare_mock_request_dump :bzip2
71
+ @template_hash_symbolized.should == Yajl::HttpStream.delete(@uri, :symbolize_keys => true)
72
+ end
73
+ end
74
+
75
+ it "should parse a deflate compressed response" do
76
+ prepare_mock_request_dump :deflate
77
+ @template_hash.should == Yajl::HttpStream.delete(@uri)
78
+ end
79
+
80
+ it "should parse a deflate compressed response and symbolize keys" do
81
+ prepare_mock_request_dump :deflate
82
+ @template_hash_symbolized.should == Yajl::HttpStream.delete(@uri, :symbolize_keys => true)
83
+ end
84
+
85
+ it "should parse a gzip compressed response" do
86
+ prepare_mock_request_dump :gzip
87
+ @template_hash.should == Yajl::HttpStream.delete(@uri)
88
+ end
89
+
90
+ it "should parse a gzip compressed response and symbolize keys" do
91
+ prepare_mock_request_dump :gzip
92
+ @template_hash_symbolized.should == Yajl::HttpStream.delete(@uri, :symbolize_keys => true)
93
+ end
94
+
95
+ it "should raise when an HTTP code that isn't 200 is returned" do
96
+ prepare_mock_request_dump :error
97
+ lambda { Yajl::HttpStream.delete(@uri) }.should raise_exception(Yajl::HttpStream::HttpError)
98
+ end
99
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
+ begin
4
+ require 'yajl/bzip2'
5
+ rescue
6
+ warn "Couldn't load yajl/bzip2, maybe you don't have bzip2-ruby installed? Continuing without running bzip2 specs."
7
+ end
8
+ require 'yajl/gzip'
9
+ require 'yajl/deflate'
10
+ require 'yajl/http_stream'
11
+
12
+ describe "Yajl HTTP error" do
13
+ before(:all) do
14
+ @request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.error.dump"), 'r')
15
+ @uri = 'file://'+File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.error.dump")
16
+ TCPSocket.should_receive(:new).and_return(@request)
17
+ @request.should_receive(:write)
18
+
19
+ begin
20
+ Yajl::HttpStream.get(@uri)
21
+ rescue Yajl::HttpStream::HttpError => e
22
+ @error = e
23
+ end
24
+ end
25
+
26
+ it "should contain the error code in the message" do
27
+ @error.message.should match(/404/)
28
+ end
29
+
30
+ it "should provide the HTTP response headers" do
31
+ @error.headers.keys.should include('ETag', 'Content-Length', 'Server')
32
+ end
33
+ end
@@ -0,0 +1,110 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
+ begin
4
+ require 'yajl/bzip2'
5
+ rescue
6
+ warn "Couldn't load yajl/bzip2, maybe you don't have bzip2-ruby installed? Continuing without running bzip2 specs."
7
+ end
8
+ require 'yajl/gzip'
9
+ require 'yajl/deflate'
10
+ require 'yajl/http_stream'
11
+
12
+ def parse_off_headers(io)
13
+ io.each_line do |line|
14
+ if line == "\r\n" # end of the headers
15
+ break
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "Yajl HTTP GET request" do
21
+ before(:all) do
22
+ raw = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.raw.dump'), 'r')
23
+ parse_off_headers(raw)
24
+ @template_hash = Yajl::Parser.parse(raw)
25
+
26
+ raw.rewind
27
+ parse_off_headers(raw)
28
+ @template_hash_symbolized = Yajl::Parser.parse(raw, :symbolize_keys => true)
29
+
30
+ @deflate = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.deflate.dump'), 'r')
31
+ @gzip = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.gzip.dump'), 'r')
32
+ @chunked_body = {"item"=>{"price"=>1.99, "updated_by_id"=>nil, "cached_tag_list"=>"", "name"=>"generated", "created_at"=>"2009-03-24T05:25:09Z", "cost"=>0.597, "delta"=>false, "created_by_id"=>nil, "updated_at"=>"2009-03-24T05:25:09Z", "import_tag"=>nil, "account_id"=>16, "id"=>1, "taxable"=>true, "unit"=>nil, "sku"=>"06317-0306", "company_id"=>0, "description"=>nil, "active"=>true}}
33
+ end
34
+
35
+ after(:each) do
36
+ @file_path = nil
37
+ end
38
+
39
+ def prepare_mock_request_dump(format=:raw)
40
+ @request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.#{format}.dump"), 'r')
41
+ @uri = 'file://'+File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.#{format}.dump")
42
+ TCPSocket.should_receive(:new).and_return(@request)
43
+ @request.should_receive(:write)
44
+ end
45
+
46
+ it "should parse a raw response" do
47
+ prepare_mock_request_dump :raw
48
+ @template_hash.should == Yajl::HttpStream.get(@uri)
49
+ end
50
+
51
+ it "should parse a raw response and symbolize keys" do
52
+ prepare_mock_request_dump :raw
53
+ @template_hash_symbolized.should == Yajl::HttpStream.get(@uri, :symbolize_keys => true)
54
+ end
55
+
56
+ it "should parse a raw response using instance method" do
57
+ prepare_mock_request_dump :raw
58
+ @uri.should_receive(:host)
59
+ @uri.should_receive(:port)
60
+ stream = Yajl::HttpStream.new
61
+ @template_hash.should == stream.get(@uri)
62
+ end
63
+
64
+ it "should parse a chunked response using instance method" do
65
+ prepare_mock_request_dump :chunked
66
+ @uri.should_receive(:host)
67
+ @uri.should_receive(:port)
68
+ stream = Yajl::HttpStream.new
69
+ stream.get(@uri) do |obj|
70
+ obj.should eql(@chunked_body)
71
+ end
72
+ end
73
+
74
+ if defined?(Yajl::Bzip2::StreamReader)
75
+ it "should parse a bzip2 compressed response" do
76
+ prepare_mock_request_dump :bzip2
77
+ @template_hash.should == Yajl::HttpStream.get(@uri)
78
+ end
79
+
80
+ it "should parse a bzip2 compressed response and symbolize keys" do
81
+ prepare_mock_request_dump :bzip2
82
+ @template_hash_symbolized.should == Yajl::HttpStream.get(@uri, :symbolize_keys => true)
83
+ end
84
+ end
85
+
86
+ it "should parse a deflate compressed response" do
87
+ prepare_mock_request_dump :deflate
88
+ @template_hash.should == Yajl::HttpStream.get(@uri)
89
+ end
90
+
91
+ it "should parse a deflate compressed response and symbolize keys" do
92
+ prepare_mock_request_dump :deflate
93
+ @template_hash_symbolized.should == Yajl::HttpStream.get(@uri, :symbolize_keys => true)
94
+ end
95
+
96
+ it "should parse a gzip compressed response" do
97
+ prepare_mock_request_dump :gzip
98
+ @template_hash.should == Yajl::HttpStream.get(@uri)
99
+ end
100
+
101
+ it "should parse a gzip compressed response and symbolize keys" do
102
+ prepare_mock_request_dump :gzip
103
+ @template_hash_symbolized.should == Yajl::HttpStream.get(@uri, :symbolize_keys => true)
104
+ end
105
+
106
+ it "should raise when an HTTP code that isn't 200 is returned" do
107
+ prepare_mock_request_dump :error
108
+ lambda { Yajl::HttpStream.get(@uri) }.should raise_exception(Yajl::HttpStream::HttpError)
109
+ end
110
+ end
@@ -0,0 +1,124 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
+ begin
4
+ require 'yajl/bzip2'
5
+ rescue
6
+ warn "Couldn't load yajl/bzip2, maybe you don't have bzip2-ruby installed? Continuing without running bzip2 specs."
7
+ end
8
+ require 'yajl/gzip'
9
+ require 'yajl/deflate'
10
+ require 'yajl/http_stream'
11
+
12
+ def parse_off_headers(io)
13
+ io.each_line do |line|
14
+ if line == "\r\n" # end of the headers
15
+ break
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "Yajl HTTP POST request" do
21
+ before(:all) do
22
+ raw = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.raw.dump'), 'r')
23
+ parse_off_headers(raw)
24
+ @template_hash = Yajl::Parser.parse(raw)
25
+
26
+ raw.rewind
27
+ parse_off_headers(raw)
28
+ @template_hash_symbolized = Yajl::Parser.parse(raw, :symbolize_keys => true)
29
+
30
+ @deflate = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.deflate.dump'), 'r')
31
+ @gzip = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.gzip.dump'), 'r')
32
+ @body = "blah=foo&bar=baz"
33
+ @hashed_body = {:blah => 'foo', 'bar' => 'baz'}
34
+ @chunked_body = {"item"=>{"price"=>1.99, "updated_by_id"=>nil, "cached_tag_list"=>"", "name"=>"generated", "created_at"=>"2009-03-24T05:25:09Z", "cost"=>0.597, "delta"=>false, "created_by_id"=>nil, "updated_at"=>"2009-03-24T05:25:09Z", "import_tag"=>nil, "account_id"=>16, "id"=>1, "taxable"=>true, "unit"=>nil, "sku"=>"06317-0306", "company_id"=>0, "description"=>nil, "active"=>true}}
35
+ end
36
+
37
+ after(:each) do
38
+ @file_path = nil
39
+ end
40
+
41
+ def prepare_mock_request_dump(format=:raw)
42
+ @request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.#{format}.dump"), 'r')
43
+ @uri = 'file://'+File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.#{format}.dump")
44
+ TCPSocket.should_receive(:new).and_return(@request)
45
+ @request.should_receive(:write)
46
+ end
47
+
48
+ it "should parse a raw response" do
49
+ prepare_mock_request_dump :raw
50
+ @template_hash.should == Yajl::HttpStream.post(@uri, @body)
51
+ end
52
+
53
+ it "should parse a raw response using instance method" do
54
+ prepare_mock_request_dump :raw
55
+ @uri.should_receive(:host)
56
+ @uri.should_receive(:port)
57
+ stream = Yajl::HttpStream.new
58
+ @template_hash.should == stream.post(@uri, @body)
59
+ end
60
+
61
+ it "should parse a raw response with hashed body" do
62
+ prepare_mock_request_dump :raw
63
+ @template_hash.should == Yajl::HttpStream.post(@uri, @hashed_body)
64
+ end
65
+
66
+ it "should parse a raw response and symbolize keys" do
67
+ prepare_mock_request_dump :raw
68
+ @template_hash_symbolized.should == Yajl::HttpStream.post(@uri, @body, :symbolize_keys => true)
69
+ end
70
+
71
+ if defined?(Yajl::Bzip2::StreamReader)
72
+ it "should parse a bzip2 compressed response" do
73
+ prepare_mock_request_dump :bzip2
74
+ @template_hash.should == Yajl::HttpStream.post(@uri, @body)
75
+ end
76
+
77
+ it "should parse a bzip2 compressed response and symbolize keys" do
78
+ prepare_mock_request_dump :bzip2
79
+ @template_hash_symbolized.should == Yajl::HttpStream.post(@uri, @body, :symbolize_keys => true)
80
+ end
81
+ end
82
+
83
+ it "should parse a deflate compressed response" do
84
+ prepare_mock_request_dump :deflate
85
+ @template_hash.should == Yajl::HttpStream.post(@uri, @body)
86
+ end
87
+
88
+ it "should parse a deflate compressed response and symbolize keys" do
89
+ prepare_mock_request_dump :deflate
90
+ @template_hash_symbolized.should == Yajl::HttpStream.post(@uri, @body, :symbolize_keys => true)
91
+ end
92
+
93
+ it "should parse a gzip compressed response" do
94
+ prepare_mock_request_dump :gzip
95
+ @template_hash.should == Yajl::HttpStream.post(@uri, @body)
96
+ end
97
+
98
+ it "should parse a gzip compressed response and symbolize keys" do
99
+ prepare_mock_request_dump :gzip
100
+ @template_hash_symbolized.should == Yajl::HttpStream.post(@uri, @body, :symbolize_keys => true)
101
+ end
102
+
103
+ it "should parse a chunked raw response" do
104
+ prepare_mock_request_dump :chunked
105
+ Yajl::HttpStream.post(@uri, @body) do |obj|
106
+ obj.should eql(@chunked_body)
107
+ end
108
+ end
109
+
110
+ it "should throw Exception if chunked response and no block given" do
111
+ prepare_mock_request_dump :chunked
112
+ lambda {Yajl::HttpStream.post(@uri, @body)}.should raise_error(Exception)
113
+ end
114
+
115
+ it "should throw InvalidContentType if unable to handle the MIME type" do
116
+ prepare_mock_request_dump :html
117
+ lambda {Yajl::HttpStream.post(@uri, @body)}.should raise_error(Yajl::HttpStream::InvalidContentType)
118
+ end
119
+
120
+ it "should raise when an HTTP code that isn't 200 is returned" do
121
+ prepare_mock_request_dump :error
122
+ lambda { Yajl::HttpStream.post(@uri, @body) }.should raise_exception(Yajl::HttpStream::HttpError)
123
+ end
124
+ end
@@ -0,0 +1,106 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
+ begin
4
+ require 'yajl/bzip2'
5
+ rescue
6
+ warn "Couldn't load yajl/bzip2, maybe you don't have bzip2-ruby installed? Continuing without running bzip2 specs."
7
+ end
8
+ require 'yajl/gzip'
9
+ require 'yajl/deflate'
10
+ require 'yajl/http_stream'
11
+
12
+ def parse_off_headers(io)
13
+ io.each_line do |line|
14
+ if line == "\r\n" # end of the headers
15
+ break
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "Yajl HTTP PUT request" do
21
+ before(:all) do
22
+ raw = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.raw.dump'), 'r')
23
+ parse_off_headers(raw)
24
+ @template_hash = Yajl::Parser.parse(raw)
25
+
26
+ raw.rewind
27
+ parse_off_headers(raw)
28
+ @template_hash_symbolized = Yajl::Parser.parse(raw, :symbolize_keys => true)
29
+
30
+ @deflate = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.deflate.dump'), 'r')
31
+ @gzip = File.new(File.expand_path(File.dirname(__FILE__) + '/fixtures/http.gzip.dump'), 'r')
32
+ @body = "blah=foo&bar=baz"
33
+ @hashed_body = {:blah => 'foo', 'bar' => 'baz'}
34
+ end
35
+
36
+ after(:each) do
37
+ @file_path = nil
38
+ end
39
+
40
+ def prepare_mock_request_dump(format=:raw)
41
+ @request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.#{format}.dump"), 'r')
42
+ @uri = 'file://'+File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.#{format}.dump")
43
+ TCPSocket.should_receive(:new).and_return(@request)
44
+ @request.should_receive(:write)
45
+ end
46
+
47
+ it "should parse a raw response" do
48
+ prepare_mock_request_dump :raw
49
+ @template_hash.should == Yajl::HttpStream.put(@uri, @body)
50
+ end
51
+
52
+ it "should parse a raw response using instance method" do
53
+ prepare_mock_request_dump :raw
54
+ @uri.should_receive(:host)
55
+ @uri.should_receive(:port)
56
+ stream = Yajl::HttpStream.new
57
+ @template_hash.should == stream.put(@uri, @body)
58
+ end
59
+
60
+ it "should parse a raw response with hashed body" do
61
+ prepare_mock_request_dump :raw
62
+ @template_hash.should == Yajl::HttpStream.post(@uri, @hashed_body)
63
+ end
64
+
65
+ it "should parse a raw response and symbolize keys" do
66
+ prepare_mock_request_dump :raw
67
+ @template_hash_symbolized.should == Yajl::HttpStream.put(@uri, @body, :symbolize_keys => true)
68
+ end
69
+
70
+ if defined?(Yajl::Bzip2::StreamReader)
71
+ it "should parse a bzip2 compressed response" do
72
+ prepare_mock_request_dump :bzip2
73
+ @template_hash.should == Yajl::HttpStream.put(@uri, @body)
74
+ end
75
+
76
+ it "should parse a bzip2 compressed response and symbolize keys" do
77
+ prepare_mock_request_dump :bzip2
78
+ @template_hash_symbolized.should == Yajl::HttpStream.put(@uri, @body, :symbolize_keys => true)
79
+ end
80
+ end
81
+
82
+ it "should parse a deflate compressed response" do
83
+ prepare_mock_request_dump :deflate
84
+ @template_hash.should == Yajl::HttpStream.put(@uri, @body)
85
+ end
86
+
87
+ it "should parse a deflate compressed response and symbolize keys" do
88
+ prepare_mock_request_dump :deflate
89
+ @template_hash_symbolized.should == Yajl::HttpStream.put(@uri, @body, :symbolize_keys => true)
90
+ end
91
+
92
+ it "should parse a gzip compressed response" do
93
+ prepare_mock_request_dump :gzip
94
+ @template_hash.should == Yajl::HttpStream.put(@uri, @body)
95
+ end
96
+
97
+ it "should parse a gzip compressed response and symbolize keys" do
98
+ prepare_mock_request_dump :gzip
99
+ @template_hash_symbolized.should == Yajl::HttpStream.put(@uri, @body, :symbolize_keys => true)
100
+ end
101
+
102
+ it "should raise when an HTTP code that isn't 200 is returned" do
103
+ prepare_mock_request_dump :error
104
+ lambda { Yajl::HttpStream.put(@uri, @body) }.should raise_exception(Yajl::HttpStream::HttpError)
105
+ end
106
+ end
@@ -0,0 +1,203 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
+
4
+ class Dummy; end
5
+
6
+ describe "JSON Gem compatability API" do
7
+ it "shoud not mixin #to_json on base objects until compatability has been enabled" do
8
+ d = Dummy.new
9
+
10
+ d.respond_to?(:to_json).should_not be_true
11
+ "".respond_to?(:to_json).should_not be_true
12
+ 1.respond_to?(:to_json).should_not be_true
13
+ "1.5".to_f.respond_to?(:to_json).should_not be_true
14
+ [].respond_to?(:to_json).should_not be_true
15
+ {:foo => "bar"}.respond_to?(:to_json).should_not be_true
16
+ true.respond_to?(:to_json).should_not be_true
17
+ false.respond_to?(:to_json).should_not be_true
18
+ nil.respond_to?(:to_json).should_not be_true
19
+ end
20
+
21
+ it "should mixin #to_json on base objects after compatability has been enabled" do
22
+ require 'yajl/json_gem'
23
+ d = Dummy.new
24
+
25
+ d.respond_to?(:to_json).should be_true
26
+ "".respond_to?(:to_json).should be_true
27
+ 1.respond_to?(:to_json).should be_true
28
+ "1.5".to_f.respond_to?(:to_json).should be_true
29
+ [].respond_to?(:to_json).should be_true
30
+ {:foo => "bar"}.respond_to?(:to_json).should be_true
31
+ true.respond_to?(:to_json).should be_true
32
+ false.respond_to?(:to_json).should be_true
33
+ nil.respond_to?(:to_json).should be_true
34
+ end
35
+
36
+ it "should require yajl/json_gem to enable the compatability API" do
37
+ defined?(JSON).should be_true
38
+
39
+ JSON.respond_to?(:parse).should be_true
40
+ JSON.respond_to?(:generate).should be_true
41
+ JSON.respond_to?(:pretty_generate).should be_true
42
+ JSON.respond_to?(:load).should be_true
43
+ JSON.respond_to?(:dump).should be_true
44
+ end
45
+
46
+ it "should allow default parsing options be set with JSON.default_options" do
47
+ default = JSON.default_options[:symbolize_keys]
48
+ JSON.parse('{"foo": 1234}').should === {"foo" => 1234}
49
+ JSON.default_options[:symbolize_keys] = true
50
+ JSON.parse('{"foo": 1234}').should === {:foo => 1234}
51
+ JSON.default_options[:symbolize_keys] = default # ensure the rest of the test cases expect the default
52
+ end
53
+
54
+ it "should encode arbitrary classes via their default to_json method" do
55
+ d = Dummy.new
56
+ d.to_json.should == "\"#{d.to_s}\""
57
+
58
+ t = Time.now
59
+ t.to_json.should == "\"#{t.to_s}\""
60
+
61
+ da = Date.today
62
+ da.to_json.should == "\"#{da.to_s}\""
63
+
64
+ dt = DateTime.new
65
+ dt.to_json.should == "\"#{dt.to_s}\""
66
+ end
67
+
68
+ it "should have the standard parsing and encoding exceptions mapped" do
69
+ JSON::JSONError.new.is_a?(StandardError).should be_true
70
+ JSON::ParserError.new.is_a?(JSON::JSONError).should be_true
71
+ JSON::GeneratorError.new.is_a?(JSON::JSONError).should be_true
72
+
73
+ lambda {
74
+ JSON.parse("blah")
75
+ }.should raise_error(JSON::ParserError)
76
+
77
+ lambda {
78
+ JSON.generate(0.0/0.0)
79
+ }.should raise_error(JSON::GeneratorError)
80
+ end
81
+
82
+ context "ported tests for Unicode" do
83
+ it "should be able to encode and parse unicode" do
84
+ '""'.should eql(''.to_json)
85
+ '"\\b"'.should eql("\b".to_json)
86
+ '"\u0001"'.should eql(0x1.chr.to_json)
87
+ '"\u001F"'.should eql(0x1f.chr.to_json)
88
+ '" "'.should eql(' '.to_json)
89
+ "\"#{0x7f.chr}\"".should eql(0x7f.chr.to_json)
90
+ utf8 = [ "© ≠ €! \01" ]
91
+ json = "[\"© ≠ €! \\u0001\"]"
92
+ json.should eql(utf8.to_json)
93
+ utf8.should eql(JSON.parse(json))
94
+ utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
95
+ json = "[\"あいうえお\"]"
96
+ json.should eql(utf8.to_json)
97
+ utf8.should eql(JSON.parse(json))
98
+ utf8 = ['საქართველო']
99
+ json = "[\"საქართველო\"]"
100
+ json.should eql(utf8.to_json)
101
+ utf8.should eql(JSON.parse(json))
102
+ '["Ã"]'.should eql(JSON.generate(["Ã"]))
103
+ ["€"].should eql(JSON.parse('["\u20ac"]'))
104
+ utf8_str = "\xf0\xa0\x80\x81"
105
+ utf8 = [utf8_str]
106
+ json = "[\"#{utf8_str}\"]"
107
+ json.should eql(JSON.generate(utf8))
108
+ utf8.should eql(JSON.parse(json))
109
+ end
110
+ end
111
+
112
+ context "ported tests for generation" do
113
+ before(:all) do
114
+ @hash = {
115
+ 'a' => 2,
116
+ 'b' => 3.141,
117
+ 'c' => 'c',
118
+ 'd' => [ 1, "b", 3.14 ],
119
+ 'e' => { 'foo' => 'bar' },
120
+ 'g' => "blah",
121
+ 'h' => 1000.0,
122
+ 'i' => 0.001
123
+ }
124
+
125
+ @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},"g":"blah","h":1000.0,"i":0.001}'
126
+
127
+ @json3 = %{
128
+ {
129
+ "a": 2,
130
+ "b": 3.141,
131
+ "c": "c",
132
+ "d": [1, "b", 3.14],
133
+ "e": {"foo": "bar"},
134
+ "g": "blah",
135
+ "h": 1000.0,
136
+ "i": 0.001
137
+ }
138
+ }.chomp
139
+ end
140
+
141
+ it "should be able to unparse" do
142
+ json = JSON.generate(@hash)
143
+ JSON.parse(@json2).should == JSON.parse(json)
144
+ parsed_json = JSON.parse(json)
145
+ @hash.should == parsed_json
146
+ json = JSON.generate({1=>2})
147
+ '{"1":2}'.should eql(json)
148
+ parsed_json = JSON.parse(json)
149
+ {"1"=>2}.should == parsed_json
150
+ end
151
+
152
+ it "should be able to unparse pretty" do
153
+ json = JSON.pretty_generate(@hash)
154
+ JSON.parse(@json3).should == JSON.parse(json)
155
+ parsed_json = JSON.parse(json)
156
+ @hash.should == parsed_json
157
+ json = JSON.pretty_generate({1=>2})
158
+ test = "{\n \"1\": 2\n}".chomp
159
+ test.should == json
160
+ parsed_json = JSON.parse(json)
161
+ {"1"=>2}.should == parsed_json
162
+ end
163
+ end
164
+
165
+ context "ported fixture tests" do
166
+ fixtures = File.join(File.dirname(__FILE__), '../parsing/fixtures/*.json')
167
+ passed, failed = Dir[fixtures].partition { |f| f['pass'] }
168
+ JSON_PASSED = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
169
+ JSON_FAILED = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
170
+
171
+ JSON_FAILED.each do |name, source|
172
+ it "should not be able to parse #{File.basename(name)} as an IO" do
173
+ lambda {
174
+ JSON.parse(StringIO.new(source))
175
+ }.should raise_error(JSON::ParserError)
176
+ end
177
+ end
178
+
179
+ JSON_FAILED.each do |name, source|
180
+ it "should not be able to parse #{File.basename(name)} as a string" do
181
+ lambda {
182
+ JSON.parse(source)
183
+ }.should raise_error(JSON::ParserError)
184
+ end
185
+ end
186
+
187
+ JSON_PASSED.each do |name, source|
188
+ it "should be able to parse #{File.basename(name)} as an IO" do
189
+ lambda {
190
+ JSON.parse(StringIO.new(source))
191
+ }.should_not raise_error(JSON::ParserError)
192
+ end
193
+ end
194
+
195
+ JSON_PASSED.each do |name, source|
196
+ it "should be able to parse #{File.basename(name)} as a string" do
197
+ lambda {
198
+ JSON.parse(source)
199
+ }.should_not raise_error(JSON::ParserError)
200
+ end
201
+ end
202
+ end
203
+ end