diff options
-rw-r--r-- | lib/zero/request/accept_type.rb | 9 | ||||
-rw-r--r-- | spec/unit/request/accepttype/preferred_spec.rb | 17 |
2 files changed, 14 insertions, 12 deletions
diff --git a/lib/zero/request/accept_type.rb b/lib/zero/request/accept_type.rb index 918bf24..a8f5dc9 100644 --- a/lib/zero/request/accept_type.rb +++ b/lib/zero/request/accept_type.rb @@ -8,11 +8,8 @@ module Zero # create a new instance of AcceptType def initialize(string) - if string.nil? - @elements = [] - else - @elements = parse_elements(string) - end + string = '*/*' if string.nil? or string.empty? + @elements = parse_elements(string) end # return the preferred type @@ -30,7 +27,7 @@ module Zero # converts the accept string to a useable array # @param string the string containing media ranges and options - def parse_elements(string = '*/*') + def parse_elements(string) string. gsub(/\s/, ''). split(MEDIA_TYPE_SEPERATOR). diff --git a/spec/unit/request/accepttype/preferred_spec.rb b/spec/unit/request/accepttype/preferred_spec.rb index 1156aa0..ef21e8d 100644 --- a/spec/unit/request/accepttype/preferred_spec.rb +++ b/spec/unit/request/accepttype/preferred_spec.rb @@ -7,18 +7,23 @@ describe Zero::Request::AcceptType, '#preferred' do let(:foo) { 'text/foo' } let(:lower_quality) { foo + ';q=0.5' } let(:default) { '*/*;q=0.1' } + let(:option) { [foo + ';b=23', html].join(',') } let(:simple_accept) { [html, json].join(',') } let(:quality_accept) { [html, lower_quality, default].join(',') } let(:random_accept) { [lower_quality, default, html].join(',') } let(:lower_accept) { [lower_quality, default].join(',') } context 'without mapping' do - it { subject.new(html).preferred.should == html } - it { subject.new(json).preferred.should == json } - it { subject.new(simple_accept).preferred.should == html } - it { subject.new(quality_accept).preferred.should == html } - it { subject.new(random_accept).preferred.should == html } - it { subject.new(lower_accept).preferred.should == foo } + it { subject.new(html).preferred.should == html } + it { subject.new(json).preferred.should == json } + it { subject.new(option).preferred.should == foo } + it { subject.new(simple_accept).preferred.should == html } + it { subject.new(quality_accept).preferred.should == html } + it { subject.new(random_accept).preferred.should == html } + it { subject.new(lower_accept).preferred.should == foo } + it { subject.new(nil).preferred.should == '*/*' } + it { subject.new('').preferred.should == '*/*' } + it { subject.new('text / html').preferred.should == html } end # context 'with mapping' do |