Improve checking of JSON MIME

This commit is contained in:
xhh 2015-12-09 12:38:38 +08:00
parent b896c9169a
commit 4fdaeb7371
3 changed files with 85 additions and 34 deletions

View File

@ -86,6 +86,15 @@ module {{moduleName}}
Typhoeus::Request.new(url, req_opts)
end
# Check if the given MIME is a JSON MIME.
# JSON MIME examples:
# application/json
# application/json; charset=UTF8
# APPLICATION/JSON
def json_mime?(mime)
!!(mime =~ /\Aapplication\/json(;.*)?\z/i)
end
# Deserialize the response to the given return type.
#
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
@ -99,9 +108,7 @@ module {{moduleName}}
# ensuring a default content type
content_type = response.headers['Content-Type'] || 'application/json'
unless content_type.start_with?('application/json')
fail "Content-Type is not supported: #{content_type}"
end
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
begin
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
@ -228,26 +235,21 @@ module {{moduleName}}
# @param [Array] accepts array for Accept
# @return [String] the Accept header (e.g. application/json)
def select_header_accept(accepts)
if accepts.empty?
return
elsif accepts.any?{ |s| s.casecmp('application/json') == 0 }
'application/json' # look for json data by default
else
accepts.join(',')
end
return nil if accepts.nil? || accepts.empty?
# use JSON when present, otherwise use all of the provided
json_accept = accepts.find { |s| json_mime?(s) }
return json_accept || accepts.join(',')
end
# Return Content-Type header based on an array of content types provided.
# @param [Array] content_types array for Content-Type
# @return [String] the Content-Type header (e.g. application/json)
def select_header_content_type(content_types)
if content_types.empty?
'application/json' # use application/json by default
elsif content_types.any?{ |s| s.casecmp('application/json')==0 }
'application/json' # use application/json if it's included
else
content_types[0] # otherwise, use the first one
end
# use application/json by default
return 'application/json' if content_types.nil? || content_types.empty?
# use JSON when present, otherwise use the first one
json_content_type = content_types.find { |s| json_mime?(s) }
return json_content_type || content_types.first
end
# Convert object (array, hash, object, etc) to JSON string.

View File

@ -86,6 +86,15 @@ module Petstore
Typhoeus::Request.new(url, req_opts)
end
# Check if the given MIME is a JSON MIME.
# JSON MIME examples:
# application/json
# application/json; charset=UTF8
# APPLICATION/JSON
def json_mime?(mime)
!!(mime =~ /\Aapplication\/json(;.*)?\z/i)
end
# Deserialize the response to the given return type.
#
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
@ -99,9 +108,7 @@ module Petstore
# ensuring a default content type
content_type = response.headers['Content-Type'] || 'application/json'
unless content_type.start_with?('application/json')
fail "Content-Type is not supported: #{content_type}"
end
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
begin
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
@ -228,26 +235,21 @@ module Petstore
# @param [Array] accepts array for Accept
# @return [String] the Accept header (e.g. application/json)
def select_header_accept(accepts)
if accepts.empty?
return
elsif accepts.any?{ |s| s.casecmp('application/json') == 0 }
'application/json' # look for json data by default
else
accepts.join(',')
end
return nil if accepts.nil? || accepts.empty?
# use JSON when present, otherwise use all of the provided
json_accept = accepts.find { |s| json_mime?(s) }
return json_accept || accepts.join(',')
end
# Return Content-Type header based on an array of content types provided.
# @param [Array] content_types array for Content-Type
# @return [String] the Content-Type header (e.g. application/json)
def select_header_content_type(content_types)
if content_types.empty?
'application/json' # use application/json by default
elsif content_types.any?{ |s| s.casecmp('application/json')==0 }
'application/json' # use application/json if it's included
else
content_types[0] # otherwise, use the first one
end
# use application/json by default
return 'application/json' if content_types.nil? || content_types.empty?
# use JSON when present, otherwise use the first one
json_content_type = content_types.find { |s| json_mime?(s) }
return json_content_type || content_types.first
end
# Convert object (array, hash, object, etc) to JSON string.

View File

@ -145,4 +145,51 @@ describe Petstore::ApiClient do
end
end
describe "#json_mime?" do
let(:api_client) { Petstore::ApiClient.new }
it "works" do
api_client.json_mime?(nil).should == false
api_client.json_mime?('').should == false
api_client.json_mime?('application/json').should == true
api_client.json_mime?('application/json; charset=UTF8').should == true
api_client.json_mime?('APPLICATION/JSON').should == true
api_client.json_mime?('application/xml').should == false
api_client.json_mime?('text/plain').should == false
end
end
describe "#select_header_accept" do
let(:api_client) { Petstore::ApiClient.new }
it "works" do
api_client.select_header_accept(nil).should == nil
api_client.select_header_accept([]).should == nil
api_client.select_header_accept(['application/json']).should == 'application/json'
api_client.select_header_accept(['application/xml', 'application/json; charset=UTF8']).should == 'application/json; charset=UTF8'
api_client.select_header_accept(['APPLICATION/JSON', 'text/html']).should == 'APPLICATION/JSON'
api_client.select_header_accept(['application/xml']).should == 'application/xml'
api_client.select_header_accept(['text/html', 'application/xml']).should == 'text/html,application/xml'
end
end
describe "#select_header_content_type" do
let(:api_client) { Petstore::ApiClient.new }
it "works" do
api_client.select_header_content_type(nil).should == 'application/json'
api_client.select_header_content_type([]).should == 'application/json'
api_client.select_header_content_type(['application/json']).should == 'application/json'
api_client.select_header_content_type(['application/xml', 'application/json; charset=UTF8']).should == 'application/json; charset=UTF8'
api_client.select_header_content_type(['APPLICATION/JSON', 'text/html']).should == 'APPLICATION/JSON'
api_client.select_header_content_type(['application/xml']).should == 'application/xml'
api_client.select_header_content_type(['text/plain', 'application/xml']).should == 'text/plain'
end
end
end