fcoury-fakeweb 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -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,60 @@
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
+ def test_register_url_without_query_and_ignoring_query_params
38
+ FakeWeb.ignore_query_params = true
39
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
40
+ assert FakeWeb.registered_uri?('http://example.com/?b=1&a=1')
41
+ end
42
+
43
+ def test_register_url_without_query_and_not_ignoring_query_params
44
+ FakeWeb.ignore_query_params = false
45
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
46
+ assert !FakeWeb.registered_uri?('http://example.com/?b=1&a=1')
47
+ end
48
+
49
+ def test_response_while_ignoring_query_params
50
+ FakeWeb.ignore_query_params = true
51
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
52
+ assert_equal 'foo', open('http://example.com/?b=1&a=1').string
53
+ end
54
+
55
+ def test_response_when_not_ignoring_query_params
56
+ FakeWeb.ignore_query_params = false
57
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
58
+ assert_not_equal 'foo', open('http://example.com/?b=1&a=1').string
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fcoury-fakeweb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Blaine Cook
8
+ - Chris Kampmeier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-03-07 00:00:00 -03: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/fake_web.rb
42
+ - lib/fake_web/ext/net_http.rb
43
+ - lib/fake_web/registry.rb
44
+ - lib/fake_web/responder.rb
45
+ - lib/fake_web/response.rb
46
+ - lib/fake_web/stub_socket.rb
47
+ - lib/fakeweb.rb
48
+ - test/fixtures/google_response_from_curl
49
+ - test/fixtures/google_response_with_transfer_encoding
50
+ - test/fixtures/google_response_without_transfer_encoding
51
+ - test/fixtures/test_example.txt
52
+ - test/fixtures/test_txt_file
53
+ - test/test_allow_net_connect.rb
54
+ - test/test_fake_authentication.rb
55
+ - test/test_fake_web.rb
56
+ - test/test_fake_web_open_uri.rb
57
+ - test/test_helper.rb
58
+ - test/test_query_string.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/chrisk/fakeweb
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --main
66
+ - README.rdoc
67
+ - --title
68
+ - FakeWeb API Documentation
69
+ - --charset
70
+ - utf-8
71
+ - --line-numbers
72
+ - --inline-source
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ requirements: []
88
+
89
+ rubyforge_project: fcoury-fakeweb
90
+ rubygems_version: 1.3.5
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: A tool for faking responses to HTTP requests
94
+ test_files:
95
+ - test/fixtures/google_response_from_curl
96
+ - test/fixtures/google_response_with_transfer_encoding
97
+ - test/fixtures/google_response_without_transfer_encoding
98
+ - test/fixtures/test_example.txt
99
+ - test/fixtures/test_txt_file
100
+ - test/test_allow_net_connect.rb
101
+ - test/test_fake_authentication.rb
102
+ - test/test_fake_web.rb
103
+ - test/test_fake_web_open_uri.rb
104
+ - test/test_helper.rb
105
+ - test/test_query_string.rb