Use MultiJson, which will pick up whatever JSON backend you're using (JSO #45

Closed
wants to merge 4 commits into
from
@@ -1,8 +1,4 @@
-begin
- require 'json'
-rescue LoadError => e
- require 'json/pure'
-end
+require 'multi_json'
module Rack
@@ -30,7 +26,7 @@ def initialize(app)
def call(env)
if Rack::Request.new(env).media_type == APPLICATION_JSON && (body = env[POST_BODY].read).length != 0
- env.update(FORM_HASH => JSON.parse(body), FORM_INPUT => env[POST_BODY])
+ env.update(FORM_HASH => MultiJson.load(body), FORM_INPUT => env[POST_BODY])
end
@app.call(env)
end
@@ -99,9 +99,9 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = %w[README.rdoc COPYING]
s.add_dependency 'rack', '>= 0.9.1'
+ s.add_dependency 'multi_json', '>= 1.0.3'
s.add_development_dependency 'test-spec', '>= 0.9.0'
s.add_development_dependency 'tmail', '>= 1.2'
- s.add_development_dependency 'json', '>= 1.1'
s.has_rdoc = true
s.homepage = "http://github.com/rack/rack-contrib/"
@@ -1,40 +1,34 @@
require 'test/spec'
require 'rack/mock'
-begin
- require 'rack/contrib/post_body_content_type_parser'
+require 'rack/contrib/post_body_content_type_parser'
- context "Rack::PostBodyContentTypeParser" do
-
- specify "should parse 'application/json' requests" do
- params = params_for_request '{"key":"value"}', "application/json"
- params['key'].should.equal "value"
- end
-
- specify "should parse 'application/json; charset=utf-8' requests" do
- params = params_for_request '{"key":"value"}', "application/json; charset=utf-8"
- params['key'].should.equal "value"
- end
-
- specify "should parse 'application/json' requests with empty body" do
- params = params_for_request "", "application/json"
- params.should.equal({})
- end
-
- specify "shouldn't affect form-urlencoded requests" do
- params = params_for_request("key=value", "application/x-www-form-urlencoded")
- params['key'].should.equal "value"
- end
+context "Rack::PostBodyContentTypeParser" do
+ specify "should parse 'application/json' requests" do
+ params = params_for_request '{"key":"value"}', "application/json"
+ params['key'].should.equal "value"
end
- def params_for_request(body, content_type)
- env = Rack::MockRequest.env_for "/", {:method => "POST", :input => body, "CONTENT_TYPE" => content_type}
- app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, Rack::Request.new(env).POST] }
- Rack::PostBodyContentTypeParser.new(app).call(env).last
+ specify "should parse 'application/json; charset=utf-8' requests" do
+ params = params_for_request '{"key":"value"}', "application/json; charset=utf-8"
+ params['key'].should.equal "value"
end
-rescue LoadError => e
- # Missing dependency JSON, skipping tests.
- STDERR.puts "WARN: Skipping Rack::PostBodyContentTypeParser tests (json not installed)"
+ specify "should parse 'application/json' requests with empty body" do
+ params = params_for_request "", "application/json"
+ params.should.equal({})
+ end
+
+ specify "shouldn't affect form-urlencoded requests" do
+ params = params_for_request("key=value", "application/x-www-form-urlencoded")
+ params['key'].should.equal "value"
+ end
+
+end
+
+def params_for_request(body, content_type)
+ env = Rack::MockRequest.env_for "/", {:method => "POST", :input => body, "CONTENT_TYPE" => content_type}
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, Rack::Request.new(env).POST] }
+ Rack::PostBodyContentTypeParser.new(app).call(env).last
end