File: http_client_spec.rb

package info (click to toggle)
ruby-slack-notifier 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: ruby: 1,076; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 1,998 bytes parent folder | download
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
# frozen_string_literal: true

RSpec.describe Slack::Notifier::Util::HTTPClient do
  describe "::post" do
    it "initializes Util::HTTPClient with the given uri and params then calls" do
      http_post_double = instance_double("Slack::Notifier::Util::HTTPClient")

      expect(described_class)
        .to receive(:new).with("uri", "params")
                         .and_return(http_post_double)
      expect(http_post_double).to receive(:call)

      described_class.post "uri", "params"
    end

    # http_post is really tested in the integration spec,
    # where the internals are run through
  end

  describe "#initialize" do
    it "allows setting of options for Net::HTTP" do
      net_http_double = instance_double("Net::HTTP")
      http_client     = described_class.new URI.parse("http://example.com"),
                                            http_options: { open_timeout: 5 }

      allow(Net::HTTP).to receive(:new).and_return(net_http_double)
      allow(net_http_double).to receive(:use_ssl=)
      allow(net_http_double).to receive(:request).with(anything) do
        Net::HTTPOK.new("GET", "200", "OK")
      end

      expect(net_http_double).to receive(:open_timeout=).with(5)

      http_client.call
    end
  end

  describe "#call" do
    it "raises an error when the response is unsuccessful" do
      net_http_double = instance_double("Net::HTTP")
      http_client = described_class.new URI.parse("http://example.com"), {}
      bad_request = Net::HTTPBadRequest.new("GET", "400", "Bad Request")

      allow(bad_request).to receive(:body).and_return("something_bad")
      allow(Net::HTTP).to receive(:new).and_return(net_http_double)
      allow(net_http_double).to receive(:use_ssl=)
      allow(net_http_double).to receive(:request).with(anything) do
        bad_request
      end

      expect { http_client.call }.to raise_error(Slack::Notifier::APIError,
                                                 /something_bad \(HTTP Code 400\)/)
    end
  end
end