aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/request/accept/preferred_spec.rb
blob: 5d1179913e3a8cba34a74f3870020f281bb513bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'spec_helper'

describe Zero::Request::Accept, '#preferred' do
  subject { Zero::Request::Accept }
  let(:html) { 'text/html' }
  let(:json) { 'application/json' }
  let(:foo)  { 'text/foo' }
  let(:lower_quality) { foo + ';q=0.5' }
  let(:default) { '*/*;q=0.1' }
  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
    before :each do
      # reset the mapping to nothing
      Zero::Request::Accept.map = {}
    end

    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 }
  end

  context 'with mapping' do
    before :all do
      Zero::Request::Accept.map = {'text/html' => 'html'}
    end

    it { subject.new(html).preferred.should == 'html' }
  end
end