mbleigh-fakeweb 1.1.3.8
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 +90 -0
- data/LICENSE.txt +281 -0
- data/README.rdoc +179 -0
- data/Rakefile +63 -0
- data/lib/fake_web.rb +153 -0
- data/lib/fake_web/ext/net_http.rb +59 -0
- data/lib/fake_web/registry.rb +78 -0
- data/lib/fake_web/responder.rb +99 -0
- data/lib/fake_web/response.rb +10 -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 +39 -0
- data/test/test_fake_web.rb +485 -0
- data/test/test_fake_web_open_uri.rb +62 -0
- data/test/test_helper.rb +52 -0
- data/test/test_query_string.rb +37 -0
- metadata +91 -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('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,37 @@
|
|
|
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_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
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_register_uri_with_query_params_and_check_in_different_order
|
|
15
|
+
FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
|
|
16
|
+
assert FakeWeb.registered_uri?('http://example.com/?b=1&a=1')
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_registered_uri_gets_recognized_with_empty_query_params
|
|
20
|
+
FakeWeb.register_uri('http://example.com/', :string => 'foo')
|
|
21
|
+
assert FakeWeb.registered_uri?('http://example.com/?')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_register_uri_with_empty_query_params_and_check_with_none
|
|
25
|
+
FakeWeb.register_uri('http://example.com/?', :string => 'foo')
|
|
26
|
+
assert FakeWeb.registered_uri?('http://example.com/')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_registry_sort_query_params
|
|
30
|
+
assert_equal "a=1&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "b=2&a=1")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_registry_sort_query_params_sorts_by_value_if_keys_collide
|
|
34
|
+
assert_equal "a=1&a=2&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "a=2&b=2&a=1")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mbleigh-fakeweb
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.1.3.8
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Blaine Cook
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-02-19 00:00:00 -08:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description:
|
|
17
|
+
email:
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- CHANGELOG
|
|
24
|
+
- LICENSE.txt
|
|
25
|
+
- README.rdoc
|
|
26
|
+
files:
|
|
27
|
+
- CHANGELOG
|
|
28
|
+
- LICENSE.txt
|
|
29
|
+
- README.rdoc
|
|
30
|
+
- Rakefile
|
|
31
|
+
- lib
|
|
32
|
+
- lib/fake_web
|
|
33
|
+
- lib/fake_web.rb
|
|
34
|
+
- lib/fake_web/ext
|
|
35
|
+
- lib/fake_web/ext/net_http.rb
|
|
36
|
+
- lib/fake_web/registry.rb
|
|
37
|
+
- lib/fake_web/responder.rb
|
|
38
|
+
- lib/fake_web/response.rb
|
|
39
|
+
- lib/fake_web/socket_delegator.rb
|
|
40
|
+
- test
|
|
41
|
+
- test/fixtures
|
|
42
|
+
- test/fixtures/google_response_from_curl
|
|
43
|
+
- test/fixtures/google_response_with_transfer_encoding
|
|
44
|
+
- test/fixtures/google_response_without_transfer_encoding
|
|
45
|
+
- test/fixtures/test_example.txt
|
|
46
|
+
- test/fixtures/test_txt_file
|
|
47
|
+
- test/test_allow_net_connect.rb
|
|
48
|
+
- test/test_fake_web.rb
|
|
49
|
+
- test/test_fake_web_open_uri.rb
|
|
50
|
+
- test/test_helper.rb
|
|
51
|
+
- test/test_query_string.rb
|
|
52
|
+
has_rdoc: true
|
|
53
|
+
homepage: http://github.com/chrisk/fakeweb
|
|
54
|
+
post_install_message:
|
|
55
|
+
rdoc_options:
|
|
56
|
+
- --main
|
|
57
|
+
- README.rdoc
|
|
58
|
+
require_paths:
|
|
59
|
+
- lib
|
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: "0"
|
|
65
|
+
version:
|
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: "0"
|
|
71
|
+
version:
|
|
72
|
+
requirements: []
|
|
73
|
+
|
|
74
|
+
rubyforge_project:
|
|
75
|
+
rubygems_version: 1.2.0
|
|
76
|
+
signing_key:
|
|
77
|
+
specification_version: 2
|
|
78
|
+
summary: A tool for faking responses to HTTP requests
|
|
79
|
+
test_files:
|
|
80
|
+
- test/fixtures
|
|
81
|
+
- test/fixtures/google_response_from_curl
|
|
82
|
+
- test/fixtures/google_response_with_transfer_encoding
|
|
83
|
+
- test/fixtures/google_response_without_transfer_encoding
|
|
84
|
+
- test/fixtures/test_example.txt
|
|
85
|
+
- test/fixtures/test_txt_file
|
|
86
|
+
- test/test_allow_net_connect.rb
|
|
87
|
+
- test/test_fake_web.rb
|
|
88
|
+
- test/test_fake_web_open_uri.rb
|
|
89
|
+
- test/test_helper.rb
|
|
90
|
+
- test/test_query_string.rb
|
|
91
|
+
- test/test_fake_authentication.rb
|