chrisk-fakeweb 1.1.2.5 → 1.1.2.6

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 CHANGED
@@ -1,5 +1,15 @@
1
1
  fakeweb (development)
2
2
 
3
+ * start work on Ruby 1.9 compatibility [Chris Kampmeier]
4
+
5
+ * add FakeWeb.allow_net_connect= to enable/disable the pass-through to
6
+ Net::HTTP for unregistered URIs [Mislav Marohnić, Chris Kampmeier]
7
+
8
+ * remove setup.rb, since most people use RubyGems [Mislav Marohnić]
9
+
10
+ * fix that 'http://example.com/?' (empty query) matches a registered
11
+ 'http://example.com/', and vice-versa [Mislav Marohnić]
12
+
3
13
  * improve the test suite to not rely on an internet connection [Chris Kampmeier]
4
14
 
5
15
  * use `rake test` instead of `rake tests` [Josh Nichols]
data/README.rdoc CHANGED
@@ -1,11 +1,24 @@
1
1
  = FakeWeb
2
2
 
3
- FakeWeb is a helper for faking web requests. It works at a global level, without
4
- modifying code or writing extensive stubs.
3
+ FakeWeb is a helper for faking web requests in Ruby. It works at a global
4
+ level, without modifying code or writing extensive stubs.
5
+
6
+ = Installation
7
+
8
+ This fork of Blaine Cook's original code has lots of fixes, stability
9
+ improvements, and a few new features. To get it, install the latest gem
10
+ directly from GitHub (currently 1.1.2.6):
11
+
12
+ sudo gem install chrisk-fakeweb --source http://gems.github.com
5
13
 
6
14
  = Examples
7
15
 
8
- == Using a string response
16
+ Start by requiring FakeWeb:
17
+
18
+ require 'rubygems'
19
+ require 'fake_web'
20
+
21
+ == Registering basic string responses
9
22
 
10
23
  FakeWeb.register_uri("http://example.com/test1", :string => "Hello World!")
11
24
 
@@ -53,53 +66,62 @@ option for that response.)
53
66
  req.delete('/posts/1').body # => "Post not found"
54
67
  end
55
68
 
56
- == Requesting with OpenURI
57
-
58
- FakeWeb.register_uri('http://example.com/', :string => "Hello, World!")
59
-
60
- open('http://example.com/').string
61
- => "Hello, World!"
62
-
63
69
  == Clearing registered URIs
64
70
 
65
71
  The FakeWeb registry is a singleton that lasts for the duration of your
66
- program, maintaining every fake responses you register. If needed, you
72
+ program, maintaining every fake response you register. If needed, you
67
73
  can clean out the registry and remove all registered URIs:
68
74
 
69
75
  FakeWeb.clean_registry
70
76
 
77
+ == Blocking all real requests
71
78
 
72
- = Description
79
+ When you're using FakeWeb to replace _all_ of your requests, it's useful to
80
+ catch when requests are made for unregistered URIs (unlike the default
81
+ behavior, which is to pass those requests through to Net::HTTP as usual).
73
82
 
74
- FakeWeb is a helper for faking web requests. This makes testing easier,
75
- because you can decouple your test environment from live services without
76
- modifying code. It allows for a range of request behaviour, from simple
77
- stubbing of HTTP responses to re-playing complete recorded responses.
83
+ FakeWeb.allow_net_connect = false
84
+ Net::HTTP.get(URI.parse('http://example.com/'))
85
+ => raises FakeWeb::NetConnectNotAllowedError
78
86
 
79
- In addition to the conceptual advantage of having idempotent request behaviour,
80
- FakeWeb makes tests run faster than if they were made to remote (or even local)
81
- web servers. It also makes it possible to run tests without a network
82
- connection or in situations where the server is behind a firewall or has
83
- host based access controls.
87
+ FakeWeb.allow_net_connect = true
88
+ Net::HTTP.get(URI.parse('http://example.com/'))
89
+ => FakeWeb is bypassed and the response from a real request is returned
90
+
91
+ This is handy when you want to make sure your tests are self-contained, or you
92
+ want to catch the scenario when a URI is changed in implementation code
93
+ without a corresponding test change.
94
+
95
+ = More info
96
+
97
+ FakeWeb lets you decouple your test environment from live services without
98
+ modifying code or writing extensive stubs.
99
+
100
+ In addition to the conceptual advantage of having idempotent request
101
+ behaviour, FakeWeb makes tests run faster than if they were made to remote (or
102
+ even local) web servers. It also makes it possible to run tests without a
103
+ network connection or in situations where the server is behind a firewall or
104
+ has host-based access controls.
105
+
106
+ FakeWeb works with anything based on Net::HTTP--both higher-level wrappers,
107
+ like OpenURI, as well as a ton of libraries for popular web services.
84
108
 
85
- FakeWeb is tested with
86
- Net::HTTP[http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html] and
87
- OpenURI[http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/]. It should work
88
- with any web client library that uses Net::HTTP for its underlying requests
89
- (e.g., Flickr.rb[http://redgreenblu.com/flickr/],
90
- Ruby/Amazon[http://www.caliban.org/ruby/ruby-amazon.shtml],
91
- soap4r[http://dev.ctor.org/soap4r/], etc.)
92
109
 
93
110
  = Known Issues
94
111
 
95
- * Request bodies are ignored, including PUT and POST parameters. If you
96
- need different responses for different request bodies, you need to request
97
- different URLs, and register different responses for each.
112
+ * Requests are only stubbed at the URI level, with no respect to HTTP method.
113
+
114
+ * Similarly, request bodies are ignored, including PUT and POST parameters. If
115
+ you need different responses for different request bodies, you need to
116
+ request different URLs, and register different responses for each. (Query
117
+ strings are fully supported, though.)
118
+
98
119
 
99
120
  = Copyright
100
121
 
101
- FakeWeb - Ruby Helper for Faking Web Requests
102
- Copyright 2006 Blaine Cook <romeda@gmail.com>.
122
+ Copyright 2006-2007 Blaine Cook
123
+
124
+ Copyright 2008 various contributors
103
125
 
104
126
  FakeWeb is free software; you can redistribute it and/or modify
105
127
  it under the terms of the GNU General Public License as published by
data/Rakefile CHANGED
@@ -33,7 +33,9 @@ Rake::RDocTask.new do |rdoc|
33
33
  rdoc.main = "README.rdoc"
34
34
  rdoc.rdoc_dir = "doc"
35
35
  rdoc.rdoc_files.include("README.rdoc", "COPYING", "CHANGELOG", "lib/*.rb")
36
- rdoc.title = "FakeWeb"
36
+ rdoc.title = "FakeWeb API Documentation"
37
+ rdoc.options << '--line-numbers' << '--inline-source'
38
+ rdoc.options << '--charset' << 'utf-8'
37
39
  end
38
40
 
39
41
  Rcov::RcovTask.new do |t|
data/lib/fake_net_http.rb CHANGED
@@ -1,16 +1,16 @@
1
1
  # FakeWeb - Ruby Helper for Faking Web Requests
2
2
  # Copyright 2006 Blaine Cook <romeda@gmail.com>.
3
- #
3
+ #
4
4
  # FakeWeb is free software; you can redistribute it and/or modify
5
5
  # it under the terms of the GNU General Public License as published by
6
6
  # the Free Software Foundation; either version 2 of the License, or
7
7
  # (at your option) any later version.
8
- #
8
+ #
9
9
  # FakeWeb is distributed in the hope that it will be useful,
10
10
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
11
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
12
  # GNU General Public License for more details.
13
- #
13
+ #
14
14
  # You should have received a copy of the GNU General Public License
15
15
  # along with FakeWeb; if not, write to the Free Software
16
16
  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
@@ -19,55 +19,55 @@ require 'net/http'
19
19
  require 'net/https'
20
20
  require 'stringio'
21
21
 
22
- module Net #:nodoc:
22
+ module Net #:nodoc: all
23
23
 
24
- class BufferedIO #:nodoc:
25
- def initialize( io, debug_output = nil )
24
+ class BufferedIO
25
+ def initialize(io, debug_output = nil)
26
26
  @read_timeout = 60
27
27
  @rbuf = ''
28
28
  @debug_output = debug_output
29
+
29
30
  @io = case io
30
- when Socket, OpenSSL::SSL::SSLSocket, IO: io
31
+ when Socket, OpenSSL::SSL::SSLSocket, IO
32
+ io
31
33
  when String
32
- File.exists?(io) ? File.open(io, "r") : StringIO.new(io)
34
+ File.exists?(io) ? File.open(io, "r") : StringIO.new(io)
33
35
  end
34
36
  raise "Unable to create local socket" unless @io
35
37
  end
36
-
37
38
  end
38
-
39
- class HTTP #:nodoc:
40
39
 
41
- def HTTP.socket_type #:nodoc:
40
+
41
+ class HTTP
42
+ def self.socket_type
42
43
  FakeWeb::SocketDelegator
43
44
  end
44
45
 
45
46
  alias :original_net_http_request :request
46
47
  alias :original_net_http_connect :connect
47
-
48
- def request(req, body = nil, &block)
49
- prot = use_ssl ? "https" : "http"
50
48
 
51
- path = req.path
52
- path = URI.parse(req.path).request_uri if req.path =~ /^http/
49
+ def request(request, body = nil, &block)
50
+ protocol = use_ssl ? "https" : "http"
53
51
 
54
- uri = "#{prot}://#{self.address}:#{self.port}#{path}"
52
+ path = request.path
53
+ path = URI.parse(request.path).request_uri if request.path =~ /^http/
54
+
55
+ uri = "#{protocol}://#{self.address}:#{self.port}#{path}"
55
56
 
56
57
  if FakeWeb.registered_uri?(uri)
57
58
  @socket = Net::HTTP.socket_type.new
58
- return FakeWeb.response_for(uri, &block)
59
+ FakeWeb.response_for(uri, &block)
60
+ elsif FakeWeb.allow_net_connect?
61
+ original_net_http_connect
62
+ original_net_http_request(request, body, &block)
59
63
  else
60
- if FakeWeb.allow_net_connect?
61
- original_net_http_connect
62
- return original_net_http_request(req, body, &block)
63
- else
64
- expected = FakeWeb::Registry.instance.uri_map.keys
65
- raise "unexpected HTTP #{req.method} to #{uri}\n-- expected one of #{expected.inspect}"
66
- end
64
+ raise FakeWeb::NetConnectNotAllowedError,
65
+ "Real HTTP connections are disabled. Unregistered URI: #{uri}"
67
66
  end
68
67
  end
69
68
 
70
69
  def connect
71
70
  end
72
71
  end
72
+
73
73
  end
data/lib/fake_web.rb CHANGED
@@ -34,13 +34,14 @@ module FakeWeb
34
34
  # registered URIs.
35
35
  #
36
36
  # If you set <tt>FakeWeb.allow_net_connect = false</tt> and subsequently try
37
- # to make a request to a URI you haven't registered with #register_uri, an
38
- # exception will be raised. This is handy when you want to make sure your
39
- # tests are self-contained, or want to catch the scenario when a URI is
40
- # changed in implementation code without a corresponding test change.
37
+ # to make a request to a URI you haven't registered with #register_uri, a
38
+ # NetConnectNotAllowedError will be raised. This is handy when you want to
39
+ # make sure your tests are self-contained, or want to catch the scenario
40
+ # when a URI is changed in implementation code without a corresponding test
41
+ # change.
41
42
  #
42
43
  # When <tt>FakeWeb.allow_net_connect = true</tt> (the default), requests to
43
- # URIs not registered with FakeWeb are passed through to Net::HTTP.
44
+ # URIs not stubbed with FakeWeb are passed through to Net::HTTP.
44
45
  def self.allow_net_connect=(allowed)
45
46
  @allow_net_connect = allowed
46
47
  end
@@ -55,6 +56,12 @@ module FakeWeb
55
56
  @allow_net_connect
56
57
  end
57
58
 
59
+ # This exception is raised if you set <tt>FakeWeb.allow_net_connect =
60
+ # false</tt> and subsequently try to make a request to a URI you haven't
61
+ # stubbed.
62
+ class NetConnectNotAllowedError < StandardError; end;
63
+
64
+
58
65
  # Register +uri+ to be handled according to +options+. +uri+ can be a
59
66
  # +String+ or an +URI+ object. +options+ must be either a +Hash+ or
60
67
  # an +Array+ of +Hashes+ (see below) that must contain any one of the
@@ -170,7 +177,7 @@ module FakeWeb
170
177
 
171
178
  def normalize_uri(uri)
172
179
  case uri
173
- when URI: uri
180
+ when URI then uri
174
181
  else
175
182
  uri = 'http://' + uri unless uri.match('^https?://')
176
183
  parsed_uri = URI.parse(uri)
@@ -241,7 +248,7 @@ module FakeWeb
241
248
 
242
249
  def baked_response
243
250
  resp = case options[:response]
244
- when Net::HTTPResponse: options[:response]
251
+ when Net::HTTPResponse then options[:response]
245
252
  when String
246
253
  socket = Net::BufferedIO.new(options[:response])
247
254
  r = Net::HTTPResponse.read_new(socket)
@@ -260,7 +267,8 @@ module FakeWeb
260
267
  return unless options.has_key?(:exception)
261
268
  ex_alloc = options[:exception].allocate
262
269
  ex_instance = case ex_alloc
263
- when Net::HTTPError, OpenURI::HTTPError: options[:exception].new('Exception from FakeWeb', response)
270
+ when Net::HTTPError, OpenURI::HTTPError
271
+ options[:exception].new('Exception from FakeWeb', response)
264
272
  else options[:exception].new
265
273
  end
266
274
  raise ex_instance
@@ -2,39 +2,36 @@ require File.join(File.dirname(__FILE__), "test_helper")
2
2
 
3
3
  class TestFakeWebAllowNetConnect < Test::Unit::TestCase
4
4
 
5
+ def setup
6
+ @original_allow_net_connect = FakeWeb.allow_net_connect?
7
+ end
8
+
9
+ def teardown
10
+ FakeWeb.allow_net_connect = @original_allow_net_connect
11
+ end
12
+
13
+
5
14
  def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_true
6
- original_value = FakeWeb.allow_net_connect?
7
15
  FakeWeb.allow_net_connect = true
8
-
9
16
  setup_expectations_for_real_apple_hot_news_request
10
17
  Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
11
-
12
- FakeWeb.allow_net_connect = original_value
13
18
  end
14
19
 
15
20
  def test_raises_for_unregistered_requests_when_allow_net_connect_is_false
16
- original_value = FakeWeb.allow_net_connect?
17
21
  FakeWeb.allow_net_connect = false
18
-
19
- assert_raise RuntimeError do
22
+ exception = assert_raise FakeWeb::NetConnectNotAllowedError do
20
23
  Net::HTTP.get(URI.parse('http://example.com/'))
21
24
  end
22
-
23
- FakeWeb.allow_net_connect = original_value
24
25
  end
25
26
 
26
27
  def test_question_mark_method_returns_true_after_setting_allow_net_connect_to_true
27
- original_value = FakeWeb.allow_net_connect?
28
28
  FakeWeb.allow_net_connect = true
29
29
  assert FakeWeb.allow_net_connect?
30
- FakeWeb.allow_net_connect = original_value
31
30
  end
32
31
 
33
32
  def test_question_mark_method_returns_false_after_setting_allow_net_connect_to_false
34
- original_value = FakeWeb.allow_net_connect?
35
33
  FakeWeb.allow_net_connect = false
36
34
  assert !FakeWeb.allow_net_connect?
37
- FakeWeb.allow_net_connect = original_value
38
35
  end
39
36
 
40
37
  def test_allow_net_connect_is_true_by_default
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chrisk-fakeweb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2.5
4
+ version: 1.1.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blaine Cook
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-25 00:00:00 -08:00
12
+ date: 2008-12-31 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -66,7 +66,7 @@ rubyforge_project:
66
66
  rubygems_version: 1.2.0
67
67
  signing_key:
68
68
  specification_version: 2
69
- summary: A test helper that makes it simple to test HTTP interaction
69
+ summary: A tool for faking responses to HTTP requests
70
70
  test_files:
71
71
  - test/fixtures
72
72
  - test/fixtures/test_example.txt