summaryrefslogtreecommitdiff
path: root/spec/unit/network/format_handler_spec.rb
blob: 1716a7296ad6ca27b7ac25b2fdee67af7d8598f6 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /usr/bin/env ruby
require 'spec_helper'

require 'puppet/network/format_handler'

describe Puppet::Network::FormatHandler do
  before(:each) do
    @saved_formats = Puppet::Network::FormatHandler.instance_variable_get(:@formats).dup
    Puppet::Network::FormatHandler.instance_variable_set(:@formats, {})
  end

  after(:each) do
    Puppet::Network::FormatHandler.instance_variable_set(:@formats, @saved_formats)
  end

  describe "when creating formats" do
    it "should instance_eval any block provided when creating a format" do
      format = Puppet::Network::FormatHandler.create(:test_format) do
        def asdfghjkl; end
      end
      format.should respond_to(:asdfghjkl)
    end
  end

  describe "when retrieving formats" do
    let!(:format) { Puppet::Network::FormatHandler.create(:the_format, :extension => "foo", :mime => "foo/bar") }

    it "should be able to retrieve a format by name" do
      Puppet::Network::FormatHandler.format(:the_format).should equal(format)
    end

    it "should be able to retrieve a format by extension" do
      Puppet::Network::FormatHandler.format_by_extension("foo").should equal(format)
    end

    it "should return nil if asked to return a format by an unknown extension" do
      Puppet::Network::FormatHandler.format_by_extension("yayness").should be_nil
    end

    it "should be able to retrieve formats by name irrespective of case" do
      Puppet::Network::FormatHandler.format(:The_Format).should equal(format)
    end

    it "should be able to retrieve a format by mime type" do
      Puppet::Network::FormatHandler.mime("foo/bar").should equal(format)
    end

    it "should be able to retrieve a format by mime type irrespective of case" do
      Puppet::Network::FormatHandler.mime("Foo/Bar").should equal(format)
    end
  end

  describe "#most_suitable_format_for" do
    before :each do
      Puppet::Network::FormatHandler.create(:one, :extension => "foo", :mime => "text/one")
      Puppet::Network::FormatHandler.create(:two, :extension => "bar", :mime => "application/two")
    end

    let(:format_one) { Puppet::Network::FormatHandler.format(:one) }
    let(:format_two) { Puppet::Network::FormatHandler.format(:two) }

    def suitable_in_setup_formats(accepted)
      Puppet::Network::FormatHandler.most_suitable_format_for(accepted, [:one, :two])
    end

    it "finds the most preferred format when anything is acceptable" do
      Puppet::Network::FormatHandler.most_suitable_format_for(["*/*"], [:two, :one]).should == format_two
    end

    it "finds no format when none are acceptable" do
      suitable_in_setup_formats(["three"]).should be_nil
    end

    it "skips unsupported, but accepted, formats" do
      suitable_in_setup_formats(["three", "two"]).should == format_two
    end

    it "gives the first acceptable and suitable format" do
      suitable_in_setup_formats(["three", "one", "two"]).should == format_one
    end

    it "allows specifying acceptable formats by mime type" do
      suitable_in_setup_formats(["text/one"]).should == format_one
    end

    it "ignores quality specifiers" do
      suitable_in_setup_formats(["two;q=0.8", "text/one;q=0.9"]).should == format_two
    end

    it "allows specifying acceptable formats by canonical name" do
      suitable_in_setup_formats([:one]).should == format_one
    end
  end
end