fakeweb 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,17 +7,17 @@ class TestFakeWebOpenURI < Test::Unit::TestCase
7
7
  end
8
8
 
9
9
  def test_content_for_registered_uri
10
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
11
- assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
10
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
11
+ assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
12
12
  end
13
13
 
14
14
  def test_mock_open
15
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
15
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
16
16
  assert_equal 'test example content', open('http://mock/test_example.txt').read
17
17
  end
18
18
 
19
19
  def test_mock_open_with_string_as_registered_uri
20
- FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
20
+ FakeWeb.register_uri(:get, 'http://mock/test_string.txt', :body => 'foo')
21
21
  assert_equal 'foo', open('http://mock/test_string.txt').string
22
22
  end
23
23
 
@@ -31,21 +31,21 @@ class TestFakeWebOpenURI < Test::Unit::TestCase
31
31
  end
32
32
 
33
33
  def test_mock_open_that_raises_exception
34
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
34
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => StandardError)
35
35
  assert_raises(StandardError) do
36
36
  open('http://mock/raising_exception.txt')
37
37
  end
38
38
  end
39
39
 
40
40
  def test_mock_open_that_raises_an_http_error
41
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
41
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
42
42
  assert_raises(OpenURI::HTTPError) do
43
43
  open('http://mock/raising_exception.txt')
44
44
  end
45
45
  end
46
46
 
47
47
  def test_mock_open_that_raises_an_http_error_with_a_specific_status
48
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
48
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
49
49
  exception = assert_raises(OpenURI::HTTPError) do
50
50
  open('http://mock/raising_exception.txt')
51
51
  end
@@ -54,7 +54,7 @@ class TestFakeWebOpenURI < Test::Unit::TestCase
54
54
  end
55
55
 
56
56
  def test_mock_open_with_block
57
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
57
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
58
58
  open('http://mock/test_example.txt') do |f|
59
59
  assert 'test example content', f.readlines
60
60
  end
@@ -8,6 +8,14 @@ require 'mocha'
8
8
 
9
9
  module FakeWebTestHelper
10
10
 
11
+ def capture_stderr
12
+ $stderr = StringIO.new
13
+ yield
14
+ $stderr.rewind && $stderr.read
15
+ ensure
16
+ $stderr = STDERR
17
+ end
18
+
11
19
  # Sets several expectations (using Mocha) that a real HTTP request makes it
12
20
  # past FakeWeb to the socket layer. You can use this when you need to check
13
21
  # that a request isn't handled by FakeWeb.
@@ -7,35 +7,35 @@ class TestFakeWebQueryString < Test::Unit::TestCase
7
7
  end
8
8
 
9
9
  def test_register_uri_string_with_query_params
10
- FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
11
- assert FakeWeb.registered_uri?('http://example.com/?a=1&b=1')
10
+ FakeWeb.register_uri(:get, 'http://example.com/?a=1&b=1', :body => 'foo')
11
+ assert FakeWeb.registered_uri?(:get, 'http://example.com/?a=1&b=1')
12
12
 
13
- FakeWeb.register_uri(URI.parse("http://example.org/?a=1&b=1"), :string => "foo")
14
- assert FakeWeb.registered_uri?("http://example.org/?a=1&b=1")
13
+ FakeWeb.register_uri(:post, URI.parse("http://example.org/?a=1&b=1"), :body => "foo")
14
+ assert FakeWeb.registered_uri?(:post, "http://example.org/?a=1&b=1")
15
15
  end
16
16
 
17
17
  def test_register_uri_with_query_params_and_check_in_different_order
18
- FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
19
- assert FakeWeb.registered_uri?('http://example.com/?b=1&a=1')
18
+ FakeWeb.register_uri(:get, 'http://example.com/?a=1&b=1', :body => 'foo')
19
+ assert FakeWeb.registered_uri?(:get, 'http://example.com/?b=1&a=1')
20
20
 
21
- FakeWeb.register_uri(URI.parse('http://example.org/?a=1&b=1'), :string => 'foo')
22
- assert FakeWeb.registered_uri?('http://example.org/?b=1&a=1')
21
+ FakeWeb.register_uri(:post, URI.parse('http://example.org/?a=1&b=1'), :body => 'foo')
22
+ assert FakeWeb.registered_uri?(:post, 'http://example.org/?b=1&a=1')
23
23
  end
24
24
 
25
25
  def test_registered_uri_gets_recognized_with_empty_query_params
26
- FakeWeb.register_uri('http://example.com/', :string => 'foo')
27
- assert FakeWeb.registered_uri?('http://example.com/?')
26
+ FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
27
+ assert FakeWeb.registered_uri?(:get, 'http://example.com/?')
28
28
 
29
- FakeWeb.register_uri(URI.parse('http://example.org/'), :string => 'foo')
30
- assert FakeWeb.registered_uri?('http://example.org/?')
29
+ FakeWeb.register_uri(:post, URI.parse('http://example.org/'), :body => 'foo')
30
+ assert FakeWeb.registered_uri?(:post, 'http://example.org/?')
31
31
  end
32
32
 
33
33
  def test_register_uri_with_empty_query_params_and_check_with_none
34
- FakeWeb.register_uri('http://example.com/?', :string => 'foo')
35
- assert FakeWeb.registered_uri?('http://example.com/')
34
+ FakeWeb.register_uri(:get, 'http://example.com/?', :body => 'foo')
35
+ assert FakeWeb.registered_uri?(:get, 'http://example.com/')
36
36
 
37
- FakeWeb.register_uri(URI.parse('http://example.org/?'), :string => 'foo')
38
- assert FakeWeb.registered_uri?('http://example.org/')
37
+ FakeWeb.register_uri(:post, URI.parse('http://example.org/?'), :body => 'foo')
38
+ assert FakeWeb.registered_uri?(:post, 'http://example.org/')
39
39
  end
40
40
 
41
41
  def test_registry_sort_query_params
@@ -0,0 +1,96 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestRegexes < Test::Unit::TestCase
4
+
5
+ def setup
6
+ FakeWeb.clean_registry
7
+ @original_allow_net_connect = FakeWeb.allow_net_connect?
8
+ end
9
+
10
+ def teardown
11
+ FakeWeb.allow_net_connect = @old_allow_net_conncet
12
+ end
13
+
14
+ def test_registered_uri_with_pattern
15
+ FakeWeb.register_uri(:get, %r|http://example.com/test_example/\d+|, :body => "example")
16
+ assert FakeWeb.registered_uri?(:get, "http://example.com/test_example/25")
17
+ assert !FakeWeb.registered_uri?(:get, "http://example.com/test_example/abc")
18
+ end
19
+
20
+ def test_response_for_with_matching_registered_uri
21
+ FakeWeb.register_uri(:get, %r|http://www.google.com|, :body => "Welcome to Google!")
22
+ assert_equal "Welcome to Google!", FakeWeb.response_for(:get, "http://www.google.com").body
23
+ end
24
+
25
+ def test_registered_uri_with_authentication_and_pattern
26
+ FakeWeb.register_uri(:get, %r|http://user:pass@mock/example\.\w+|i, :body => "example")
27
+ assert FakeWeb.registered_uri?(:get, 'http://user:pass@mock/example.txt')
28
+ end
29
+
30
+ def test_registered_uri_with_authentication_and_pattern_handles_case_insensitivity
31
+ FakeWeb.register_uri(:get, %r|http://user:pass@mock/example\.\w+|i, :body => "example")
32
+ assert FakeWeb.registered_uri?(:get, 'http://uSeR:PAss@mock/example.txt')
33
+ end
34
+
35
+ def test_request_with_authentication_and_pattern_handles_case_insensitivity
36
+ FakeWeb.register_uri(:get, %r|http://user:pass@mock/example\.\w+|i, :body => "example")
37
+ http = Net::HTTP.new('mock', 80)
38
+ req = Net::HTTP::Get.new('/example.txt')
39
+ req.basic_auth 'uSeR', 'PAss'
40
+ assert_equal "example", http.request(req).body
41
+ end
42
+
43
+ def test_registering_with_overlapping_regexes_uses_first_registered
44
+ FakeWeb.register_uri(:get, %r|http://example\.com/|, :body => "first")
45
+ FakeWeb.register_uri(:get, %r|http://example\.com/a|, :body => "second")
46
+ response = Net::HTTP.start("example.com") { |query| query.get('/a') }
47
+ assert_equal "first", response.body
48
+ end
49
+
50
+ def test_registry_does_not_find_using_mismatched_protocols_or_ports_when_registered_with_both
51
+ FakeWeb.register_uri(:get, %r|http://www.example.com:80|, :body => "example")
52
+ assert !FakeWeb.registered_uri?(:get, "https://www.example.com:80")
53
+ assert !FakeWeb.registered_uri?(:get, "http://www.example.com:443")
54
+ end
55
+
56
+ def test_registry_only_finds_using_default_port_when_registered_without_if_protocol_matches
57
+ FakeWeb.register_uri(:get, %r|http://www.example.com/test|, :body => "example")
58
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:80/test")
59
+ assert !FakeWeb.registered_uri?(:get, "http://www.example.com:443/test")
60
+ assert !FakeWeb.registered_uri?(:get, "https://www.example.com:443/test")
61
+ FakeWeb.register_uri(:get, %r|https://www.example.org/test|, :body => "example")
62
+ assert FakeWeb.registered_uri?(:get, "https://www.example.org:443/test")
63
+ assert !FakeWeb.registered_uri?(:get, "https://www.example.org:80/test")
64
+ assert !FakeWeb.registered_uri?(:get, "http://www.example.org:80/test")
65
+ end
66
+
67
+ def test_registry_matches_using_mismatched_port_when_registered_without
68
+ FakeWeb.register_uri(:get, %r|http://www.example.com|, :body => "example")
69
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:80")
70
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:443")
71
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:12345")
72
+ assert !FakeWeb.registered_uri?(:get, "https://www.example.com:443")
73
+ assert !FakeWeb.registered_uri?(:get, "https://www.example.com")
74
+ end
75
+
76
+ def test_registry_matches_using_any_protocol_and_port_when_registered_without_protocol_or_port
77
+ FakeWeb.register_uri(:get, %r|www.example.com|, :body => "example")
78
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com")
79
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:80")
80
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:443")
81
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com")
82
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com:80")
83
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com:443")
84
+ end
85
+
86
+ def test_registry_matches_with_query_params
87
+ FakeWeb.register_uri(:get, %r[example.com/list\?(.*&|)important=1], :body => "example")
88
+ assert FakeWeb.registered_uri?(:get, "http://example.com/list?hash=123&important=1&unimportant=2")
89
+ assert FakeWeb.registered_uri?(:get, "http://example.com/list?hash=123&important=12&unimportant=2")
90
+ assert FakeWeb.registered_uri?(:get, "http://example.com/list?important=1&unimportant=2")
91
+ assert !FakeWeb.registered_uri?(:get, "http://example.com/list?important=2")
92
+ assert !FakeWeb.registered_uri?(:get, "http://example.com/list?important=2&unimportant=1")
93
+ assert !FakeWeb.registered_uri?(:get, "http://example.com/list?hash=123&important=2&unimportant=1")
94
+ assert !FakeWeb.registered_uri?(:get, "http://example.com/list?notimportant=1&unimportant=1")
95
+ end
96
+ end
@@ -0,0 +1,71 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestResponseHeaders < Test::Unit::TestCase
4
+
5
+ def setup
6
+ FakeWeb.clean_registry
7
+ end
8
+
9
+ def test_content_type_when_registering_with_string_and_content_type_header
10
+ FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]', :content_type => "application/json")
11
+ response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
12
+ assert_equal '[{"username": "chrisk"}]', response.body
13
+ assert_equal "application/json", response['Content-Type']
14
+ end
15
+
16
+ def test_content_type_when_registering_with_string_only
17
+ FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]')
18
+ response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
19
+ assert_equal '[{"username": "chrisk"}]', response.body
20
+ assert_nil response['Content-Type']
21
+ end
22
+
23
+ def test_cookies_when_registering_with_file_and_set_cookie_header
24
+ FakeWeb.register_uri(:get, "http://example.com/", :body => File.dirname(__FILE__) + '/fixtures/test_example.txt',
25
+ :set_cookie => "user_id=1; example=yes")
26
+ response = Net::HTTP.start("example.com") { |query| query.get("/") }
27
+ assert_equal "test example content", response.body
28
+ assert_equal "user_id=1; example=yes", response['Set-Cookie']
29
+ end
30
+
31
+ def test_registering_with_baked_response_ignores_header_options
32
+ fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
33
+ fake_response["Server"] = "Apache/1.3.27 (Unix)"
34
+ FakeWeb.register_uri(:get, "http://example.com/", :response => fake_response,
35
+ :server => "FakeWeb/1.2.3 (Ruby)")
36
+ response = Net::HTTP.start("example.com") { |query| query.get("/") }
37
+ assert_equal "200", response.code
38
+ assert_equal "OK", response.message
39
+ assert_equal "Apache/1.3.27 (Unix)", response["Server"]
40
+ end
41
+
42
+ def test_headers_are_rotated_when_registering_with_response_rotation
43
+ FakeWeb.register_uri(:get, "http://example.com",
44
+ [{:body => 'test1', :expires => "Thu, 14 Jun 2009 16:00:00 GMT",
45
+ :content_type => "text/plain"},
46
+ {:body => 'test2', :expires => "Thu, 14 Jun 2009 16:00:01 GMT"}])
47
+
48
+ Net::HTTP.start("example.com") do |query|
49
+ response = query.get("/")
50
+ assert_equal 'test1', response.body
51
+ assert_equal "Thu, 14 Jun 2009 16:00:00 GMT", response['Expires']
52
+ assert_equal "text/plain", response['Content-Type']
53
+
54
+ response = query.get("/")
55
+ assert_equal 'test2', response.body
56
+ assert_equal "Thu, 14 Jun 2009 16:00:01 GMT", response['Expires']
57
+ assert_nil response['Content-Type']
58
+ end
59
+ end
60
+
61
+ def test_registering_with_status_option_and_response_headers
62
+ FakeWeb.register_uri(:get, "http://example.com", :status => ["301", "Moved Permanently"],
63
+ :location => "http://www.example.com")
64
+
65
+ response = Net::HTTP.start("example.com") { |query| query.get("/") }
66
+ assert_equal "301", response.code
67
+ assert_equal "Moved Permanently", response.message
68
+ assert_equal "http://www.example.com", response["Location"]
69
+ end
70
+
71
+ end
@@ -12,48 +12,48 @@ class TestFakeWebTrailingSlashes < Test::Unit::TestCase
12
12
  end
13
13
 
14
14
  def test_registering_root_without_slash_and_ask_predicate_method_with_slash
15
- FakeWeb.register_uri(:get, "http://www.example.com", :string => "root")
15
+ FakeWeb.register_uri(:get, "http://www.example.com", :body => "root")
16
16
  assert FakeWeb.registered_uri?(:get, "http://www.example.com/")
17
17
  end
18
18
 
19
19
  def test_registering_root_without_slash_and_request
20
- FakeWeb.register_uri(:get, "http://www.example.com", :string => "root")
20
+ FakeWeb.register_uri(:get, "http://www.example.com", :body => "root")
21
21
  response = Net::HTTP.start("www.example.com") { |query| query.get('/') }
22
22
  assert_equal "root", response.body
23
23
  end
24
24
 
25
25
  def test_registering_root_with_slash_and_ask_predicate_method_without_slash
26
- FakeWeb.register_uri(:get, "http://www.example.com/", :string => "root")
26
+ FakeWeb.register_uri(:get, "http://www.example.com/", :body => "root")
27
27
  assert FakeWeb.registered_uri?(:get, "http://www.example.com")
28
28
  end
29
29
 
30
30
  def test_registering_root_with_slash_and_request
31
- FakeWeb.register_uri(:get, "http://www.example.com/", :string => "root")
31
+ FakeWeb.register_uri(:get, "http://www.example.com/", :body => "root")
32
32
  response = Net::HTTP.start("www.example.com") { |query| query.get('/') }
33
33
  assert_equal "root", response.body
34
34
  end
35
35
 
36
36
  def test_registering_path_without_slash_and_ask_predicate_method_with_slash
37
- FakeWeb.register_uri(:get, "http://www.example.com/users", :string => "User list")
37
+ FakeWeb.register_uri(:get, "http://www.example.com/users", :body => "User list")
38
38
  assert !FakeWeb.registered_uri?(:get, "http://www.example.com/users/")
39
39
  end
40
40
 
41
41
  def test_registering_path_without_slash_and_request_with_slash
42
42
  FakeWeb.allow_net_connect = false
43
- FakeWeb.register_uri(:get, "http://www.example.com/users", :string => "User list")
43
+ FakeWeb.register_uri(:get, "http://www.example.com/users", :body => "User list")
44
44
  assert_raise FakeWeb::NetConnectNotAllowedError do
45
45
  response = Net::HTTP.start("www.example.com") { |query| query.get('/users/') }
46
46
  end
47
47
  end
48
48
 
49
49
  def test_registering_path_with_slash_and_ask_predicate_method_without_slash
50
- FakeWeb.register_uri(:get, "http://www.example.com/users/", :string => "User list")
50
+ FakeWeb.register_uri(:get, "http://www.example.com/users/", :body => "User list")
51
51
  assert !FakeWeb.registered_uri?(:get, "http://www.example.com/users")
52
52
  end
53
53
 
54
54
  def test_registering_path_with_slash_and_request_without_slash
55
55
  FakeWeb.allow_net_connect = false
56
- FakeWeb.register_uri(:get, "http://www.example.com/users/", :string => "User list")
56
+ FakeWeb.register_uri(:get, "http://www.example.com/users/", :body => "User list")
57
57
  assert_raise FakeWeb::NetConnectNotAllowedError do
58
58
  response = Net::HTTP.start("www.example.com") { |query| query.get('/users') }
59
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakeweb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Kampmeier
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-05-31 00:00:00 -07:00
13
+ date: 2009-06-24 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -40,30 +40,36 @@ files:
40
40
  - LICENSE.txt
41
41
  - README.rdoc
42
42
  - Rakefile
43
+ - lib
44
+ - lib/fake_web
43
45
  - lib/fake_web.rb
46
+ - lib/fake_web/ext
44
47
  - lib/fake_web/ext/net_http.rb
45
48
  - lib/fake_web/registry.rb
46
49
  - lib/fake_web/responder.rb
47
50
  - lib/fake_web/response.rb
48
51
  - lib/fake_web/stub_socket.rb
49
52
  - lib/fakeweb.rb
53
+ - test
54
+ - test/fixtures
50
55
  - test/fixtures/google_response_from_curl
51
56
  - test/fixtures/google_response_with_transfer_encoding
52
57
  - test/fixtures/google_response_without_transfer_encoding
53
58
  - test/fixtures/test_example.txt
54
59
  - test/fixtures/test_txt_file
55
60
  - test/test_allow_net_connect.rb
61
+ - test/test_deprecations.rb
56
62
  - test/test_fake_authentication.rb
57
63
  - test/test_fake_web.rb
58
64
  - test/test_fake_web_open_uri.rb
59
65
  - test/test_helper.rb
60
66
  - test/test_missing_open_uri.rb
61
67
  - test/test_query_string.rb
68
+ - test/test_regexes.rb
69
+ - test/test_response_headers.rb
62
70
  - test/test_trailing_slashes.rb
63
71
  has_rdoc: true
64
72
  homepage: http://github.com/chrisk/fakeweb
65
- licenses: []
66
-
67
73
  post_install_message:
68
74
  rdoc_options:
69
75
  - --main
@@ -91,21 +97,25 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
97
  requirements: []
92
98
 
93
99
  rubyforge_project: fakeweb
94
- rubygems_version: 1.3.2
100
+ rubygems_version: 1.3.0
95
101
  signing_key:
96
- specification_version: 3
102
+ specification_version: 2
97
103
  summary: A tool for faking responses to HTTP requests
98
104
  test_files:
105
+ - test/fixtures
99
106
  - test/fixtures/google_response_from_curl
100
107
  - test/fixtures/google_response_with_transfer_encoding
101
108
  - test/fixtures/google_response_without_transfer_encoding
102
109
  - test/fixtures/test_example.txt
103
110
  - test/fixtures/test_txt_file
104
111
  - test/test_allow_net_connect.rb
112
+ - test/test_deprecations.rb
105
113
  - test/test_fake_authentication.rb
106
114
  - test/test_fake_web.rb
107
115
  - test/test_fake_web_open_uri.rb
108
116
  - test/test_helper.rb
109
117
  - test/test_missing_open_uri.rb
110
118
  - test/test_query_string.rb
119
+ - test/test_regexes.rb
120
+ - test/test_response_headers.rb
111
121
  - test/test_trailing_slashes.rb