From a045a3f8f6c6b62b1d497c2fd8b6028086013671 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sun, 10 Mar 2013 12:39:29 +0100 Subject: [PATCH] make AcceptEntry behave like string --- lib/sinatra/base.rb | 21 +++++++++++++++------ test/request_test.rb | 11 +++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index c91a981d..8caca0d7 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -71,9 +71,10 @@ module Sinatra [key, value] end - @type = entry[/[^;]+/].delete(' ') + @entry = entry + @type = entry[/[^;]+/].delete(' ') @params = Hash[params] - @q = @params.delete('q') { "1.0" }.to_f + @q = @params.delete('q') { "1.0" }.to_f end def <=>(other) @@ -85,13 +86,21 @@ module Sinatra [ @q, -@type.count('*'), @params.size ] end - def [](param) - @params[param] - end - def to_str @type end + + def to_s(full = false) + full ? entry : to_str + end + + def respond_to?(*args) + super or to_str.respond_to?(*args) + end + + def method_missing(*args, &block) + to_str.send(*args, &block) + end end end diff --git a/test/request_test.rb b/test/request_test.rb index fa28bd8b..0f25343f 100644 --- a/test/request_test.rb +++ b/test/request_test.rb @@ -50,6 +50,17 @@ class RequestTest < Test::Unit::TestCase assert_equal({ 'compress' => '0.25' }, request.preferred_type.params) end + it "makes accept types behave like strings" do + request = Sinatra::Request.new('HTTP_ACCEPT' => 'image/jpeg; compress=0.25') + assert_equal 'image/jpeg', request.preferred_type.to_s + assert_equal 'image/jpeg', request.preferred_type.to_str + assert_equal 'image', request.preferred_type.split('/').first + + String.instance_methods.each do |method| + assert request.preferred_type.respond_to? method + end + end + it "properly decodes MIME type parameters" do request = Sinatra::Request.new( 'HTTP_ACCEPT' => 'image/jpeg;unquoted=0.25;quoted="0.25";chartest="\";,\x"'