Fix HEADER_VALUE_WITH_PARAMS regex and added a test case for this regex. #679

Merged
merged 2 commits into from Mar 14, 2013
View
2 lib/sinatra/base.rb
@@ -17,7 +17,7 @@ module Sinatra
# http://rack.rubyforge.org/doc/classes/Rack/Request.html
class Request < Rack::Request
HEADER_PARAM = /\s*[\w.]+=(?:[\w.]+|"(?:[^"\\]|\\.)*")?\s*/
- HEADER_VALUE_WITH_PARAMS = /(?:(?:\w+|\*)\/(?:\w+|\*))\s*(?:;#{HEADER_PARAM})*/
+ HEADER_VALUE_WITH_PARAMS = /(?:(?:\w+|\*)\/(?:\w+(?:\.|\-|\+)?|\*)*)\s*(?:;#{HEADER_PARAM})*/
# Returns an array of acceptable media types for the response
def accept
View
123 test/routing_test.rb
@@ -768,6 +768,119 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 'Hello Bar', body
end
+ it 'matches mime_types with dots, hyphens and plus signs' do
+ mime_types = %w(
+ application/atom+xml
+ application/ecmascript
+ application/EDI-X12
+ application/EDIFACT
+ application/json
+ application/javascript
+ application/octet-stream
+ application/ogg
+ application/pdf
+ application/postscript
+ application/rdf+xml
+ application/rss+xml
+ application/soap+xml
+ application/font-woff
+ application/xhtml+xml
+ application/xml
+ application/xml-dtd
+ application/xop+xml
+ application/zip
+ application/gzip
+ audio/basic
+ audio/L24
+ audio/mp4
+ audio/mpeg
+ audio/ogg
+ audio/vorbis
+ audio/vnd.rn-realaudio
+ audio/vnd.wave
+ audio/webm
+ image/gif
+ image/jpeg
+ image/pjpeg
+ image/png
+ image/svg+xml
+ image/tiff
+ image/vnd.microsoft.icon
+ message/http
+ message/imdn+xml
+ message/partial
+ message/rfc822
+ model/example
+ model/iges
+ model/mesh
+ model/vrml
+ model/x3d+binary
+ model/x3d+vrml
+ model/x3d+xml
+ multipart/mixed
+ multipart/alternative
+ multipart/related
+ multipart/form-data
+ multipart/signed
+ multipart/encrypted
+ text/cmd
+ text/css
+ text/csv
+ text/html
+ text/javascript
+ application/javascript
+ text/plain
+ text/vcard
+ text/xml
+ video/mpeg
+ video/mp4
+ video/ogg
+ video/quicktime
+ video/webm
+ video/x-matroska
+ video/x-ms-wmv
+ video/x-flv
+ application/vnd.oasis.opendocument.text
+ application/vnd.oasis.opendocument.spreadsheet
+ application/vnd.oasis.opendocument.presentation
+ application/vnd.oasis.opendocument.graphics
+ application/vnd.ms-excel
+ application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
+ application/vnd.ms-powerpoint
+ application/vnd.openxmlformats-officedocument.presentationml.presentation
+ application/vnd.openxmlformats-officedocument.wordprocessingml.document
+ application/vnd.mozilla.xul+xml
+ application/vnd.google-earth.kml+xml
+ application/x-deb
+ application/x-dvi
+ application/x-font-ttf
+ application/x-javascript
+ application/x-latex
+ application/x-mpegURL
+ application/x-rar-compressed
+ application/x-shockwave-flash
+ application/x-stuffit
+ application/x-tar
+ application/x-www-form-urlencoded
+ application/x-xpinstall
+ audio/x-aac
+ audio/x-caf
+ image/x-xcf
+ text/x-gwt-rpc
+ text/x-jquery-tmpl
+ application/x-pkcs12
+ application/x-pkcs12
+ application/x-pkcs7-certificates
+ application/x-pkcs7-certificates
+ application/x-pkcs7-certreqresp
+ application/x-pkcs7-mime
+ application/x-pkcs7-mime
+ application/x-pkcs7-signature
+ )
+
+ mime_types.each { |mime_type| assert mime_type.match(Sinatra::Request::HEADER_VALUE_WITH_PARAMS) }
+ end
+
it "filters by accept header" do
mock_app {
get '/', :provides => :xml do
@@ -776,6 +889,9 @@ class RoutingTest < Test::Unit::TestCase
get '/foo', :provides => :html do
env['HTTP_ACCEPT']
end
+ get '/stream', :provides => 'text/event-stream' do
+ env['HTTP_ACCEPT']
+ end
}
get '/', {}, { 'HTTP_ACCEPT' => 'application/xml' }
@@ -792,6 +908,13 @@ class RoutingTest < Test::Unit::TestCase
get '/foo', {}, { 'HTTP_ACCEPT' => '' }
assert !ok?
+
+ get '/stream', {}, { 'HTTP_ACCEPT' => 'text/event-stream' }
+ assert ok?
+ assert_equal 'text/event-stream', body
+
+ get '/stream', {}, { 'HTTP_ACCEPT' => '' }
+ assert !ok?
end
it "filters by current Content-Type" do