fakeweb 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,17 @@
1
+ fakeweb (1.2.1)
2
+
3
+ * fix that query parameters are handled correctly when registering with a URI
4
+ object [Anselmo Alves, Chris Kampmeier]
5
+
6
+ * fix an exception when registering with the :response option and a string
7
+ containing "\0" [Jonathan Baudanza, Chris Kampmeier]
8
+
9
+ * fix that trailing slashes were considered significant for requests to the root
10
+ of a domain [Chris Kampmeier]
11
+
12
+ * add support for HTTP basic authentication via userinfo strings in URIs
13
+ [Michael Bleigh]
14
+
1
15
  fakeweb (1.2.0)
2
16
 
3
17
  * add lib/fakeweb.rb so you can require "fakeweb" as well [Chris Kampmeier]
data/README.rdoc CHANGED
@@ -11,6 +11,10 @@ RubyForge mirror. Just install the gem:
11
11
 
12
12
  sudo gem install fakeweb
13
13
 
14
+ Note: the gem was previously available as +FakeWeb+ (capital letters), but now
15
+ all versions are simply registered as +fakeweb+. If you have any old +FakeWeb+
16
+ gems lying around, remove them: <tt>sudo gem uninstall FakeWeb</tt>
17
+
14
18
 
15
19
  == Help and discussion
16
20
 
@@ -19,6 +23,7 @@ RDocs for the current release are available at http://fakeweb.rubyforge.org.
19
23
  There's a mailing list for questions and discussion at
20
24
  http://groups.google.com/group/fakeweb-users.
21
25
 
26
+ The main source repository is http://github.com/chrisk/fakeweb.
22
27
 
23
28
  == Examples
24
29
 
@@ -86,6 +91,21 @@ option for that response.)
86
91
  req.delete("/posts/1").body # => "Post not found"
87
92
  end
88
93
 
94
+ === Using HTTP basic authentication
95
+
96
+ You can stub requests that use basic authentication with +userinfo+ strings in
97
+ the URIs:
98
+
99
+ FakeWeb.register_uri("http://example.com/secret", :string => "Unauthorized", :status => ["401", "Unauthorized"])
100
+ FakeWeb.register_uri("http://user:pass@example.com/secret", :string => "Authorized")
101
+
102
+ Net::HTTP.start("example.com") do |http|
103
+ req = Net::HTTP::Get.new("/secret")
104
+ http.request(req) # => "Unauthorized"
105
+ req.basic_auth("user", "pass")
106
+ http.request(req) # => "Authorized"
107
+ end
108
+
89
109
  === Clearing registered URIs
90
110
 
91
111
  The FakeWeb registry is a singleton that lasts for the duration of your
@@ -159,4 +179,4 @@ Copyright 2008-2009 various contributors
159
179
  with FakeWeb; if not, write to the Free Software Foundation, Inc., 51
160
180
  Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
161
181
 
162
- See <tt>LICENSE.txt</tt> for the full terms.
182
+ See <tt>LICENSE.txt</tt> for the full terms.
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
+ require 'rake/gempackagetask'
2
3
  require 'rake/testtask'
3
4
  require 'rake/rdoctask'
4
5
 
@@ -7,7 +8,7 @@ task :default => :test
7
8
  desc "Run All Tests"
8
9
  Rake::TestTask.new :test do |test|
9
10
  test.test_files = ["test/**/*.rb"]
10
- test.verbose = true
11
+ test.verbose = false
11
12
  end
12
13
 
13
14
  desc "Generate Documentation"
@@ -60,4 +61,10 @@ else
60
61
  t.rcov_opts << "--exclude gems"
61
62
  t.rcov_opts << "--no-validator-links"
62
63
  end
63
- end
64
+ end
65
+
66
+ spec = eval(File.read(File.join(File.dirname(__FILE__), "fakeweb.gemspec")))
67
+ Rake::GemPackageTask.new(spec) do |pkg|
68
+ pkg.need_tar_gz = true
69
+ pkg.need_zip = true
70
+ end
@@ -14,7 +14,11 @@ module Net #:nodoc: all
14
14
  when Socket, OpenSSL::SSL::SSLSocket, IO
15
15
  io
16
16
  when String
17
- File.exists?(io) ? File.open(io, "r") : StringIO.new(io)
17
+ if !io.include?("\0") && File.exists?(io)
18
+ File.open(io, "r")
19
+ else
20
+ StringIO.new(io)
21
+ end
18
22
  end
19
23
  raise "Unable to create local socket" unless @io
20
24
  end
@@ -34,7 +38,13 @@ module Net #:nodoc: all
34
38
  path = request.path
35
39
  path = URI.parse(request.path).request_uri if request.path =~ /^http/
36
40
 
37
- uri = "#{protocol}://#{self.address}:#{self.port}#{path}"
41
+ if request['authorization'].nil?
42
+ userinfo = ""
43
+ else
44
+ userinfo = request['authorization'].sub(/^Basic /, "").unpack("m").first + "@"
45
+ end
46
+
47
+ uri = "#{protocol}://#{userinfo}#{self.address}:#{self.port}#{path}"
38
48
  method = request.method.downcase.to_sym
39
49
 
40
50
  if FakeWeb.registered_uri?(method, uri)
@@ -53,4 +63,4 @@ module Net #:nodoc: all
53
63
  end
54
64
  end
55
65
 
56
- end
66
+ end
@@ -56,14 +56,15 @@ module FakeWeb
56
56
  private
57
57
 
58
58
  def normalize_uri(uri)
59
- case uri
60
- when URI then uri
61
- else
62
- uri = 'http://' + uri unless uri.match('^https?://')
63
- parsed_uri = URI.parse(uri)
64
- parsed_uri.query = sort_query_params(parsed_uri.query)
65
- parsed_uri
66
- end
59
+ normalized_uri =
60
+ case uri
61
+ when URI then uri
62
+ else
63
+ uri = 'http://' + uri unless uri.match('^https?://')
64
+ URI.parse(uri)
65
+ end
66
+ normalized_uri.query = sort_query_params(normalized_uri.query)
67
+ normalized_uri.normalize
67
68
  end
68
69
 
69
70
  def sort_query_params(query)
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestFakeAuthentication < Test::Unit::TestCase
4
+ def setup
5
+ FakeWeb.register_uri('http://user:pass@mock/auth.txt', :string => 'authorized')
6
+ FakeWeb.register_uri('http://user2:pass@mock/auth.txt', :string => 'wrong user')
7
+ FakeWeb.register_uri('http://mock/auth.txt', :string => 'unauthorized')
8
+ end
9
+
10
+ def test_register_uri_with_authentication
11
+ FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :string => "example")
12
+ assert FakeWeb.registered_uri?('http://user:pass@mock/test_example.txt')
13
+ end
14
+
15
+ def test_register_uri_with_authentication_doesnt_trigger_without
16
+ FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :string => "example")
17
+ assert !FakeWeb.registered_uri?('http://mock/test_example.txt')
18
+ end
19
+
20
+ def test_register_uri_with_authentication_doesnt_trigger_with_incorrect_credentials
21
+ FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :string => "example")
22
+ assert !FakeWeb.registered_uri?('http://user:wrong@mock/test_example.txt')
23
+ end
24
+
25
+ def test_unauthenticated_request
26
+ http = Net::HTTP.new('mock', 80)
27
+ req = Net::HTTP::Get.new('/auth.txt')
28
+ assert_equal http.request(req).body, 'unauthorized'
29
+ end
30
+
31
+ def test_authenticated_request
32
+ http = Net::HTTP.new('mock',80)
33
+ req = Net::HTTP::Get.new('/auth.txt')
34
+ req.basic_auth 'user', 'pass'
35
+ assert_equal http.request(req).body, 'authorized'
36
+ end
37
+
38
+ def test_incorrectly_authenticated_request
39
+ http = Net::HTTP.new('mock',80)
40
+ req = Net::HTTP::Get.new('/auth.txt')
41
+ req.basic_auth 'user2', 'pass'
42
+ assert_equal http.request(req).body, 'wrong user'
43
+ end
44
+ end
@@ -486,4 +486,15 @@ class TestFakeWeb < Test::Unit::TestCase
486
486
  def test_requiring_fakeweb_instead_of_fake_web
487
487
  require "fakeweb"
488
488
  end
489
+
490
+ def test_registering_using_response_with_string_containing_null_byte
491
+ # Regression test for File.exists? raising an ArgumentError ("string
492
+ # contains null byte") since :response first tries to find by filename.
493
+ # The string should be treated as a response body, instead, and an
494
+ # EOFError is raised when the byte is encountered.
495
+ FakeWeb.register_uri("http://example.com", :response => "test\0test")
496
+ assert_raise EOFError do
497
+ Net::HTTP.get(URI.parse("http://example.com"))
498
+ end
499
+ end
489
500
  end
@@ -6,24 +6,36 @@ class TestFakeWebQueryString < Test::Unit::TestCase
6
6
  FakeWeb.clean_registry
7
7
  end
8
8
 
9
- def test_register_uri_with_query_params
9
+ def test_register_uri_string_with_query_params
10
10
  FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
11
11
  assert FakeWeb.registered_uri?('http://example.com/?a=1&b=1')
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")
12
15
  end
13
16
 
14
17
  def test_register_uri_with_query_params_and_check_in_different_order
15
18
  FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
16
19
  assert FakeWeb.registered_uri?('http://example.com/?b=1&a=1')
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')
17
23
  end
18
24
 
19
25
  def test_registered_uri_gets_recognized_with_empty_query_params
20
26
  FakeWeb.register_uri('http://example.com/', :string => 'foo')
21
27
  assert FakeWeb.registered_uri?('http://example.com/?')
28
+
29
+ FakeWeb.register_uri(URI.parse('http://example.org/'), :string => 'foo')
30
+ assert FakeWeb.registered_uri?('http://example.org/?')
22
31
  end
23
32
 
24
33
  def test_register_uri_with_empty_query_params_and_check_with_none
25
34
  FakeWeb.register_uri('http://example.com/?', :string => 'foo')
26
35
  assert FakeWeb.registered_uri?('http://example.com/')
36
+
37
+ FakeWeb.register_uri(URI.parse('http://example.org/?'), :string => 'foo')
38
+ assert FakeWeb.registered_uri?('http://example.org/')
27
39
  end
28
40
 
29
41
  def test_registry_sort_query_params
@@ -0,0 +1,62 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestFakeWebTrailingSlashes < 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_registering_root_without_slash_and_ask_predicate_method_with_slash
15
+ FakeWeb.register_uri(:get, "http://www.example.com", :string => "root")
16
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com/")
17
+ end
18
+
19
+ def test_registering_root_without_slash_and_request
20
+ FakeWeb.register_uri(:get, "http://www.example.com", :string => "root")
21
+ response = Net::HTTP.start("www.example.com") { |query| query.get('/') }
22
+ assert_equal "root", response.body
23
+ end
24
+
25
+ def test_registering_root_with_slash_and_ask_predicate_method_without_slash
26
+ FakeWeb.register_uri(:get, "http://www.example.com/", :string => "root")
27
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com")
28
+ end
29
+
30
+ def test_registering_root_with_slash_and_request
31
+ FakeWeb.register_uri(:get, "http://www.example.com/", :string => "root")
32
+ response = Net::HTTP.start("www.example.com") { |query| query.get('/') }
33
+ assert_equal "root", response.body
34
+ end
35
+
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")
38
+ assert !FakeWeb.registered_uri?(:get, "http://www.example.com/users/")
39
+ end
40
+
41
+ def test_registering_path_without_slash_and_request_with_slash
42
+ FakeWeb.allow_net_connect = false
43
+ FakeWeb.register_uri(:get, "http://www.example.com/users", :string => "User list")
44
+ assert_raise FakeWeb::NetConnectNotAllowedError do
45
+ response = Net::HTTP.start("www.example.com") { |query| query.get('/users/') }
46
+ end
47
+ end
48
+
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")
51
+ assert !FakeWeb.registered_uri?(:get, "http://www.example.com/users")
52
+ end
53
+
54
+ def test_registering_path_with_slash_and_request_without_slash
55
+ FakeWeb.allow_net_connect = false
56
+ FakeWeb.register_uri(:get, "http://www.example.com/users/", :string => "User list")
57
+ assert_raise FakeWeb::NetConnectNotAllowedError do
58
+ response = Net::HTTP.start("www.example.com") { |query| query.get('/users') }
59
+ end
60
+ end
61
+
62
+ 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.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blaine Cook
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-03-07 00:00:00 -08:00
13
+ date: 2009-04-29 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -56,10 +56,12 @@ files:
56
56
  - test/fixtures/test_example.txt
57
57
  - test/fixtures/test_txt_file
58
58
  - test/test_allow_net_connect.rb
59
+ - test/test_fake_authentication.rb
59
60
  - test/test_fake_web.rb
60
61
  - test/test_fake_web_open_uri.rb
61
62
  - test/test_helper.rb
62
63
  - test/test_query_string.rb
64
+ - test/test_trailing_slashes.rb
63
65
  has_rdoc: true
64
66
  homepage: http://github.com/chrisk/fakeweb
65
67
  post_install_message:
@@ -89,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
91
  requirements: []
90
92
 
91
93
  rubyforge_project: fakeweb
92
- rubygems_version: 1.3.1
94
+ rubygems_version: 1.3.0
93
95
  signing_key:
94
96
  specification_version: 2
95
97
  summary: A tool for faking responses to HTTP requests
@@ -101,7 +103,9 @@ test_files:
101
103
  - test/fixtures/test_example.txt
102
104
  - test/fixtures/test_txt_file
103
105
  - test/test_allow_net_connect.rb
106
+ - test/test_fake_authentication.rb
104
107
  - test/test_fake_web.rb
105
108
  - test/test_fake_web_open_uri.rb
106
109
  - test/test_helper.rb
107
110
  - test/test_query_string.rb
111
+ - test/test_trailing_slashes.rb