ivanvr-fakeweb 1.2.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +163 -0
- data/LICENSE.txt +281 -0
- data/README.rdoc +206 -0
- data/Rakefile +76 -0
- data/lib/fake_web/ext/net_http.rb +72 -0
- data/lib/fake_web/registry.rb +159 -0
- data/lib/fake_web/responder.rb +120 -0
- data/lib/fake_web/response.rb +10 -0
- data/lib/fake_web/stub_socket.rb +15 -0
- data/lib/fake_web/utility.rb +44 -0
- data/lib/fake_web.rb +173 -0
- data/lib/fakeweb.rb +2 -0
- data/test/fixtures/google_response_from_curl +12 -0
- data/test/fixtures/google_response_with_transfer_encoding +17 -0
- data/test/fixtures/google_response_without_transfer_encoding +11 -0
- data/test/fixtures/test_example.txt +1 -0
- data/test/fixtures/test_txt_file +3 -0
- data/test/test_allow_net_connect.rb +85 -0
- data/test/test_deprecations.rb +54 -0
- data/test/test_fake_authentication.rb +92 -0
- data/test/test_fake_web.rb +526 -0
- data/test/test_fake_web_open_uri.rb +58 -0
- data/test/test_helper.rb +74 -0
- data/test/test_missing_open_uri.rb +25 -0
- data/test/test_precedence.rb +79 -0
- data/test/test_query_string.rb +45 -0
- data/test/test_regexes.rb +152 -0
- data/test/test_response_headers.rb +73 -0
- data/test/test_trailing_slashes.rb +53 -0
- data/test/test_utility.rb +91 -0
- metadata +127 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
|
+
|
|
3
|
+
class TestMissingOpenURI < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
super
|
|
7
|
+
@saved_open_uri = OpenURI
|
|
8
|
+
Object.send(:remove_const, :OpenURI)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def teardown
|
|
12
|
+
super
|
|
13
|
+
Object.const_set(:OpenURI, @saved_open_uri)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_register_using_exception_without_open_uri
|
|
18
|
+
# regression test for Responder needing OpenURI::HTTPError to be defined
|
|
19
|
+
FakeWeb.register_uri(:get, "http://example.com/", :exception => StandardError)
|
|
20
|
+
assert_raises(StandardError) do
|
|
21
|
+
Net::HTTP.start("example.com") { |http| http.get("/") }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
|
+
|
|
3
|
+
class TestPrecedence < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def test_matching_get_strings_have_precedence_over_matching_get_regexes
|
|
6
|
+
FakeWeb.register_uri(:get, "http://example.com/test", :body => "string")
|
|
7
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/test|, :body => "regex")
|
|
8
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
|
9
|
+
assert_equal "string", response.body
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_matching_any_strings_have_precedence_over_matching_any_regexes
|
|
13
|
+
FakeWeb.register_uri(:any, "http://example.com/test", :body => "string")
|
|
14
|
+
FakeWeb.register_uri(:any, %r|http://example\.com/test|, :body => "regex")
|
|
15
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
|
16
|
+
assert_equal "string", response.body
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_matching_get_strings_have_precedence_over_matching_any_strings
|
|
20
|
+
FakeWeb.register_uri(:get, "http://example.com/test", :body => "get method")
|
|
21
|
+
FakeWeb.register_uri(:any, "http://example.com/test", :body => "any method")
|
|
22
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
|
23
|
+
assert_equal "get method", response.body
|
|
24
|
+
|
|
25
|
+
# registration order should not matter
|
|
26
|
+
FakeWeb.register_uri(:any, "http://example.com/test2", :body => "any method")
|
|
27
|
+
FakeWeb.register_uri(:get, "http://example.com/test2", :body => "get method")
|
|
28
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test2') }
|
|
29
|
+
assert_equal "get method", response.body
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_matching_any_strings_have_precedence_over_matching_get_regexes
|
|
33
|
+
FakeWeb.register_uri(:any, "http://example.com/test", :body => "any string")
|
|
34
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/test|, :body => "get regex")
|
|
35
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
|
36
|
+
assert_equal "any string", response.body
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_registered_strings_and_uris_are_equivalent_so_second_takes_precedence
|
|
40
|
+
FakeWeb.register_uri(:get, "http://example.com/test", :body => "string")
|
|
41
|
+
FakeWeb.register_uri(:get, URI.parse("http://example.com/test"), :body => "uri")
|
|
42
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
|
43
|
+
assert_equal "uri", response.body
|
|
44
|
+
|
|
45
|
+
FakeWeb.register_uri(:get, URI.parse("http://example.com/test2"), :body => "uri")
|
|
46
|
+
FakeWeb.register_uri(:get, "http://example.com/test2", :body => "string")
|
|
47
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test2') }
|
|
48
|
+
assert_equal "string", response.body
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_identical_registration_replaces_previous_registration
|
|
52
|
+
FakeWeb.register_uri(:get, "http://example.com/test", :body => "first")
|
|
53
|
+
FakeWeb.register_uri(:get, "http://example.com/test", :body => "second")
|
|
54
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
|
55
|
+
assert_equal "second", response.body
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_identical_registration_replaces_previous_registration_accounting_for_normalization
|
|
59
|
+
FakeWeb.register_uri(:get, "http://example.com/test?", :body => "first")
|
|
60
|
+
FakeWeb.register_uri(:get, "http://example.com:80/test", :body => "second")
|
|
61
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
|
62
|
+
assert_equal "second", response.body
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def test_identical_registration_replaces_previous_registration_accounting_for_query_params
|
|
66
|
+
FakeWeb.register_uri(:get, "http://example.com/test?a=1&b=2", :body => "first")
|
|
67
|
+
FakeWeb.register_uri(:get, "http://example.com/test?b=2&a=1", :body => "second")
|
|
68
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test?a=1&b=2') }
|
|
69
|
+
assert_equal "second", response.body
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def test_identical_registration_replaces_previous_registration_with_regexes
|
|
73
|
+
FakeWeb.register_uri(:get, /test/, :body => "first")
|
|
74
|
+
FakeWeb.register_uri(:get, /test/, :body => "second")
|
|
75
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
|
76
|
+
assert_equal "second", response.body
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
|
+
|
|
3
|
+
class TestFakeWebQueryString < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def test_register_uri_string_with_query_params
|
|
6
|
+
FakeWeb.register_uri(:get, 'http://example.com/?a=1&b=1', :body => 'foo')
|
|
7
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com/?a=1&b=1')
|
|
8
|
+
|
|
9
|
+
FakeWeb.register_uri(:post, URI.parse("http://example.org/?a=1&b=1"), :body => "foo")
|
|
10
|
+
assert FakeWeb.registered_uri?(:post, "http://example.org/?a=1&b=1")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_register_uri_with_query_params_and_check_in_different_order
|
|
14
|
+
FakeWeb.register_uri(:get, 'http://example.com/?a=1&b=1', :body => 'foo')
|
|
15
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com/?b=1&a=1')
|
|
16
|
+
|
|
17
|
+
FakeWeb.register_uri(:post, URI.parse('http://example.org/?a=1&b=1'), :body => 'foo')
|
|
18
|
+
assert FakeWeb.registered_uri?(:post, 'http://example.org/?b=1&a=1')
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_registered_uri_gets_recognized_with_empty_query_params
|
|
22
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
|
|
23
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com/?')
|
|
24
|
+
|
|
25
|
+
FakeWeb.register_uri(:post, URI.parse('http://example.org/'), :body => 'foo')
|
|
26
|
+
assert FakeWeb.registered_uri?(:post, 'http://example.org/?')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_register_uri_with_empty_query_params_and_check_with_none
|
|
30
|
+
FakeWeb.register_uri(:get, 'http://example.com/?', :body => 'foo')
|
|
31
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com/')
|
|
32
|
+
|
|
33
|
+
FakeWeb.register_uri(:post, URI.parse('http://example.org/?'), :body => 'foo')
|
|
34
|
+
assert FakeWeb.registered_uri?(:post, 'http://example.org/')
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_registry_sort_query_params
|
|
38
|
+
assert_equal "a=1&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "b=2&a=1")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_registry_sort_query_params_sorts_by_value_if_keys_collide
|
|
42
|
+
assert_equal "a=1&a=2&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "a=2&b=2&a=1")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
|
+
|
|
3
|
+
class TestRegexes < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def test_registered_uri_with_pattern
|
|
6
|
+
FakeWeb.register_uri(:get, %r|http://example.com/test_example/\d+|, :body => "example")
|
|
7
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/test_example/25")
|
|
8
|
+
assert !FakeWeb.registered_uri?(:get, "http://example.com/test_example/abc")
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def test_response_for_with_matching_registered_uri
|
|
12
|
+
FakeWeb.register_uri(:get, %r|http://www.google.com|, :body => "Welcome to Google!")
|
|
13
|
+
assert_equal "Welcome to Google!", FakeWeb.response_for(:get, "http://www.google.com").body
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_response_for_with_matching_registered_uri_and_get_method_matching_to_any_method
|
|
17
|
+
FakeWeb.register_uri(:any, %r|http://www.example.com|, :body => "example")
|
|
18
|
+
assert_equal "example", FakeWeb.response_for(:get, "http://www.example.com").body
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_registered_uri_with_authentication_and_pattern
|
|
22
|
+
FakeWeb.register_uri(:get, %r|http://user:pass@mock/example\.\w+|i, :body => "example")
|
|
23
|
+
assert FakeWeb.registered_uri?(:get, 'http://user:pass@mock/example.txt')
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_registered_uri_with_authentication_and_pattern_handles_case_insensitivity
|
|
27
|
+
FakeWeb.register_uri(:get, %r|http://user:pass@mock/example\.\w+|i, :body => "example")
|
|
28
|
+
assert FakeWeb.registered_uri?(:get, 'http://uSeR:PAss@mock/example.txt')
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_request_with_authentication_and_pattern_handles_case_insensitivity
|
|
32
|
+
FakeWeb.register_uri(:get, %r|http://user:pass@mock/example\.\w+|i, :body => "example")
|
|
33
|
+
http = Net::HTTP.new('mock', 80)
|
|
34
|
+
req = Net::HTTP::Get.new('/example.txt')
|
|
35
|
+
req.basic_auth 'uSeR', 'PAss'
|
|
36
|
+
assert_equal "example", http.request(req).body
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_requesting_a_uri_that_matches_two_registered_regexes_raises_an_error
|
|
40
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/|, :body => "first")
|
|
41
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/a|, :body => "second")
|
|
42
|
+
assert_raise FakeWeb::MultipleMatchingURIsError do
|
|
43
|
+
Net::HTTP.start("example.com") { |query| query.get('/a') }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_requesting_a_uri_that_matches_two_registered_regexes_with_differently_ordered_query_params_raises_an_error
|
|
48
|
+
FakeWeb.register_uri(:get, %r[example.com/list\?b=2&a=1], :body => "first")
|
|
49
|
+
FakeWeb.register_uri(:get, %r[example.com/list\?a=1&b=2], :body => "second")
|
|
50
|
+
assert_raise FakeWeb::MultipleMatchingURIsError do
|
|
51
|
+
Net::HTTP.start("example.com") { |query| query.get('/list?a=1&b=2') }
|
|
52
|
+
end
|
|
53
|
+
assert_raise FakeWeb::MultipleMatchingURIsError do
|
|
54
|
+
Net::HTTP.start("example.com") { |query| query.get('/list?b=2&a=1') }
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_requesting_a_uri_that_matches_two_registered_regexes_raises_an_error_including_request_info
|
|
59
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/|, :body => "first")
|
|
60
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/a|, :body => "second")
|
|
61
|
+
begin
|
|
62
|
+
Net::HTTP.start("example.com") { |query| query.get('/a') }
|
|
63
|
+
rescue FakeWeb::MultipleMatchingURIsError => exception
|
|
64
|
+
end
|
|
65
|
+
assert exception.message.include?("GET http://example.com/a")
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def test_registry_does_not_find_using_mismatched_protocols_or_ports_when_registered_with_both
|
|
69
|
+
FakeWeb.register_uri(:get, %r|http://www.example.com:80|, :body => "example")
|
|
70
|
+
assert !FakeWeb.registered_uri?(:get, "https://www.example.com:80")
|
|
71
|
+
assert !FakeWeb.registered_uri?(:get, "http://www.example.com:443")
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def test_registry_finds_using_non_default_port
|
|
75
|
+
FakeWeb.register_uri(:get, %r|example\.com:8080|, :body => "example")
|
|
76
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:8080/path")
|
|
77
|
+
assert FakeWeb.registered_uri?(:get, "https://www.example.com:8080/path")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_registry_finds_using_default_port_and_http_when_registered_with_explicit_port_80
|
|
81
|
+
FakeWeb.register_uri(:get, %r|example\.com:80|, :body => "example")
|
|
82
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com/path")
|
|
83
|
+
|
|
84
|
+
# check other permutations, too
|
|
85
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:80/path")
|
|
86
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:8080/path")
|
|
87
|
+
assert FakeWeb.registered_uri?(:get, "https://www.example.com:80/path")
|
|
88
|
+
assert FakeWeb.registered_uri?(:get, "https://www.example.com:8080/path")
|
|
89
|
+
assert !FakeWeb.registered_uri?(:get, "https://www.example.com/path")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def test_registry_finds_using_default_port_and_https_when_registered_with_explicit_port_443
|
|
93
|
+
FakeWeb.register_uri(:get, %r|example\.com:443|, :body => "example")
|
|
94
|
+
assert FakeWeb.registered_uri?(:get, "https://www.example.com/path")
|
|
95
|
+
|
|
96
|
+
# check other permutations, too
|
|
97
|
+
assert FakeWeb.registered_uri?(:get, "https://www.example.com:443/path")
|
|
98
|
+
assert FakeWeb.registered_uri?(:get, "https://www.example.com:44321/path")
|
|
99
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:443/path")
|
|
100
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:44321/path")
|
|
101
|
+
assert !FakeWeb.registered_uri?(:get, "http://www.example.com/path")
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def test_registry_only_finds_using_default_port_when_registered_without_if_protocol_matches
|
|
105
|
+
FakeWeb.register_uri(:get, %r|http://www.example.com/test|, :body => "example")
|
|
106
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:80/test")
|
|
107
|
+
assert !FakeWeb.registered_uri?(:get, "http://www.example.com:443/test")
|
|
108
|
+
assert !FakeWeb.registered_uri?(:get, "https://www.example.com:443/test")
|
|
109
|
+
FakeWeb.register_uri(:get, %r|https://www.example.org/test|, :body => "example")
|
|
110
|
+
assert FakeWeb.registered_uri?(:get, "https://www.example.org:443/test")
|
|
111
|
+
assert !FakeWeb.registered_uri?(:get, "https://www.example.org:80/test")
|
|
112
|
+
assert !FakeWeb.registered_uri?(:get, "http://www.example.org:80/test")
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def test_registry_matches_using_mismatched_port_when_registered_without
|
|
116
|
+
FakeWeb.register_uri(:get, %r|http://www.example.com|, :body => "example")
|
|
117
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:80")
|
|
118
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:443")
|
|
119
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:12345")
|
|
120
|
+
assert !FakeWeb.registered_uri?(:get, "https://www.example.com:443")
|
|
121
|
+
assert !FakeWeb.registered_uri?(:get, "https://www.example.com")
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def test_registry_matches_using_default_port_for_protocol_when_registered_without_protocol_or_port
|
|
125
|
+
FakeWeb.register_uri(:get, %r|www.example.com/home|, :body => "example")
|
|
126
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com/home")
|
|
127
|
+
assert FakeWeb.registered_uri?(:get, "https://www.example.com/home")
|
|
128
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com:80/home")
|
|
129
|
+
assert FakeWeb.registered_uri?(:get, "https://www.example.com:443/home")
|
|
130
|
+
assert !FakeWeb.registered_uri?(:get, "https://www.example.com:80/home")
|
|
131
|
+
assert !FakeWeb.registered_uri?(:get, "http://www.example.com:443/home")
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def test_registry_matches_with_query_params
|
|
135
|
+
FakeWeb.register_uri(:get, %r[example.com/list\?(.*&|)important=1], :body => "example")
|
|
136
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/list?hash=123&important=1&unimportant=2")
|
|
137
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/list?hash=123&important=12&unimportant=2")
|
|
138
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/list?important=1&unimportant=2")
|
|
139
|
+
assert !FakeWeb.registered_uri?(:get, "http://example.com/list?important=2")
|
|
140
|
+
assert !FakeWeb.registered_uri?(:get, "http://example.com/list?important=2&unimportant=1")
|
|
141
|
+
assert !FakeWeb.registered_uri?(:get, "http://example.com/list?hash=123&important=2&unimportant=1")
|
|
142
|
+
assert !FakeWeb.registered_uri?(:get, "http://example.com/list?notimportant=1&unimportant=1")
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def test_registry_matches_with_unsorted_query_params
|
|
146
|
+
FakeWeb.register_uri(:get, %r[example\.com/list\?b=2&a=1], :body => "example")
|
|
147
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/list?b=2&a=1")
|
|
148
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/list?a=1&b=2")
|
|
149
|
+
assert FakeWeb.registered_uri?(:get, "https://example.com:443/list?b=2&a=1")
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
|
+
|
|
3
|
+
class TestResponseHeaders < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def test_content_type_when_registering_with_string_and_content_type_header_as_symbol_option
|
|
6
|
+
FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]', :content_type => "application/json")
|
|
7
|
+
response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
|
|
8
|
+
assert_equal '[{"username": "chrisk"}]', response.body
|
|
9
|
+
assert_equal "application/json", response['Content-Type']
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_content_type_when_registering_with_string_and_content_type_header_as_string_option
|
|
13
|
+
FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]', 'Content-Type' => "application/json")
|
|
14
|
+
response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
|
|
15
|
+
assert_equal "application/json", response['Content-Type']
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_content_type_when_registering_with_string_only
|
|
19
|
+
FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]')
|
|
20
|
+
response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
|
|
21
|
+
assert_equal '[{"username": "chrisk"}]', response.body
|
|
22
|
+
assert_nil response['Content-Type']
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_cookies_when_registering_with_file_and_set_cookie_header
|
|
26
|
+
FakeWeb.register_uri(:get, "http://example.com/", :body => File.dirname(__FILE__) + '/fixtures/test_example.txt',
|
|
27
|
+
:set_cookie => "user_id=1; example=yes")
|
|
28
|
+
response = Net::HTTP.start("example.com") { |query| query.get("/") }
|
|
29
|
+
assert_equal "test example content", response.body
|
|
30
|
+
assert_equal "user_id=1; example=yes", response['Set-Cookie']
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_registering_with_baked_response_ignores_header_options
|
|
34
|
+
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
|
35
|
+
fake_response["Server"] = "Apache/1.3.27 (Unix)"
|
|
36
|
+
FakeWeb.register_uri(:get, "http://example.com/", :response => fake_response,
|
|
37
|
+
:server => "FakeWeb/1.2.3 (Ruby)")
|
|
38
|
+
response = Net::HTTP.start("example.com") { |query| query.get("/") }
|
|
39
|
+
assert_equal "200", response.code
|
|
40
|
+
assert_equal "OK", response.message
|
|
41
|
+
assert_equal "Apache/1.3.27 (Unix)", response["Server"]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_headers_are_rotated_when_registering_with_response_rotation
|
|
45
|
+
FakeWeb.register_uri(:get, "http://example.com",
|
|
46
|
+
[{:body => 'test1', :expires => "Thu, 14 Jun 2009 16:00:00 GMT",
|
|
47
|
+
:content_type => "text/plain"},
|
|
48
|
+
{:body => 'test2', :expires => "Thu, 14 Jun 2009 16:00:01 GMT"}])
|
|
49
|
+
|
|
50
|
+
first_response = second_response = nil
|
|
51
|
+
Net::HTTP.start("example.com") do |query|
|
|
52
|
+
first_response = query.get("/")
|
|
53
|
+
second_response = query.get("/")
|
|
54
|
+
end
|
|
55
|
+
assert_equal 'test1', first_response.body
|
|
56
|
+
assert_equal "Thu, 14 Jun 2009 16:00:00 GMT", first_response['Expires']
|
|
57
|
+
assert_equal "text/plain", first_response['Content-Type']
|
|
58
|
+
assert_equal 'test2', second_response.body
|
|
59
|
+
assert_equal "Thu, 14 Jun 2009 16:00:01 GMT", second_response['Expires']
|
|
60
|
+
assert_nil second_response['Content-Type']
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_registering_with_status_option_and_response_headers
|
|
64
|
+
FakeWeb.register_uri(:get, "http://example.com", :status => ["301", "Moved Permanently"],
|
|
65
|
+
:location => "http://www.example.com")
|
|
66
|
+
|
|
67
|
+
response = Net::HTTP.start("example.com") { |query| query.get("/") }
|
|
68
|
+
assert_equal "301", response.code
|
|
69
|
+
assert_equal "Moved Permanently", response.message
|
|
70
|
+
assert_equal "http://www.example.com", response["Location"]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
|
+
|
|
3
|
+
class TestFakeWebTrailingSlashes < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def test_registering_root_without_slash_and_ask_predicate_method_with_slash
|
|
6
|
+
FakeWeb.register_uri(:get, "http://www.example.com", :body => "root")
|
|
7
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com/")
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def test_registering_root_without_slash_and_request
|
|
11
|
+
FakeWeb.register_uri(:get, "http://www.example.com", :body => "root")
|
|
12
|
+
response = Net::HTTP.start("www.example.com") { |query| query.get('/') }
|
|
13
|
+
assert_equal "root", response.body
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_registering_root_with_slash_and_ask_predicate_method_without_slash
|
|
17
|
+
FakeWeb.register_uri(:get, "http://www.example.com/", :body => "root")
|
|
18
|
+
assert FakeWeb.registered_uri?(:get, "http://www.example.com")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_registering_root_with_slash_and_request
|
|
22
|
+
FakeWeb.register_uri(:get, "http://www.example.com/", :body => "root")
|
|
23
|
+
response = Net::HTTP.start("www.example.com") { |query| query.get('/') }
|
|
24
|
+
assert_equal "root", response.body
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_registering_path_without_slash_and_ask_predicate_method_with_slash
|
|
28
|
+
FakeWeb.register_uri(:get, "http://www.example.com/users", :body => "User list")
|
|
29
|
+
assert !FakeWeb.registered_uri?(:get, "http://www.example.com/users/")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_registering_path_without_slash_and_request_with_slash
|
|
33
|
+
FakeWeb.allow_net_connect = false
|
|
34
|
+
FakeWeb.register_uri(:get, "http://www.example.com/users", :body => "User list")
|
|
35
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
|
36
|
+
response = Net::HTTP.start("www.example.com") { |query| query.get('/users/') }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_registering_path_with_slash_and_ask_predicate_method_without_slash
|
|
41
|
+
FakeWeb.register_uri(:get, "http://www.example.com/users/", :body => "User list")
|
|
42
|
+
assert !FakeWeb.registered_uri?(:get, "http://www.example.com/users")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_registering_path_with_slash_and_request_without_slash
|
|
46
|
+
FakeWeb.allow_net_connect = false
|
|
47
|
+
FakeWeb.register_uri(:get, "http://www.example.com/users/", :body => "User list")
|
|
48
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
|
49
|
+
response = Net::HTTP.start("www.example.com") { |query| query.get('/users') }
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
|
+
|
|
3
|
+
class TestUtility < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def test_decode_userinfo_from_header_handles_basic_auth
|
|
6
|
+
authorization_header = "Basic dXNlcm5hbWU6c2VjcmV0"
|
|
7
|
+
userinfo = FakeWeb::Utility.decode_userinfo_from_header(authorization_header)
|
|
8
|
+
assert_equal "username:secret", userinfo
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def test_encode_unsafe_chars_in_userinfo_does_not_encode_userinfo_safe_punctuation
|
|
12
|
+
userinfo = "user;&=+$,:secret"
|
|
13
|
+
assert_equal userinfo, FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_encode_unsafe_chars_in_userinfo_does_not_encode_rfc_3986_unreserved_characters
|
|
17
|
+
userinfo = "-_.!~*'()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:secret"
|
|
18
|
+
assert_equal userinfo, FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_encode_unsafe_chars_in_userinfo_does_encode_other_characters
|
|
22
|
+
userinfo, safe_userinfo = 'us#rn@me:sec//ret?"', 'us%23rn%40me:sec%2F%2Fret%3F%22'
|
|
23
|
+
assert_equal safe_userinfo, FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_strip_default_port_from_uri_strips_80_from_http_with_path
|
|
27
|
+
uri = "http://example.com:80/foo/bar"
|
|
28
|
+
stripped_uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
|
|
29
|
+
assert_equal "http://example.com/foo/bar", stripped_uri
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_strip_default_port_from_uri_strips_80_from_http_without_path
|
|
33
|
+
uri = "http://example.com:80"
|
|
34
|
+
stripped_uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
|
|
35
|
+
assert_equal "http://example.com", stripped_uri
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_strip_default_port_from_uri_strips_443_from_https_without_path
|
|
39
|
+
uri = "https://example.com:443"
|
|
40
|
+
stripped_uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
|
|
41
|
+
assert_equal "https://example.com", stripped_uri
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_strip_default_port_from_uri_strips_443_from_https
|
|
45
|
+
uri = "https://example.com:443/foo/bar"
|
|
46
|
+
stripped_uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
|
|
47
|
+
assert_equal "https://example.com/foo/bar", stripped_uri
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def test_strip_default_port_from_uri_does_not_strip_8080_from_http
|
|
51
|
+
uri = "http://example.com:8080/foo/bar"
|
|
52
|
+
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_strip_default_port_from_uri_does_not_strip_443_from_http
|
|
56
|
+
uri = "http://example.com:443/foo/bar"
|
|
57
|
+
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def test_strip_default_port_from_uri_does_not_strip_80_from_query_string
|
|
61
|
+
uri = "http://example.com/?a=:80&b=c"
|
|
62
|
+
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def test_strip_default_port_from_uri_does_not_modify_strings_that_do_not_start_with_http_or_https
|
|
66
|
+
uri = "httpz://example.com:80/"
|
|
67
|
+
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def test_simple_array_permutation_with_one_element
|
|
71
|
+
array, permutations = [1], [[1]]
|
|
72
|
+
FakeWeb::Utility.simple_array_permutation(array) do |permutation|
|
|
73
|
+
permutations.delete(permutation)
|
|
74
|
+
end
|
|
75
|
+
assert permutations.empty?
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def test_simple_array_permutation_with_three_elements
|
|
79
|
+
array = [1, 2, 3]
|
|
80
|
+
permutations = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
|
|
81
|
+
FakeWeb::Utility.simple_array_permutation(array) do |permutation|
|
|
82
|
+
permutations.delete(permutation)
|
|
83
|
+
end
|
|
84
|
+
assert permutations.empty?
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def test_simple_array_permutation_return_value
|
|
88
|
+
array = [1, 2, 3]
|
|
89
|
+
assert array, FakeWeb::Utility.simple_array_permutation(array) { }
|
|
90
|
+
end
|
|
91
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ivanvr-fakeweb
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.2.5.3
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Chris Kampmeier
|
|
8
|
+
- Blaine Cook
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2009-07-08 00:00:00 -07:00
|
|
14
|
+
default_executable:
|
|
15
|
+
dependencies:
|
|
16
|
+
- !ruby/object:Gem::Dependency
|
|
17
|
+
name: mocha
|
|
18
|
+
type: :development
|
|
19
|
+
version_requirement:
|
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
21
|
+
requirements:
|
|
22
|
+
- - ">="
|
|
23
|
+
- !ruby/object:Gem::Version
|
|
24
|
+
version: 0.9.5
|
|
25
|
+
version:
|
|
26
|
+
description: FakeWeb is a helper for faking web requests in Ruby. It works at a global level, without modifying code or writing extensive stubs.
|
|
27
|
+
email:
|
|
28
|
+
- chris@kampers.net
|
|
29
|
+
- romeda@gmail.com
|
|
30
|
+
executables: []
|
|
31
|
+
|
|
32
|
+
extensions: []
|
|
33
|
+
|
|
34
|
+
extra_rdoc_files:
|
|
35
|
+
- CHANGELOG
|
|
36
|
+
- LICENSE.txt
|
|
37
|
+
- README.rdoc
|
|
38
|
+
files:
|
|
39
|
+
- CHANGELOG
|
|
40
|
+
- LICENSE.txt
|
|
41
|
+
- README.rdoc
|
|
42
|
+
- Rakefile
|
|
43
|
+
- lib
|
|
44
|
+
- lib/fake_web
|
|
45
|
+
- lib/fake_web.rb
|
|
46
|
+
- lib/fake_web/ext
|
|
47
|
+
- lib/fake_web/ext/net_http.rb
|
|
48
|
+
- lib/fake_web/registry.rb
|
|
49
|
+
- lib/fake_web/responder.rb
|
|
50
|
+
- lib/fake_web/response.rb
|
|
51
|
+
- lib/fake_web/stub_socket.rb
|
|
52
|
+
- lib/fake_web/utility.rb
|
|
53
|
+
- lib/fakeweb.rb
|
|
54
|
+
- test
|
|
55
|
+
- test/fixtures
|
|
56
|
+
- test/fixtures/google_response_from_curl
|
|
57
|
+
- test/fixtures/google_response_with_transfer_encoding
|
|
58
|
+
- test/fixtures/google_response_without_transfer_encoding
|
|
59
|
+
- test/fixtures/test_example.txt
|
|
60
|
+
- test/fixtures/test_txt_file
|
|
61
|
+
- test/test_allow_net_connect.rb
|
|
62
|
+
- test/test_deprecations.rb
|
|
63
|
+
- test/test_fake_authentication.rb
|
|
64
|
+
- test/test_fake_web.rb
|
|
65
|
+
- test/test_fake_web_open_uri.rb
|
|
66
|
+
- test/test_helper.rb
|
|
67
|
+
- test/test_missing_open_uri.rb
|
|
68
|
+
- test/test_precedence.rb
|
|
69
|
+
- test/test_query_string.rb
|
|
70
|
+
- test/test_regexes.rb
|
|
71
|
+
- test/test_response_headers.rb
|
|
72
|
+
- test/test_trailing_slashes.rb
|
|
73
|
+
- test/test_utility.rb
|
|
74
|
+
has_rdoc: true
|
|
75
|
+
homepage: http://github.com/ivanvr/fakeweb
|
|
76
|
+
licenses:
|
|
77
|
+
post_install_message:
|
|
78
|
+
rdoc_options:
|
|
79
|
+
- --main
|
|
80
|
+
- README.rdoc
|
|
81
|
+
- --title
|
|
82
|
+
- FakeWeb API Documentation
|
|
83
|
+
- --charset
|
|
84
|
+
- utf-8
|
|
85
|
+
- --line-numbers
|
|
86
|
+
- --inline-source
|
|
87
|
+
require_paths:
|
|
88
|
+
- lib
|
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
|
+
requirements:
|
|
91
|
+
- - ">="
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: "0"
|
|
94
|
+
version:
|
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
|
+
requirements:
|
|
97
|
+
- - ">="
|
|
98
|
+
- !ruby/object:Gem::Version
|
|
99
|
+
version: "0"
|
|
100
|
+
version:
|
|
101
|
+
requirements: []
|
|
102
|
+
|
|
103
|
+
rubyforge_project:
|
|
104
|
+
rubygems_version: 1.3.5
|
|
105
|
+
signing_key:
|
|
106
|
+
specification_version: 2
|
|
107
|
+
summary: A tool for faking responses to HTTP requests
|
|
108
|
+
test_files:
|
|
109
|
+
- test/fixtures
|
|
110
|
+
- test/fixtures/google_response_from_curl
|
|
111
|
+
- test/fixtures/google_response_with_transfer_encoding
|
|
112
|
+
- test/fixtures/google_response_without_transfer_encoding
|
|
113
|
+
- test/fixtures/test_example.txt
|
|
114
|
+
- test/fixtures/test_txt_file
|
|
115
|
+
- test/test_allow_net_connect.rb
|
|
116
|
+
- test/test_deprecations.rb
|
|
117
|
+
- test/test_fake_authentication.rb
|
|
118
|
+
- test/test_fake_web.rb
|
|
119
|
+
- test/test_fake_web_open_uri.rb
|
|
120
|
+
- test/test_helper.rb
|
|
121
|
+
- test/test_missing_open_uri.rb
|
|
122
|
+
- test/test_precedence.rb
|
|
123
|
+
- test/test_query_string.rb
|
|
124
|
+
- test/test_regexes.rb
|
|
125
|
+
- test/test_response_headers.rb
|
|
126
|
+
- test/test_trailing_slashes.rb
|
|
127
|
+
- test/test_utility.rb
|