dkubb-fakeweb 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.
@@ -0,0 +1,64 @@
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
+ FakeWeb::CLIENT_LIBRARIES.each { |library_identifier|
12
+ assert_equal 'test example content', FakeWeb.response_for(library_identifier, 'http://mock/test_example.txt').get_content
13
+ }
14
+ end
15
+
16
+ def test_mock_open
17
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
18
+ assert_equal 'test example content', open('http://mock/test_example.txt').read
19
+ end
20
+
21
+ def test_mock_open_with_string_as_registered_uri
22
+ FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
23
+ assert_equal 'foo', open('http://mock/test_string.txt').string
24
+ end
25
+
26
+ def test_real_open
27
+ setup_expectations_for_real_apple_hot_news_request
28
+ resp = open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
29
+ assert_equal "200", resp.status.first
30
+ body = resp.read
31
+ assert body.include?('Apple')
32
+ assert body.include?('News')
33
+ end
34
+
35
+ def test_mock_open_that_raises_exception
36
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
37
+ assert_raises(StandardError) do
38
+ open('http://mock/raising_exception.txt')
39
+ end
40
+ end
41
+
42
+ def test_mock_open_that_raises_an_http_error
43
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
44
+ assert_raises(OpenURI::HTTPError) do
45
+ open('http://mock/raising_exception.txt')
46
+ end
47
+ end
48
+
49
+ def test_mock_open_that_raises_an_http_error_with_a_specific_status
50
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
51
+ exception = assert_raises(OpenURI::HTTPError) do
52
+ open('http://mock/raising_exception.txt')
53
+ end
54
+ assert_equal '123', exception.io.code
55
+ assert_equal 'jodel', exception.io.message
56
+ end
57
+
58
+ def test_mock_open_with_block
59
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
60
+ open('http://mock/test_example.txt') do |f|
61
+ assert 'test example content', f.readlines
62
+ end
63
+ end
64
+ 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).with(1024).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,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dkubb-fakeweb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.2.6
5
+ platform: ruby
6
+ authors:
7
+ - Blaine Cook
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-31 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_httpclient.rb
33
+ - lib/fake_net_http.rb
34
+ - lib/fake_web.rb
35
+ - test
36
+ - test/fixtures
37
+ - test/fixtures/test_example.txt
38
+ - test/fixtures/test_request
39
+ - test/test_allow_net_connect.rb
40
+ - test/test_fake_web.rb
41
+ - test/test_fake_web_open_uri.rb
42
+ - test/test_helper.rb
43
+ - test/test_query_string.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/chrisk/fakeweb
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main
49
+ - README.rdoc
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: A tool for faking responses to HTTP requests
71
+ test_files:
72
+ - test/fixtures
73
+ - test/fixtures/test_example.txt
74
+ - test/fixtures/test_request
75
+ - test/test_allow_net_connect.rb
76
+ - test/test_fake_web.rb
77
+ - test/test_fake_web_open_uri.rb
78
+ - test/test_helper.rb
79
+ - test/test_query_string.rb