rubyist-fakeweb 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +128 -0
- data/LICENSE.txt +281 -0
- data/README.rdoc +196 -0
- data/Rakefile +70 -0
- data/lib/fake_web.rb +154 -0
- data/lib/fake_web/ext/net_http.rb +72 -0
- data/lib/fake_web/registry.rb +79 -0
- data/lib/fake_web/responder.rb +122 -0
- data/lib/fake_web/response.rb +10 -0
- data/lib/fake_web/stub_socket.rb +15 -0
- data/lib/fake_web/url_encoded_pair_parser.rb +88 -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 +41 -0
- data/test/test_fake_authentication.rb +68 -0
- data/test/test_fake_web.rb +617 -0
- data/test/test_fake_web_open_uri.rb +62 -0
- data/test/test_helper.rb +52 -0
- data/test/test_missing_open_uri.rb +24 -0
- data/test/test_query_string.rb +49 -0
- data/test/test_trailing_slashes.rb +62 -0
- metadata +114 -0
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestFakeWebOpenURI < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
FakeWeb.clean_registry
|
7
|
+
end
|
8
|
+
|
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(nil, 'http://mock/test_example.txt').body
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_mock_open
|
15
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
16
|
+
assert_equal 'test example content', open('http://mock/test_example.txt').read
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_mock_open_with_string_as_registered_uri
|
20
|
+
FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
|
21
|
+
assert_equal 'foo', open('http://mock/test_string.txt').string
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_real_open
|
25
|
+
setup_expectations_for_real_apple_hot_news_request
|
26
|
+
resp = open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
27
|
+
assert_equal "200", resp.status.first
|
28
|
+
body = resp.read
|
29
|
+
assert body.include?('Apple')
|
30
|
+
assert body.include?('News')
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_mock_open_that_raises_exception
|
34
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
|
35
|
+
assert_raises(StandardError) do
|
36
|
+
open('http://mock/raising_exception.txt')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_mock_open_that_raises_an_http_error
|
41
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
|
42
|
+
assert_raises(OpenURI::HTTPError) do
|
43
|
+
open('http://mock/raising_exception.txt')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
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'])
|
49
|
+
exception = assert_raises(OpenURI::HTTPError) do
|
50
|
+
open('http://mock/raising_exception.txt')
|
51
|
+
end
|
52
|
+
assert_equal '123', exception.io.code
|
53
|
+
assert_equal 'jodel', exception.io.message
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_mock_open_with_block
|
57
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
58
|
+
open('http://mock/test_example.txt') do |f|
|
59
|
+
assert 'test example content', f.readlines
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
$:.unshift "#{File.dirname(__FILE__)}/../lib"
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'fake_web'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'mocha'
|
8
|
+
|
9
|
+
module FakeWebTestHelper
|
10
|
+
|
11
|
+
# Sets several expectations (using Mocha) that a real HTTP request makes it
|
12
|
+
# past FakeWeb to the socket layer. You can use this when you need to check
|
13
|
+
# that a request isn't handled by FakeWeb.
|
14
|
+
def setup_expectations_for_real_request(options = {})
|
15
|
+
# Socket handling
|
16
|
+
if options[:port] == 443
|
17
|
+
socket = mock("SSLSocket")
|
18
|
+
OpenSSL::SSL::SSLSocket.expects(:===).with(socket).returns(true).at_least_once
|
19
|
+
OpenSSL::SSL::SSLSocket.expects(:new).with(socket, instance_of(OpenSSL::SSL::SSLContext)).returns(socket).at_least_once
|
20
|
+
socket.stubs(:sync_close=).returns(true)
|
21
|
+
socket.expects(:connect).with().at_least_once
|
22
|
+
else
|
23
|
+
socket = mock("TCPSocket")
|
24
|
+
Socket.expects(:===).with(socket).returns(true)
|
25
|
+
end
|
26
|
+
|
27
|
+
TCPSocket.expects(:open).with(options[:host], options[:port]).returns(socket).at_least_once
|
28
|
+
socket.stubs(:closed?).returns(false)
|
29
|
+
socket.stubs(:close).returns(true)
|
30
|
+
|
31
|
+
# Request/response handling
|
32
|
+
request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
|
33
|
+
socket.expects(:write).with(all_of(includes(request_parts[0]), includes(request_parts[1]))).returns(100)
|
34
|
+
|
35
|
+
# TODO: handle long response bodies that use more than one #sysread call
|
36
|
+
socket.expects(:sysread).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)
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
# A helper that calls #setup_expectations_for_real_request for you, using
|
41
|
+
# defaults for our commonly used test request to images.apple.com.
|
42
|
+
def setup_expectations_for_real_apple_hot_news_request(options = {})
|
43
|
+
defaults = { :host => "images.apple.com", :port => 80, :method => "GET",
|
44
|
+
:path => "/main/rss/hotnews/hotnews.rss",
|
45
|
+
:response_code => 200, :response_message => "OK",
|
46
|
+
:response_body => "<title>Apple Hot News</title>" }
|
47
|
+
setup_expectations_for_real_request(defaults.merge(options))
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
Test::Unit::TestCase.send(:include, FakeWebTestHelper)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestMissingOpenURI < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
FakeWeb.clean_registry
|
7
|
+
@saved_open_uri = OpenURI
|
8
|
+
Object.send(:remove_const, :OpenURI)
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
Object.const_set(:OpenURI, @saved_open_uri)
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def test_register_using_exception_without_open_uri
|
17
|
+
# regression test for Responder needing OpenURI::HTTPError to be defined
|
18
|
+
FakeWeb.register_uri(:get, "http://example.com/", :exception => StandardError)
|
19
|
+
assert_raises(StandardError) do
|
20
|
+
Net::HTTP.start("example.com") { |http| http.get("/") }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestFakeWebQueryString < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
FakeWeb.clean_registry
|
7
|
+
end
|
8
|
+
|
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')
|
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")
|
15
|
+
end
|
16
|
+
|
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')
|
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')
|
23
|
+
end
|
24
|
+
|
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/?')
|
28
|
+
|
29
|
+
FakeWeb.register_uri(URI.parse('http://example.org/'), :string => 'foo')
|
30
|
+
assert FakeWeb.registered_uri?('http://example.org/?')
|
31
|
+
end
|
32
|
+
|
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/')
|
36
|
+
|
37
|
+
FakeWeb.register_uri(URI.parse('http://example.org/?'), :string => 'foo')
|
38
|
+
assert FakeWeb.registered_uri?('http://example.org/')
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_registry_sort_query_params
|
42
|
+
assert_equal "a=1&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "b=2&a=1")
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_registry_sort_query_params_sorts_by_value_if_keys_collide
|
46
|
+
assert_equal "a=1&a=2&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "a=2&b=2&a=1")
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -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
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubyist-fakeweb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Blaine Cook
|
8
|
+
- Chris Kampmeier
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-05-04 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:
|
27
|
+
email: chris@kampers.net
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- CHANGELOG
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.rdoc
|
36
|
+
files:
|
37
|
+
- CHANGELOG
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.rdoc
|
40
|
+
- Rakefile
|
41
|
+
- lib
|
42
|
+
- lib/fake_web
|
43
|
+
- lib/fake_web.rb
|
44
|
+
- lib/fake_web/ext
|
45
|
+
- lib/fake_web/ext/net_http.rb
|
46
|
+
- lib/fake_web/registry.rb
|
47
|
+
- lib/fake_web/responder.rb
|
48
|
+
- lib/fake_web/response.rb
|
49
|
+
- lib/fake_web/stub_socket.rb
|
50
|
+
- lib/fake_web/url_encoded_pair_parser.rb
|
51
|
+
- lib/fakeweb.rb
|
52
|
+
- test
|
53
|
+
- test/fixtures
|
54
|
+
- test/fixtures/google_response_from_curl
|
55
|
+
- test/fixtures/google_response_with_transfer_encoding
|
56
|
+
- test/fixtures/google_response_without_transfer_encoding
|
57
|
+
- test/fixtures/test_example.txt
|
58
|
+
- test/fixtures/test_txt_file
|
59
|
+
- test/test_allow_net_connect.rb
|
60
|
+
- test/test_fake_authentication.rb
|
61
|
+
- test/test_fake_web.rb
|
62
|
+
- test/test_fake_web_open_uri.rb
|
63
|
+
- test/test_helper.rb
|
64
|
+
- test/test_missing_open_uri.rb
|
65
|
+
- test/test_query_string.rb
|
66
|
+
- test/test_trailing_slashes.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/chrisk/fakeweb
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- --main
|
72
|
+
- README.rdoc
|
73
|
+
- --title
|
74
|
+
- FakeWeb API Documentation
|
75
|
+
- --charset
|
76
|
+
- utf-8
|
77
|
+
- --line-numbers
|
78
|
+
- --inline-source
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: "0"
|
86
|
+
version:
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
version:
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project: fakeweb
|
96
|
+
rubygems_version: 1.2.0
|
97
|
+
signing_key:
|
98
|
+
specification_version: 2
|
99
|
+
summary: A tool for faking responses to HTTP requests
|
100
|
+
test_files:
|
101
|
+
- test/fixtures
|
102
|
+
- test/fixtures/google_response_from_curl
|
103
|
+
- test/fixtures/google_response_with_transfer_encoding
|
104
|
+
- test/fixtures/google_response_without_transfer_encoding
|
105
|
+
- test/fixtures/test_example.txt
|
106
|
+
- test/fixtures/test_txt_file
|
107
|
+
- test/test_allow_net_connect.rb
|
108
|
+
- test/test_fake_authentication.rb
|
109
|
+
- test/test_fake_web.rb
|
110
|
+
- test/test_fake_web_open_uri.rb
|
111
|
+
- test/test_helper.rb
|
112
|
+
- test/test_missing_open_uri.rb
|
113
|
+
- test/test_query_string.rb
|
114
|
+
- test/test_trailing_slashes.rb
|