File: test_allow_net_connect.rb

package info (click to toggle)
ruby-fakeweb 1.3.0%2Bgit20170806%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 436 kB
  • sloc: ruby: 2,057; sh: 24; makefile: 3
file content (168 lines) | stat: -rw-r--r-- 6,804 bytes parent folder | download | duplicates (3)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require 'test_helper'

class TestFakeWebAllowNetConnect < Test::Unit::TestCase
  def _test_unregistered_requests_are_passed_through_when_allow_net_connect_is_true
    FakeWeb.allow_net_connect = true
    setup_expectations_for_real_apple_hot_news_request
    Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
  end

  def test_raises_for_unregistered_requests_when_allow_net_connect_is_false
    FakeWeb.allow_net_connect = false
    assert_raise FakeWeb::NetConnectNotAllowedError do
      Net::HTTP.get(URI.parse("http://example.com/"))
    end
  end

  def _test_unregistered_requests_are_passed_through_when_allow_net_connect_is_the_same_string
    FakeWeb.allow_net_connect = "http://images.apple.com/main/rss/hotnews/hotnews.rss"
    setup_expectations_for_real_apple_hot_news_request
    Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
  end

  def _test_unregistered_requests_are_passed_through_when_allow_net_connect_is_the_same_string_with_default_port
    FakeWeb.allow_net_connect = "http://images.apple.com:80/main/rss/hotnews/hotnews.rss"
    setup_expectations_for_real_apple_hot_news_request
    Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
  end

  def _test_unregistered_requests_are_passed_through_when_allow_net_connect_is_the_same_uri
    FakeWeb.allow_net_connect = URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss")
    setup_expectations_for_real_apple_hot_news_request
    Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
  end

  def _test_unregistered_requests_are_passed_through_when_allow_net_connect_is_a_matching_regexp
    FakeWeb.allow_net_connect = %r[^http://images\.apple\.com]
    setup_expectations_for_real_apple_hot_news_request
    Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
  end

  def test_raises_for_unregistered_requests_when_allow_net_connect_is_a_different_string
    FakeWeb.allow_net_connect = "http://example.com"
    assert_raise FakeWeb::NetConnectNotAllowedError do
      Net::HTTP.get(URI.parse("http://example.com/path"))
    end
  end

  def test_raises_for_unregistered_requests_when_allow_net_connect_is_a_different_uri
    FakeWeb.allow_net_connect = URI.parse("http://example.com")
    assert_raise FakeWeb::NetConnectNotAllowedError do
      Net::HTTP.get(URI.parse("http://example.com/path"))
    end
  end

  def test_raises_for_unregistered_requests_when_allow_net_connect_is_a_non_matching_regexp
    FakeWeb.allow_net_connect = %r[example\.net]
    assert_raise FakeWeb::NetConnectNotAllowedError do
      Net::HTTP.get(URI.parse("http://example.com"))
    end
  end

  def test_changing_allow_net_connect_from_string_to_false_corretly_removes_whitelist
    FakeWeb.allow_net_connect = "http://example.com"
    FakeWeb.allow_net_connect = false
    assert_raise FakeWeb::NetConnectNotAllowedError do
      Net::HTTP.get(URI.parse("http://example.com"))
    end
  end

  def test_changing_allow_net_connect_from_true_to_string_corretly_limits_connections
    FakeWeb.allow_net_connect = true
    FakeWeb.allow_net_connect = "http://example.com"
    assert_raise FakeWeb::NetConnectNotAllowedError do
      Net::HTTP.get(URI.parse("http://example.net"))
    end
  end

  def test_exception_message_includes_unregistered_request_method_and_uri_but_no_default_port
    FakeWeb.allow_net_connect = false
    exception = assert_raise FakeWeb::NetConnectNotAllowedError do
      Net::HTTP.get(URI.parse("http://example.com/"))
    end
    assert exception.message.include?("GET http://example.com/")

    exception = assert_raise FakeWeb::NetConnectNotAllowedError do
      http = Net::HTTP.new("example.com", 443)
      http.use_ssl = true
      http.get("/")
    end
    assert exception.message.include?("GET https://example.com/")
  end

  def test_exception_message_includes_unregistered_request_port_when_not_default
    FakeWeb.allow_net_connect = false
    exception = assert_raise FakeWeb::NetConnectNotAllowedError do
      Net::HTTP.start("example.com", 8000) { |http| http.get("/") }
    end
    assert exception.message.include?("GET http://example.com:8000/")

    exception = assert_raise FakeWeb::NetConnectNotAllowedError do
      http = Net::HTTP.new("example.com", 4433)
      http.use_ssl = true
      http.get("/")
    end
    assert exception.message.include?("GET https://example.com:4433/")
  end

  def test_exception_message_includes_unregistered_request_port_when_not_default_with_path
    FakeWeb.allow_net_connect = false
    exception = assert_raise FakeWeb::NetConnectNotAllowedError do
      Net::HTTP.start("example.com", 8000) { |http| http.get("/test") }
    end
    assert exception.message.include?("GET http://example.com:8000/test")

    exception = assert_raise FakeWeb::NetConnectNotAllowedError do
      http = Net::HTTP.new("example.com", 4433)
      http.use_ssl = true
      http.get("/test")
    end
    assert exception.message.include?("GET https://example.com:4433/test")
  end

  def test_question_mark_method_returns_true_after_setting_allow_net_connect_to_true
    FakeWeb.allow_net_connect = true
    assert FakeWeb.allow_net_connect?
  end

  def test_question_mark_method_returns_false_after_setting_allow_net_connect_to_false
    FakeWeb.allow_net_connect = false
    assert !FakeWeb.allow_net_connect?
  end

  def test_question_mark_method_raises_with_no_argument_when_allow_net_connect_is_a_whitelist
    FakeWeb.allow_net_connect = "http://example.com"
    exception = assert_raise ArgumentError do
      FakeWeb.allow_net_connect?
    end
    assert_equal "You must supply a URI to test", exception.message
  end

  def test_question_mark_method_returns_true_when_argument_is_same_uri_as_allow_net_connect_string
    FakeWeb.allow_net_connect = "http://example.com"
    assert FakeWeb.allow_net_connect?("http://example.com/")
  end

  def test_question_mark_method_returns_true_when_argument_matches_allow_net_connect_regexp
    FakeWeb.allow_net_connect = %r[^https?://example.com/]
    assert FakeWeb.allow_net_connect?("http://example.com/path")
    assert FakeWeb.allow_net_connect?("https://example.com:443/")
  end

  def test_question_mark_method_returns_false_when_argument_does_not_match_allow_net_connect_regexp
    FakeWeb.allow_net_connect = %r[^http://example.com/]
    assert !FakeWeb.allow_net_connect?("http://example.com:8080")
  end
end


class TestFakeWebAllowNetConnectWithCleanState < Test::Unit::TestCase
  # Our test_helper.rb sets allow_net_connect = false in an inherited #setup
  # method. Disable that here to test the default setting.
  def setup; end
  def teardown; end

  def test_allow_net_connect_is_true_by_default
    assert FakeWeb.allow_net_connect?
  end
end