mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
More robust handling of mime types (Alex Vollmer)
This commit is contained in:
parent
84a2237d04
commit
a2686cf97b
4 changed files with 53 additions and 5 deletions
3
History
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
|
||||
|
|
|
@ -9,8 +9,17 @@ require 'json'
|
|||
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 @@ module HTTParty
|
|||
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
|
||||
|
||||
|
|
|
@ -116,7 +116,7 @@ module HTTParty
|
|||
# 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:
|
||||
|
|
|
@ -28,6 +28,44 @@ describe HTTParty::Request do
|
|||
@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
|
||||
|
|
Loading…
Reference in a new issue