Skip to content
Browse files

More robust handling of mime types (Alex Vollmer)

  • Loading branch information...
1 parent 84a2237 commit a2686cf97bcdb15ca0c4af0fb57679eaf4eaf3c4 @jnunemaker committed
Showing with 53 additions and 5 deletions.
  1. +2 −1 History
  2. +12 −3 lib/httparty.rb
  3. +1 −1 lib/httparty/request.rb
  4. +38 −0 spec/httparty/request_spec.rb
View
3 History
@@ -1,7 +1,8 @@
== 0.2.7 2009-01-28
-* 2 minor fixes
+* 2 minor fixes, 1 minor enhancement
* fixed undefined method add_node for nil class error that occasionally happened (juliocesar)
* Handle nil or unexpected values better when typecasting. (Brian Landau)
+ * More robust handling of mime types (Alex Vollmer)
== 0.2.6 2009-01-05
* 1 minor bug fix
View
15 lib/httparty.rb
@@ -9,8 +9,17 @@
require 'module_level_inheritable_attributes'
require 'core_extensions'
-module HTTParty
- AllowedFormats = {:xml => 'text/xml', :json => 'application/json', :html => 'text/html'}
+module HTTParty
+
+ AllowedFormats = {
+ 'text/xml' => :xml,
+ 'application/xml' => :xml,
+ 'application/json' => :json,
+ 'text/json' => :json,
+ 'application/javascript' => :json,
+ 'text/javascript' => :json,
+ 'text/html' => :html
+ }
def self.included(base)
base.extend ClassMethods
@@ -51,7 +60,7 @@ def headers(h={})
end
def format(f)
- raise UnsupportedFormat, "Must be one of: #{AllowedFormats.keys.join(', ')}" unless AllowedFormats.key?(f)
+ raise UnsupportedFormat, "Must be one of: #{AllowedFormats.values.join(', ')}" unless AllowedFormats.value?(f)
default_options[:format] = f
end
View
2 lib/httparty/request.rb
@@ -116,7 +116,7 @@ def parse_response(body) #:nodoc:
# It compares the MIME type returned to the types stored in the AllowedFormats hash
def format_from_mimetype(mimetype) #:nodoc:
return nil if mimetype.nil?
- AllowedFormats.each { |k, v| return k if mimetype.include?(v) }
+ AllowedFormats.each { |k, v| return v if mimetype.include?(k) }
end
def validate! #:nodoc:
View
38 spec/httparty/request_spec.rb
@@ -28,6 +28,44 @@
@request.instance_variable_get(:@raw_request)['authorization'].should_not be_nil
end
end
+
+ describe '#format_from_mimetype' do
+ it 'should handle text/xml' do
+ ["text/xml", "text/xml; charset=iso8859-1"].each do |ct|
+ @request.send(:format_from_mimetype, ct).should == :xml
+ end
+ end
+
+ it 'should handle application/xml' do
+ ["application/xml", "application/xml; charset=iso8859-1"].each do |ct|
+ @request.send(:format_from_mimetype, ct).should == :xml
+ end
+ end
+
+ it 'should handle text/json' do
+ ["text/json", "text/json; charset=iso8859-1"].each do |ct|
+ @request.send(:format_from_mimetype, ct).should == :json
+ end
+ end
+
+ it 'should handle application/json' do
+ ["application/json", "application/json; charset=iso8859-1"].each do |ct|
+ @request.send(:format_from_mimetype, ct).should == :json
+ end
+ end
+
+ it 'should handle text/javascript' do
+ ["text/javascript", "text/javascript; charset=iso8859-1"].each do |ct|
+ @request.send(:format_from_mimetype, ct).should == :json
+ end
+ end
+
+ it 'should handle application/javascript' do
+ ["application/javascript", "application/javascript; charset=iso8859-1"].each do |ct|
+ @request.send(:format_from_mimetype, ct).should == :json
+ end
+ end
+ end
describe 'parsing responses' do
it 'should handle xml automatically' do

0 comments on commit a2686cf

Please sign in to comment.
Something went wrong with that request. Please try again.