fakeweb 1.2.7 → 1.2.8
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG +9 -0
- data/README.rdoc +18 -17
- data/Rakefile +5 -10
- data/fakeweb.gemspec +4 -3
- data/lib/fake_web.rb +1 -1
- data/lib/fake_web/registry.rb +11 -11
- data/lib/fake_web/responder.rb +12 -9
- data/lib/fake_web/utility.rb +11 -1
- data/test/test_allow_net_connect.rb +1 -1
- data/test/test_deprecations.rb +2 -2
- data/test/test_fake_authentication.rb +1 -1
- data/test/test_fake_web.rb +31 -17
- data/test/test_fake_web_open_uri.rb +4 -4
- data/test/test_helper.rb +14 -3
- data/test/test_missing_open_uri.rb +1 -1
- data/test/test_missing_pathname.rb +37 -0
- data/test/test_other_net_http_libraries.rb +2 -3
- data/test/test_precedence.rb +1 -1
- data/test/test_query_string.rb +1 -1
- data/test/test_regexes.rb +1 -1
- data/test/test_response_headers.rb +2 -2
- data/test/test_trailing_slashes.rb +1 -1
- data/test/test_utility.rb +7 -1
- metadata +4 -3
- data/lib/fake_web/VERSION +0 -1
data/.gitignore
CHANGED
data/CHANGELOG
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
fakeweb (1.2.8)
|
2
|
+
|
3
|
+
* support Pathname objects where a filename is expected [Chris Kampmeier]
|
4
|
+
|
5
|
+
* fix compatibility with Ruby 1.9.2 [Chris Kampmeier]
|
6
|
+
|
7
|
+
* simplify storage of FakeWeb::VERSION [Josh Peek, Woody Peterson, Ben Woosley]
|
8
|
+
|
9
|
+
|
1
10
|
fakeweb (1.2.7)
|
2
11
|
|
3
12
|
* revert to sorting query params before matching requests against regexps,
|
data/README.rdoc
CHANGED
@@ -6,14 +6,11 @@ level, without modifying code or writing extensive stubs.
|
|
6
6
|
|
7
7
|
== Installation
|
8
8
|
|
9
|
-
|
10
|
-
RubyForge mirror. Just install the gem:
|
11
|
-
|
12
|
-
sudo gem install fakeweb
|
9
|
+
gem install fakeweb
|
13
10
|
|
14
11
|
Note: the gem was previously available as +FakeWeb+ (capital letters), but now
|
15
12
|
all versions are simply registered as +fakeweb+. If you have any old +FakeWeb+
|
16
|
-
gems lying around, remove them: <tt>
|
13
|
+
gems lying around, remove them: <tt>gem uninstall FakeWeb</tt>
|
17
14
|
|
18
15
|
|
19
16
|
== Help and discussion
|
@@ -29,7 +26,6 @@ The main source repository is http://github.com/chrisk/fakeweb.
|
|
29
26
|
|
30
27
|
Start by requiring FakeWeb:
|
31
28
|
|
32
|
-
require 'rubygems'
|
33
29
|
require 'fakeweb'
|
34
30
|
|
35
31
|
=== Registering basic string responses
|
@@ -45,6 +41,11 @@ Start by requiring FakeWeb:
|
|
45
41
|
You can also call <tt>register_uri</tt> with a regular expression, to match
|
46
42
|
more than one URI.
|
47
43
|
|
44
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/|, :body => "Hello World!")
|
45
|
+
|
46
|
+
Net::HTTP.get(URI.parse("http://example.com/test3"))
|
47
|
+
=> "Hello World!"
|
48
|
+
|
48
49
|
=== Replaying a recorded response
|
49
50
|
|
50
51
|
page = `curl -is http://www.google.com/`
|
@@ -71,17 +72,17 @@ more than one URI.
|
|
71
72
|
|
72
73
|
If you use the <tt>:any</tt> symbol, the URI you specify will be completely
|
73
74
|
stubbed out (regardless of the HTTP method of the request). This can be useful
|
74
|
-
for RPC-
|
75
|
+
for RPC-style services, where the HTTP method isn't significant. (Older
|
75
76
|
versions of FakeWeb always behaved like this, and didn't accept the first
|
76
77
|
+method+ argument above; this syntax is now deprecated.)
|
77
78
|
|
78
79
|
=== Rotating responses
|
79
80
|
|
80
|
-
You can optionally call FakeWeb.register_uri with an array of options
|
81
|
-
these are used, in order, to respond to repeated requests. Once you run
|
82
|
-
responses, further requests always receive the last response. (You can
|
83
|
-
a response more than once before rotating, by specifying a
|
84
|
-
option for that response.)
|
81
|
+
You can optionally call <tt>FakeWeb.register_uri</tt> with an array of options
|
82
|
+
hashes; these are used, in order, to respond to repeated requests. Once you run
|
83
|
+
out of responses, further requests always receive the last response. (You can
|
84
|
+
also send a response more than once before rotating, by specifying a
|
85
|
+
<tt>:times</tt> option for that response.)
|
85
86
|
|
86
87
|
FakeWeb.register_uri(:delete, "http://example.com/posts/1",
|
87
88
|
[{:body => "Post 1 deleted.", :status => ["200", "OK"]},
|
@@ -95,8 +96,8 @@ option for that response.)
|
|
95
96
|
|
96
97
|
=== Using HTTP basic authentication
|
97
98
|
|
98
|
-
You can
|
99
|
-
|
99
|
+
You can fake requests that use basic authentication by adding +userinfo+ strings
|
100
|
+
to your URIs:
|
100
101
|
|
101
102
|
FakeWeb.register_uri(:get, "http://example.com/secret", :body => "Unauthorized", :status => ["401", "Unauthorized"])
|
102
103
|
FakeWeb.register_uri(:get, "http://user:pass@example.com/secret", :body => "Authorized")
|
@@ -110,9 +111,9 @@ the URIs:
|
|
110
111
|
|
111
112
|
=== Clearing registered URIs
|
112
113
|
|
113
|
-
The FakeWeb registry is a singleton that lasts for the duration of your
|
114
|
-
|
115
|
-
|
114
|
+
The FakeWeb registry is a singleton that lasts for the duration of your program,
|
115
|
+
maintaining every fake response you register. If needed, you can clean out the
|
116
|
+
registry and remove all registered URIs:
|
116
117
|
|
117
118
|
FakeWeb.clean_registry
|
118
119
|
|
data/Rakefile
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
puts "Using ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}"
|
2
|
-
|
3
1
|
require 'rubygems'
|
4
2
|
require 'rake'
|
5
3
|
|
6
|
-
version =
|
4
|
+
version = '1.2.8'
|
7
5
|
|
8
6
|
begin
|
9
7
|
require 'jeweler'
|
@@ -18,19 +16,15 @@ begin
|
|
18
16
|
gem.homepage = "http://github.com/chrisk/fakeweb"
|
19
17
|
gem.add_development_dependency "mocha", ">= 0.9.5"
|
20
18
|
end
|
21
|
-
Jeweler::GemcutterTasks.new
|
22
|
-
Jeweler::RubyforgeTasks.new do |rubyforge|
|
23
|
-
rubyforge.doc_task = "rdoc"
|
24
|
-
rubyforge.remote_doc_path = ""
|
25
|
-
end
|
26
19
|
rescue LoadError
|
27
|
-
puts "Jeweler (or a dependency) not available. Install it with:
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
28
21
|
end
|
29
22
|
|
30
23
|
|
31
24
|
require 'rake/testtask'
|
32
25
|
Rake::TestTask.new(:test) do |test|
|
33
26
|
test.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb", "test/vendor")
|
27
|
+
test.libs << "test"
|
34
28
|
test.verbose = false
|
35
29
|
test.warning = true
|
36
30
|
end
|
@@ -42,6 +36,7 @@ begin
|
|
42
36
|
require 'rcov/rcovtask'
|
43
37
|
Rcov::RcovTask.new do |t|
|
44
38
|
t.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb", "test/vendor")
|
39
|
+
t.libs << "test"
|
45
40
|
t.rcov_opts << "--sort coverage"
|
46
41
|
t.rcov_opts << "--exclude gems"
|
47
42
|
t.warning = true
|
@@ -66,5 +61,5 @@ begin
|
|
66
61
|
end
|
67
62
|
rescue LoadError
|
68
63
|
puts "\nIt looks like you're using an old version of RDoc, but FakeWeb requires a newer one."
|
69
|
-
puts "You can try upgrading with `
|
64
|
+
puts "You can try upgrading with `gem install rdoc`.\n\n"
|
70
65
|
end
|
data/fakeweb.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{fakeweb}
|
8
|
-
s.version = "1.2.
|
8
|
+
s.version = "1.2.8"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Chris Kampmeier", "Blaine Cook"]
|
12
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-12-25}
|
13
13
|
s.description = %q{FakeWeb is a helper for faking web requests in Ruby. It works at a global level, without modifying code or writing extensive stubs.}
|
14
14
|
s.email = ["chris@kampers.net", "romeda@gmail.com"]
|
15
15
|
s.extra_rdoc_files = [
|
@@ -24,7 +24,6 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"fakeweb.gemspec",
|
26
26
|
"lib/fake_web.rb",
|
27
|
-
"lib/fake_web/VERSION",
|
28
27
|
"lib/fake_web/ext/net_http.rb",
|
29
28
|
"lib/fake_web/registry.rb",
|
30
29
|
"lib/fake_web/responder.rb",
|
@@ -44,6 +43,7 @@ Gem::Specification.new do |s|
|
|
44
43
|
"test/test_fake_web_open_uri.rb",
|
45
44
|
"test/test_helper.rb",
|
46
45
|
"test/test_missing_open_uri.rb",
|
46
|
+
"test/test_missing_pathname.rb",
|
47
47
|
"test/test_other_net_http_libraries.rb",
|
48
48
|
"test/test_precedence.rb",
|
49
49
|
"test/test_query_string.rb",
|
@@ -87,6 +87,7 @@ Gem::Specification.new do |s|
|
|
87
87
|
"test/test_fake_web_open_uri.rb",
|
88
88
|
"test/test_helper.rb",
|
89
89
|
"test/test_missing_open_uri.rb",
|
90
|
+
"test/test_missing_pathname.rb",
|
90
91
|
"test/test_other_net_http_libraries.rb",
|
91
92
|
"test/test_precedence.rb",
|
92
93
|
"test/test_query_string.rb",
|
data/lib/fake_web.rb
CHANGED
@@ -13,7 +13,7 @@ FakeWeb::Utility.puts_warning_for_net_http_around_advice_libs_if_needed
|
|
13
13
|
module FakeWeb
|
14
14
|
|
15
15
|
# Returns the version string for the copy of FakeWeb you have loaded.
|
16
|
-
VERSION =
|
16
|
+
VERSION = '1.2.8'
|
17
17
|
|
18
18
|
# Resets the FakeWeb Registry. This will force all subsequent web requests to
|
19
19
|
# behave as real requests.
|
data/lib/fake_web/registry.rb
CHANGED
@@ -19,29 +19,29 @@ module FakeWeb
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def registered_uri?(method, uri)
|
22
|
-
!
|
22
|
+
!responders_for(method, uri).empty?
|
23
23
|
end
|
24
24
|
|
25
25
|
def response_for(method, uri, &block)
|
26
|
-
|
27
|
-
return nil if
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
if
|
32
|
-
|
33
|
-
|
26
|
+
responders = responders_for(method, uri)
|
27
|
+
return nil if responders.empty?
|
28
|
+
|
29
|
+
next_responder = responders.last
|
30
|
+
responders.each do |responder|
|
31
|
+
if responder.times and responder.times > 0
|
32
|
+
responder.times -= 1
|
33
|
+
next_responder = responder
|
34
34
|
break
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
|
38
|
+
next_responder.response(&block)
|
39
39
|
end
|
40
40
|
|
41
41
|
|
42
42
|
private
|
43
43
|
|
44
|
-
def
|
44
|
+
def responders_for(method, uri)
|
45
45
|
uri = normalize_uri(uri)
|
46
46
|
|
47
47
|
uri_map_matches(method, uri, URI) ||
|
data/lib/fake_web/responder.rb
CHANGED
@@ -47,6 +47,8 @@ module FakeWeb
|
|
47
47
|
def body
|
48
48
|
return '' unless options.has_key?(:body)
|
49
49
|
|
50
|
+
options[:body] = options[:body].to_s if defined?(Pathname) && options[:body].is_a?(Pathname)
|
51
|
+
|
50
52
|
if !options[:body].include?("\0") && File.exists?(options[:body]) && !File.directory?(options[:body])
|
51
53
|
File.read(options[:body])
|
52
54
|
else
|
@@ -55,30 +57,31 @@ module FakeWeb
|
|
55
57
|
end
|
56
58
|
|
57
59
|
def baked_response
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
socket = Net::BufferedIO.new(options[:response])
|
60
|
+
return options[:response] if options[:response].is_a?(Net::HTTPResponse)
|
61
|
+
|
62
|
+
if options[:response].is_a?(String) || (defined?(Pathname) && options[:response].is_a?(Pathname))
|
63
|
+
socket = Net::BufferedIO.new(options[:response].to_s)
|
62
64
|
r = Net::HTTPResponse.read_new(socket)
|
63
65
|
|
64
|
-
# Store the
|
66
|
+
# Store the original transfer-encoding
|
65
67
|
saved_transfer_encoding = r.instance_eval {
|
66
68
|
@header['transfer-encoding'] if @header.key?('transfer-encoding')
|
67
69
|
}
|
68
70
|
|
69
|
-
#
|
71
|
+
# Read the body of response
|
70
72
|
r.instance_eval { @header['transfer-encoding'] = nil }
|
71
73
|
r.reading_body(socket, true) {}
|
72
74
|
|
73
|
-
# Delete the transfer-encoding key from r.@header if there wasn't one
|
74
|
-
#
|
75
|
+
# Delete the transfer-encoding key from r.@header if there wasn't one;
|
76
|
+
# otherwise, restore the saved_transfer_encoding
|
75
77
|
if saved_transfer_encoding.nil?
|
76
78
|
r.instance_eval { @header.delete('transfer-encoding') }
|
77
79
|
else
|
78
80
|
r.instance_eval { @header['transfer-encoding'] = saved_transfer_encoding }
|
79
81
|
end
|
80
82
|
r
|
81
|
-
else
|
83
|
+
else
|
84
|
+
raise StandardError, "Handler unimplemented for response #{options[:response]}"
|
82
85
|
end
|
83
86
|
end
|
84
87
|
|
data/lib/fake_web/utility.rb
CHANGED
@@ -7,7 +7,7 @@ module FakeWeb
|
|
7
7
|
|
8
8
|
def self.encode_unsafe_chars_in_userinfo(userinfo)
|
9
9
|
unsafe_in_userinfo = /[^#{URI::REGEXP::PATTERN::UNRESERVED};&=+$,]|^(#{URI::REGEXP::PATTERN::ESCAPED})/
|
10
|
-
userinfo.split(":").map { |part|
|
10
|
+
userinfo.split(":").map { |part| uri_escape(part, unsafe_in_userinfo) }.join(":")
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.strip_default_port_from_uri(uri)
|
@@ -18,6 +18,16 @@ module FakeWeb
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
# Wrapper for URI escaping that switches between URI::Parser#escape and
|
22
|
+
# URI.escape for 1.9-compatibility
|
23
|
+
def self.uri_escape(*args)
|
24
|
+
if URI.const_defined?(:Parser)
|
25
|
+
URI::Parser.new.escape(*args)
|
26
|
+
else
|
27
|
+
URI.escape(*args)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
21
31
|
def self.puts_warning_for_net_http_around_advice_libs_if_needed
|
22
32
|
libs = {"Samuel" => defined?(Samuel)}
|
23
33
|
warnings = libs.select { |_, loaded| loaded }.map do |name, _|
|
data/test/test_deprecations.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class TestDeprecations < Test::Unit::TestCase
|
4
4
|
|
@@ -39,7 +39,7 @@ class TestDeprecations < Test::Unit::TestCase
|
|
39
39
|
|
40
40
|
def test_register_uri_with_file_option_prints_deprecation_warning
|
41
41
|
warning = capture_stderr do
|
42
|
-
FakeWeb.register_uri(:get, "http://example.com", :file =>
|
42
|
+
FakeWeb.register_uri(:get, "http://example.com", :file => fixture_path("test_example.txt"))
|
43
43
|
end
|
44
44
|
assert_match %r(deprecation warning: fakeweb's :file option)i, warning
|
45
45
|
end
|
data/test/test_fake_web.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class TestFakeWeb < Test::Unit::TestCase
|
4
4
|
|
@@ -36,7 +36,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
36
36
|
|
37
37
|
def test_register_uri_without_domain_name
|
38
38
|
assert_raises URI::InvalidURIError do
|
39
|
-
FakeWeb.register_uri(:get, 'test_example2.txt',
|
39
|
+
FakeWeb.register_uri(:get, 'test_example2.txt', fixture_path("test_example.txt"))
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -122,7 +122,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
122
122
|
end
|
123
123
|
|
124
124
|
def test_response_for_with_registered_uri
|
125
|
-
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body =>
|
125
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
126
126
|
assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
|
127
127
|
end
|
128
128
|
|
@@ -223,7 +223,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
223
223
|
end
|
224
224
|
|
225
225
|
def test_mock_request_with_block
|
226
|
-
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body =>
|
226
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
227
227
|
response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }
|
228
228
|
assert_equal 'test example content', response.body
|
229
229
|
end
|
@@ -253,7 +253,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
253
253
|
end
|
254
254
|
|
255
255
|
def test_mock_request_with_undocumented_full_uri_argument_style
|
256
|
-
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body =>
|
256
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
257
257
|
response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt') }
|
258
258
|
assert_equal 'test example content', response.body
|
259
259
|
end
|
@@ -265,7 +265,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
265
265
|
end
|
266
266
|
|
267
267
|
def test_mock_post
|
268
|
-
FakeWeb.register_uri(:post, 'http://mock/test_example.txt', :body =>
|
268
|
+
FakeWeb.register_uri(:post, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
269
269
|
response = Net::HTTP.start('mock') { |query| query.post('/test_example.txt', '') }
|
270
270
|
assert_equal 'test example content', response.body
|
271
271
|
end
|
@@ -284,14 +284,14 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
284
284
|
end
|
285
285
|
|
286
286
|
def test_mock_get_with_request_from_file_as_registered_uri
|
287
|
-
FakeWeb.register_uri(:get, 'http://www.google.com/', :response =>
|
287
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => fixture_path("google_response_without_transfer_encoding"))
|
288
288
|
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
289
289
|
assert_equal '200', response.code
|
290
290
|
assert response.body.include?('<title>Google</title>')
|
291
291
|
end
|
292
292
|
|
293
293
|
def test_mock_post_with_request_from_file_as_registered_uri
|
294
|
-
FakeWeb.register_uri(:post, 'http://www.google.com/', :response =>
|
294
|
+
FakeWeb.register_uri(:post, 'http://www.google.com/', :response => fixture_path("google_response_without_transfer_encoding"))
|
295
295
|
response = Net::HTTP.start('www.google.com') { |query| query.post('/', '') }
|
296
296
|
assert_equal "200", response.code
|
297
297
|
assert response.body.include?('<title>Google</title>')
|
@@ -408,7 +408,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
408
408
|
end
|
409
409
|
|
410
410
|
def test_mock_instance_syntax
|
411
|
-
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body =>
|
411
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
412
412
|
response = nil
|
413
413
|
uri = URI.parse('http://mock/test_example.txt')
|
414
414
|
http = Net::HTTP.new(uri.host, uri.port)
|
@@ -423,7 +423,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
423
423
|
response = nil
|
424
424
|
proxy_address = nil
|
425
425
|
proxy_port = nil
|
426
|
-
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body =>
|
426
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
427
427
|
uri = URI.parse('http://mock/test_example.txt')
|
428
428
|
http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
|
429
429
|
uri.host, (uri.port or 80))
|
@@ -451,7 +451,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
451
451
|
|
452
452
|
def test_mock_rotate_responses
|
453
453
|
FakeWeb.register_uri(:get, 'http://mock/multiple_test_example.txt',
|
454
|
-
[ {:body =>
|
454
|
+
[ {:body => fixture_path("test_example.txt"), :times => 2},
|
455
455
|
{:body => "thrice", :times => 3},
|
456
456
|
{:body => "ever_more"} ])
|
457
457
|
|
@@ -462,28 +462,28 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
462
462
|
end
|
463
463
|
|
464
464
|
def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header
|
465
|
-
FakeWeb.register_uri(:get, 'http://www.google.com/', :response =>
|
465
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => fixture_path("google_response_with_transfer_encoding"))
|
466
466
|
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
467
467
|
assert_not_nil response['transfer-encoding']
|
468
468
|
assert response['transfer-encoding'] == 'chunked'
|
469
469
|
end
|
470
470
|
|
471
471
|
def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header
|
472
|
-
FakeWeb.register_uri(:get, 'http://www.google.com/', :response =>
|
472
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => fixture_path("google_response_without_transfer_encoding"))
|
473
473
|
response = nil
|
474
474
|
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
475
475
|
assert !response.key?('transfer-encoding')
|
476
476
|
end
|
477
477
|
|
478
478
|
def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header
|
479
|
-
FakeWeb.register_uri(:get, 'http://www.google.com/', :response =>
|
479
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => fixture_path("google_response_from_curl"))
|
480
480
|
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
481
481
|
assert_not_nil response['transfer-encoding']
|
482
482
|
assert response['transfer-encoding'] == 'chunked'
|
483
483
|
end
|
484
484
|
|
485
485
|
def test_txt_file_should_have_three_lines
|
486
|
-
FakeWeb.register_uri(:get, 'http://www.google.com/', :body =>
|
486
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :body => fixture_path("test_txt_file"))
|
487
487
|
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
488
488
|
assert response.body.split(/\n/).size == 3, "response has #{response.body.split(/\n/).size} lines should have 3"
|
489
489
|
end
|
@@ -520,6 +520,20 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
520
520
|
assert_equal File.dirname(__FILE__), body
|
521
521
|
end
|
522
522
|
|
523
|
+
def test_registering_with_a_body_pointing_to_a_pathname
|
524
|
+
path = Pathname.new(fixture_path("test_example.txt"))
|
525
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => path)
|
526
|
+
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
527
|
+
assert_equal "test example content", response.body
|
528
|
+
end
|
529
|
+
|
530
|
+
def test_registering_with_a_response_pointing_to_a_pathname
|
531
|
+
path = Pathname.new(fixture_path("google_response_without_transfer_encoding"))
|
532
|
+
FakeWeb.register_uri(:get, "http://google.com", :response => path)
|
533
|
+
response = Net::HTTP.start("google.com") { |http| http.get("/") }
|
534
|
+
assert response.body.include?("<title>Google</title>")
|
535
|
+
end
|
536
|
+
|
523
537
|
def test_http_version_from_string_response
|
524
538
|
FakeWeb.register_uri(:get, "http://example.com", :body => "example")
|
525
539
|
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
@@ -527,13 +541,13 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
527
541
|
end
|
528
542
|
|
529
543
|
def test_http_version_from_file_response
|
530
|
-
FakeWeb.register_uri(:get, "http://example.com", :body =>
|
544
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => fixture_path("test_example.txt"))
|
531
545
|
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
532
546
|
assert_equal "1.0", response.http_version
|
533
547
|
end
|
534
548
|
|
535
549
|
def test_version
|
536
|
-
assert_equal "1.2.
|
550
|
+
assert_equal "1.2.8", FakeWeb::VERSION
|
537
551
|
end
|
538
552
|
|
539
553
|
end
|
@@ -1,14 +1,14 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class TestFakeWebOpenURI < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def test_content_for_registered_uri
|
6
|
-
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body =>
|
6
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
7
7
|
assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_mock_open
|
11
|
-
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body =>
|
11
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
12
12
|
assert_equal 'test example content', open('http://mock/test_example.txt').read
|
13
13
|
end
|
14
14
|
|
@@ -51,7 +51,7 @@ class TestFakeWebOpenURI < Test::Unit::TestCase
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_mock_open_with_block
|
54
|
-
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body =>
|
54
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
55
55
|
body = open('http://mock/test_example.txt') { |f| f.readlines }
|
56
56
|
assert_equal 'test example content', body.first
|
57
57
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
$:.unshift "#{File.dirname(__FILE__)}/../lib"
|
2
|
-
|
3
1
|
require 'test/unit'
|
4
2
|
require 'open-uri'
|
3
|
+
require 'pathname'
|
5
4
|
require 'fake_web'
|
5
|
+
require 'rbconfig'
|
6
6
|
require 'rubygems'
|
7
7
|
require 'mocha'
|
8
8
|
|
@@ -25,6 +25,10 @@ end
|
|
25
25
|
|
26
26
|
module FakeWebTestHelper
|
27
27
|
|
28
|
+
def fixture_path(basename)
|
29
|
+
"test/fixtures/#{basename}"
|
30
|
+
end
|
31
|
+
|
28
32
|
def capture_stderr
|
29
33
|
$stderr = StringIO.new
|
30
34
|
yield
|
@@ -33,6 +37,12 @@ module FakeWebTestHelper
|
|
33
37
|
$stderr = STDERR
|
34
38
|
end
|
35
39
|
|
40
|
+
# The path to the current ruby interpreter. Adapted from Rake's FileUtils.
|
41
|
+
def ruby_path
|
42
|
+
ext = ((RbConfig::CONFIG['ruby_install_name'] =~ /\.(com|cmd|exe|bat|rb|sh)$/) ? "" : RbConfig::CONFIG['EXEEXT'])
|
43
|
+
File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'] + ext).sub(/.*\s.*/m, '"\&"')
|
44
|
+
end
|
45
|
+
|
36
46
|
# Sets several expectations (using Mocha) that a real HTTP request makes it
|
37
47
|
# past FakeWeb to the socket layer. You can use this when you need to check
|
38
48
|
# that a request isn't handled by FakeWeb.
|
@@ -57,7 +67,8 @@ module FakeWebTestHelper
|
|
57
67
|
request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
|
58
68
|
socket.expects(:write).with(all_of(includes(request_parts[0]), includes(request_parts[1]))).returns(100)
|
59
69
|
|
60
|
-
|
70
|
+
read_method = RUBY_VERSION >= "1.9.2" ? :read_nonblock : :sysread
|
71
|
+
socket.expects(read_method).at_least_once.returns("HTTP/1.1 #{options[:response_code]} #{options[:response_message]}\nContent-Length: #{options[:response_body].length}\n\n#{options[:response_body]}").then.raises(EOFError)
|
61
72
|
end
|
62
73
|
|
63
74
|
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestMissingPathname < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
super
|
7
|
+
@saved_pathname = Pathname
|
8
|
+
Object.send(:remove_const, :Pathname)
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
super
|
13
|
+
Object.const_set(:Pathname, @saved_pathname)
|
14
|
+
end
|
15
|
+
|
16
|
+
# FakeWeb supports using Pathname objects where filenames are expected, but
|
17
|
+
# Pathname isn't required to use FakeWeb. Make sure everything still works
|
18
|
+
# when Pathname isn't in use.
|
19
|
+
|
20
|
+
def test_register_using_body_without_pathname
|
21
|
+
FakeWeb.register_uri(:get, "http://example.com/", :body => fixture_path("test_example.txt"))
|
22
|
+
Net::HTTP.start("example.com") { |http| http.get("/") }
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_register_using_response_without_pathname
|
26
|
+
FakeWeb.register_uri(:get, "http://example.com/", :response => fixture_path("google_response_without_transfer_encoding"))
|
27
|
+
Net::HTTP.start("example.com") { |http| http.get("/") }
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_register_using_unsupported_response_without_pathname
|
31
|
+
FakeWeb.register_uri(:get, "http://example.com/", :response => 1)
|
32
|
+
assert_raise StandardError do
|
33
|
+
Net::HTTP.start("example.com") { |http| http.get("/") }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class TestOtherNetHttpLibraries < Test::Unit::TestCase
|
4
4
|
|
@@ -8,8 +8,7 @@ class TestOtherNetHttpLibraries < Test::Unit::TestCase
|
|
8
8
|
vendor_dirs = Dir["#{File.dirname(__FILE__)}/vendor/*/lib"]
|
9
9
|
load_path_opts = vendor_dirs.unshift(fakeweb_dir).map { |dir| "-I#{dir}" }.join(" ")
|
10
10
|
|
11
|
-
#
|
12
|
-
`ruby #{load_path_opts} -e "#{requires}; #{additional_code}" 2>&1`
|
11
|
+
`#{ruby_path} #{load_path_opts} -e "#{requires}; #{additional_code}" 2>&1`
|
13
12
|
end
|
14
13
|
|
15
14
|
def test_requiring_samuel_before_fakeweb_prints_warning
|
data/test/test_precedence.rb
CHANGED
data/test/test_query_string.rb
CHANGED
data/test/test_regexes.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class TestResponseHeaders < Test::Unit::TestCase
|
4
4
|
|
@@ -23,7 +23,7 @@ class TestResponseHeaders < Test::Unit::TestCase
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_cookies_when_registering_with_file_and_set_cookie_header
|
26
|
-
FakeWeb.register_uri(:get, "http://example.com/", :body =>
|
26
|
+
FakeWeb.register_uri(:get, "http://example.com/", :body => fixture_path("test_example.txt"),
|
27
27
|
:set_cookie => "user_id=1; example=yes")
|
28
28
|
response = Net::HTTP.start("example.com") { |query| query.get("/") }
|
29
29
|
assert_equal "test example content", response.body
|
data/test/test_utility.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class TestUtility < Test::Unit::TestCase
|
4
4
|
|
@@ -67,4 +67,10 @@ class TestUtility < Test::Unit::TestCase
|
|
67
67
|
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
68
68
|
end
|
69
69
|
|
70
|
+
def test_uri_escape_delegates_to_uri_parser_when_available
|
71
|
+
parsing_object = URI.const_defined?(:Parser) ? URI::Parser.any_instance : URI
|
72
|
+
parsing_object.expects(:escape).with("string", /unsafe/).returns("escaped")
|
73
|
+
assert_equal "escaped", FakeWeb::Utility.uri_escape("string", /unsafe/)
|
74
|
+
end
|
75
|
+
|
70
76
|
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.
|
4
|
+
version: 1.2.8
|
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-
|
13
|
+
date: 2009-12-25 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -42,7 +42,6 @@ files:
|
|
42
42
|
- Rakefile
|
43
43
|
- fakeweb.gemspec
|
44
44
|
- lib/fake_web.rb
|
45
|
-
- lib/fake_web/VERSION
|
46
45
|
- lib/fake_web/ext/net_http.rb
|
47
46
|
- lib/fake_web/registry.rb
|
48
47
|
- lib/fake_web/responder.rb
|
@@ -62,6 +61,7 @@ files:
|
|
62
61
|
- test/test_fake_web_open_uri.rb
|
63
62
|
- test/test_helper.rb
|
64
63
|
- test/test_missing_open_uri.rb
|
64
|
+
- test/test_missing_pathname.rb
|
65
65
|
- test/test_other_net_http_libraries.rb
|
66
66
|
- test/test_precedence.rb
|
67
67
|
- test/test_query_string.rb
|
@@ -126,6 +126,7 @@ test_files:
|
|
126
126
|
- test/test_fake_web_open_uri.rb
|
127
127
|
- test/test_helper.rb
|
128
128
|
- test/test_missing_open_uri.rb
|
129
|
+
- test/test_missing_pathname.rb
|
129
130
|
- test/test_other_net_http_libraries.rb
|
130
131
|
- test/test_precedence.rb
|
131
132
|
- test/test_query_string.rb
|
data/lib/fake_web/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.2.7
|