File: webmock_spec.rb

package info (click to toggle)
ruby-webmock 3.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: ruby: 12,829; makefile: 6
file content (114 lines) | stat: -rw-r--r-- 3,813 bytes parent folder | download | duplicates (2)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'spec_helper'

describe "WebMock" do

  describe ".version" do
    it "should report version" do
      expect(WebMock.version).to eq(WebMock::VERSION)
    end

    it "should not require safe_yaml" do
      expect(defined?SafeYAML).to eq(nil)
    end

    it "should alias enable_net_connect! to allow_net_connect!" do
      expect(WebMock.method(:enable_net_connect!)).to eq(WebMock.method(:allow_net_connect!))
    end

    it "should alias disallow_net_connect! to disable_net_connect!" do
      expect(WebMock.method(:disallow_net_connect!)).to eq(WebMock.method(:disable_net_connect!))
    end
  end

  describe ".net_connect_allowed?" do
    context 'enabled globally' do
      before do
        WebMock.enable_net_connect!
      end

      context 'without arguments' do
        it 'returns WebMock::Config.instance.allow_net_connect' do
          expect(WebMock.net_connect_allowed?).to eql(true)
        end
      end
    end

    context 'disabled with allowed remote string' do
      before do
        WebMock.disable_net_connect!(allow: "http://192.168.64.2:20031")
      end

      context 'without arguments' do
        it 'returns WebMock::Config.instance.allow_net_connect' do
          expect(WebMock.net_connect_allowed?).to eql(false)
        end
      end
    end

    context 'disabled globally' do
      before do
        WebMock.disable_net_connect!
      end

      context 'without arguments' do
        it 'returns WebMock::Config.instance.allow_net_connect' do
          expect(WebMock.net_connect_allowed?).to eql(false)
        end
      end
    end
  end

  describe ".net_http_connect_on_start?" do
    let(:uri) { Addressable::URI.parse("http://example.org:5432") }

    it "will not connect on start when false" do
      WebMock.disable_net_connect!
      expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
    end

    it "will connect on start when true" do
      WebMock.disable_net_connect!(net_http_connect_on_start: true)
      expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
    end

    it "will connect on start when regexp matches" do
      WebMock.disable_net_connect!(net_http_connect_on_start: /example/)
      expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
    end

    it "will not connect on start when regexp does not match" do
      WebMock.disable_net_connect!(net_http_connect_on_start: /nope/)
      expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
    end

    it "will connect on start when host matches" do
      WebMock.disable_net_connect!(net_http_connect_on_start: "example.org")
      expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
    end

    it "will not connect on start when host does not match" do
      WebMock.disable_net_connect!(net_http_connect_on_start: "localhost")
      expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
    end

    it "will connect on start when host + port matches" do
      WebMock.disable_net_connect!(net_http_connect_on_start: "example.org:5432")
      expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
    end

    it "will not connect on start when host + port does not match" do
      WebMock.disable_net_connect!(net_http_connect_on_start: "example.org:80")
      expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
    end

    it "will connect on start when scheme + host + port matches" do
      WebMock.disable_net_connect!(net_http_connect_on_start: "http://example.org:5432")
      expect(WebMock.net_http_connect_on_start?(uri)).to be(true)
    end

    it "will not connect on start when scheme + host + port does not match" do
      WebMock.disable_net_connect!(net_http_connect_on_start: "https://example.org:5432")
      expect(WebMock.net_http_connect_on_start?(uri)).to be(false)
    end
  end
end