summaryrefslogtreecommitdiffstats
path: root/spec/unit/network/http/rack_spec.rb
blob: 9e1ee3d1eb58ac35c8813d4f81dab9de3dc7c976 (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
95
96
97
98
99
100
#!/usr/bin/env rspec
require 'spec_helper'
require 'puppet/network/handler'
require 'puppet/network/http/rack' if Puppet.features.rack?

describe "Puppet::Network::HTTP::Rack", :if => Puppet.features.rack? do
  describe "while initializing" do

    it "should require a protocol specification" do
      Proc.new { Puppet::Network::HTTP::Rack.new({}) }.should raise_error(ArgumentError)
    end

    it "should not accept imaginary protocols" do
      Proc.new { Puppet::Network::HTTP::Rack.new({:protocols => [:foo]}) }.should raise_error(ArgumentError)
    end

    it "should accept the REST protocol" do
      Proc.new { Puppet::Network::HTTP::Rack.new({:protocols => [:rest]}) }.should_not raise_error(ArgumentError)
    end

    it "should create a RackREST instance" do
      Puppet::Network::HTTP::RackREST.expects(:new)
      Puppet::Network::HTTP::Rack.new({:protocols => [:rest]})
    end

    describe "with XMLRPC enabled" do

      it "should require XMLRPC handlers" do
        Proc.new { Puppet::Network::HTTP::Rack.new({:protocols => [:xmlrpc]}) }.should raise_error(ArgumentError)
      end

      it "should create a RackXMLRPC instance" do
        Puppet::Network::HTTP::RackXMLRPC.expects(:new)
        Puppet::Network::HTTP::Rack.new({:protocols => [:xmlrpc], :xmlrpc_handlers => [:Status]})
      end

    end

  end

  describe "when called" do

    before :all do
      @app = Puppet::Network::HTTP::Rack.new({:protocols => [:rest]})
      # let's use Rack::Lint to verify that we're OK with the rack specification
      @linted = Rack::Lint.new(@app)
    end

    before :each do
      @env = Rack::MockRequest.env_for('/')
    end

    it "should create a Request object" do
      request = Rack::Request.new(@env)
      Rack::Request.expects(:new).returns request
      @linted.call(@env)
    end

    it "should create a Response object" do
      Rack::Response.expects(:new).returns stub_everything
      @app.call(@env) # can't lint when Rack::Response is a stub
    end

    it "should let RackREST process the request" do
      Puppet::Network::HTTP::RackREST.any_instance.expects(:process).once
      @linted.call(@env)
    end

    it "should catch unhandled exceptions from RackREST" do
      Puppet::Network::HTTP::RackREST.any_instance.expects(:process).raises(ArgumentError, 'test error')
      Proc.new { @linted.call(@env) }.should_not raise_error
    end

    it "should finish() the Response" do
      Rack::Response.any_instance.expects(:finish).once
      @app.call(@env) # can't lint when finish is a stub
    end

  end

  describe "when serving XMLRPC" do

    before :all do
      @app = Puppet::Network::HTTP::Rack.new({:protocols => [:rest, :xmlrpc], :xmlrpc_handlers => [:Status]})
      @linted = Rack::Lint.new(@app)
    end

    before :each do
      @env = Rack::MockRequest.env_for('/RPC2', :method => 'POST')
    end

    it "should use RackXMLRPC to serve /RPC2 requests" do
      Puppet::Network::HTTP::RackXMLRPC.any_instance.expects(:process).once
      @linted.call(@env)
    end

  end

end