Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make AcceptEntry behave like string

  • Loading branch information...
commit a045a3f8f6c6b62b1d497c2fd8b6028086013671 1 parent 2a4811b
@rkh rkh authored
Showing with 26 additions and 6 deletions.
  1. +15 −6 lib/sinatra/base.rb
  2. +11 −0 test/request_test.rb
View
21 lib/sinatra/base.rb
@@ -71,9 +71,10 @@ def initialize(entry)
[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 @@ def priority
[ @q, -@type.count('*'), @params.size ]
end
- def [](param)

Has [] been dropped now or is it available via Rack?

@rkh Owner
rkh added a note

[] has not been tested nor documented, nor has it been part of a release yet. With this commit, [] will behave like Stinrg#[]. Note that this is params on the AcceptEntry, not on Request.

ah, ok. Should've checked more carefully. Assumed it was on Request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- @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
View
11 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"'
@burningTyger

Has [] been dropped now or is it available via Rack?

@rkh

[] has not been tested nor documented, nor has it been part of a release yet. With this commit, [] will behave like Stinrg#[]. Note that this is params on the AcceptEntry, not on Request.

@burningTyger

ah, ok. Should've checked more carefully. Assumed it was on Request.

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