summaryrefslogtreecommitdiffstats
path: root/spec/unit/reports/http_spec.rb
blob: acf0c045cabc2a90edec014cdb3b09908a2da1ef (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
#!/usr/bin/env ruby

require 'spec_helper'

require 'puppet/reports'

# FakeHTTP fakes the behavior of Net::HTTP#request and acts as a sensor for an
# otherwise difficult to trace method call.
#
class FakeHTTP
  REQUESTS = {}
  def self.request(req)
    REQUESTS[req.path] = req
  end
end

processor = Puppet::Reports.report(:http)

describe processor do
  before  { Net::HTTP.any_instance.stubs(:start).yields(FakeHTTP) }
  subject { Puppet::Transaction::Report.new("apply").extend(processor) }

  it { should respond_to(:process) }

  it "should use the reporturl setting's host and port" do
    uri = URI.parse(Puppet[:reporturl])
    Net::HTTP.expects(:new).with(uri.host, uri.port).returns(stub_everything('http'))
    subject.process
  end

  describe "request" do
    before { subject.process }

    describe "path" do
      it "should use the path specified by the 'reporturl' setting" do
        reports_request.path.should == URI.parse(Puppet[:reporturl]).path
      end
    end

    describe "body" do
      it "should be the report as YAML" do
        reports_request.body.should == subject.to_yaml
      end
    end

    describe "content type" do
      it "should be 'application/x-yaml'" do
        reports_request.content_type.should == "application/x-yaml"
      end
    end
  end

  private

  def reports_request; FakeHTTP::REQUESTS[URI.parse(Puppet[:reporturl]).path] end
end