Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Handle empty body with GET request and content-type application/json #15

Merged
merged 2 commits into from

3 participants

Alex Kira Justin Kronz John Weathers
Alex Kira

If you send a request with Content-Type application/json and empty body it breaks. Fix this case by checking for empty body before parsing json. Also included spec for this.

Here is stack trace:

JSON::ParserError: A JSON text must at least contain two octets!
        /Users/ak/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/json/common.rb:146:in `initialize'
        /Users/ak/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/json/common.rb:146:in `new'
        /Users/ak/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/json/common.rb:146:in `parse'
        /Users/ak/.rvm/gems/ruby-1.9.2-p290/bundler/gems/dzl-397e83337441/lib/dzl/request.rb:72:in `preformatted_params'
        /Users/ak/.rvm/gems/ruby-1.9.2-p290/bundler/gems/dzl-397e83337441/lib/dzl/request.rb:30:in `params'
        /Users/ak/.rvm/gems/ruby-1.9.2-p290/bundler/gems/dzl-397e83337441/lib/dzl/rack_interface.rb:110:in `log_request'
        /Users/ak/.rvm/gems/ruby-1.9.2-p290/bundler/gems/dzl-397e83337441/lib/dzl/rack_interface.rb:38:in `call'
Justin Kronz jkronz commented on the diff
spec/fun_with_params_spec.rb
@@ -110,7 +110,7 @@ def app; Dzl::Examples::FunWithParams; end
it 'converts :bar to time' do
get '/foo/2012-01-01' do |response|
response.status.should == 200
- JSON.parse(response.body)['params']['bar'].should == '2012-01-01T00:00:00-08:00'
+ JSON.parse(response.body)['params']['bar'].should include('2012-01-01T00:00:00-')
Justin Kronz
jkronz added a note

Are there + offset timezones?
include('2012-01-01T00:00:00')

Alex Kira
akira added a note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
John Weathers jweathers777 merged commit 8c22c68 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 11, 2012
  1. handle empty content for application/json

    Alex Kira authored
  2. tests break in different tzone - fix

    Alex Kira authored
This page is out of date. Refresh to see the latest.
2  lib/dzl/request.rb
View
@@ -68,7 +68,7 @@ def silent?
protected
def preformatted_params
@preformatted_params ||= begin
- if content_type == "application/json"
+ if content_type == "application/json" && !body.blank?
JSON.parse(body).recursively_symbolize_keys!
else
{}
7 spec/fun_with_handlers_spec.rb
View
@@ -29,6 +29,13 @@ def app; Dzl::Examples::FunWithHandlers; end
end
end
+ it "should handle empty body for content-type application/json" do
+ get '/say_bar?baz=no&bam=nope&bar=Hello%2C%20world', '', {"CONTENT_TYPE" => "application/json"} do |response|
+ response.status.should == 200
+ response.body.should == 'Hello, world'
+ end
+ end
+
it 'calls error hooks on errors' do
Object.should_receive(:first_error_hook)
Object.should_receive(:second_error_hook)
2  spec/fun_with_params_spec.rb
View
@@ -110,7 +110,7 @@ def app; Dzl::Examples::FunWithParams; end
it 'converts :bar to time' do
get '/foo/2012-01-01' do |response|
response.status.should == 200
- JSON.parse(response.body)['params']['bar'].should == '2012-01-01T00:00:00-08:00'
+ JSON.parse(response.body)['params']['bar'].should include('2012-01-01T00:00:00-')
Justin Kronz
jkronz added a note

Are there + offset timezones?
include('2012-01-01T00:00:00')

Alex Kira
akira added a note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
end
end
Something went wrong with that request. Please try again.