aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/cask/dsl/appcast_spec.rb
blob: b8903b1be8294cfd1e078bfbbf427c04846c21cc (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
require "cmd/cask"

describe Hbc::DSL::Appcast do
  subject { described_class.new(url, params) }

  let(:url) { "http://example.com" }
  let(:uri) { Hbc::UnderscoreSupportingURI.parse(url) }
  let(:params) { {} }

  describe "#to_s" do
    it "returns the parsed URI string" do
      expect(subject.to_s).to eq("http://example.com")
    end
  end

  describe "#to_yaml" do
    let(:yaml) { [uri, params].to_yaml }

    context "with empty parameters" do
      it "returns an YAML serialized array composed of the URI and parameters" do
        expect(subject.to_yaml).to eq(yaml)
      end
    end

    context "with checkpoint in parameters" do
      let(:params) { { checkpoint: "abc123" } }

      it "returns an YAML serialized array composed of the URI and parameters" do
        expect(subject.to_yaml).to eq(yaml)
      end
    end
  end

  describe "#calculate_checkpoint" do
    before do
      expect(Hbc::SystemCommand).to receive(:run).with(*cmd_args).and_return(cmd_result)
      allow(cmd_result).to receive(:success?).and_return(cmd_success)
      allow(cmd_result).to receive(:stdout).and_return(cmd_stdout)
    end

    context "when server returns a successful HTTP status" do
      let(:cmd_args) { ["/usr/bin/curl", args: ["--compressed", "--location", "--user-agent", Hbc::URL::FAKE_USER_AGENT, "--fail", uri], print_stderr: false] }
      let(:cmd_result) { double("Hbc::SystemCommand::Result") }
      let(:cmd_success) { true }
      let(:cmd_stdout) { "hello world" }

      it "generates the content digest hash and returns a hash with the command result and the digest hash for the checkpoint" do
        expected_digest = Digest::SHA2.hexdigest(cmd_stdout)
        expected_result = {
          checkpoint: expected_digest,
          command_result: cmd_result,
        }

        expect(subject.calculate_checkpoint).to eq(expected_result)
      end
    end

    context "when server returns a non-successful HTTP status" do
      let(:cmd_args) { ["/usr/bin/curl", args: ["--compressed", "--location", "--user-agent", Hbc::URL::FAKE_USER_AGENT, "--fail", uri], print_stderr: false] }
      let(:cmd_result) { double("Hbc::SystemCommand::Result") }
      let(:cmd_success) { false }
      let(:cmd_stdout) { "some error message from the server" }

      it "returns a hash with the command result and nil for the checkpoint" do
        expected_result = {
          checkpoint: nil,
          command_result: cmd_result,
        }

        expect(subject.calculate_checkpoint).to eq(expected_result)
      end
    end
  end
end