test/test_fake_web.rb in chrisk-fakeweb-1.1.2.6 vs test/test_fake_web.rb in chrisk-fakeweb-1.1.2.7

- old
+ new

@@ -1,35 +1,46 @@ -# FakeWeb - Ruby Helper for Faking Web Requests -# Copyright 2006 Blaine Cook <romeda@gmail.com>. -# -# FakeWeb is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# FakeWeb is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with FakeWeb; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - require File.join(File.dirname(__FILE__), "test_helper") class TestFakeWeb < Test::Unit::TestCase def setup + FakeWeb.allow_net_connect = true FakeWeb.clean_registry - FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt') end def test_register_uri + FakeWeb.register_uri('http://mock/test_example.txt', :string => "example") assert FakeWeb.registered_uri?('http://mock/test_example.txt') end + def test_register_uri_with_wrong_number_of_arguments + assert_raises ArgumentError do + FakeWeb.register_uri("http://example.com") + end + assert_raises ArgumentError do + FakeWeb.register_uri(:get, "http://example.com", "/example", :string => "example") + end + end + + def test_registered_uri_with_wrong_number_of_arguments + assert_raises ArgumentError do + FakeWeb.registered_uri? + end + assert_raises ArgumentError do + FakeWeb.registered_uri?(:get, "http://example.com", "/example") + end + end + + def test_response_for_with_wrong_number_of_arguments + assert_raises ArgumentError do + FakeWeb.response_for + end + assert_raises ArgumentError do + FakeWeb.response_for(:get, "http://example.com", "/example") + end + end + def test_register_uri_without_domain_name assert_raises URI::InvalidURIError do FakeWeb.register_uri('test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt') end end @@ -62,14 +73,50 @@ def test_register_uri_with_no_port_for_https_and_check_with_default_port FakeWeb.register_uri('https://example.com/', :string => 'foo') assert FakeWeb.registered_uri?('https://example.com:443/') end - def test_content_for_registered_uri + def test_register_uri_for_any_method_explicitly + FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :string => "OK") + assert FakeWeb.registered_uri?(:get, "http://example.com/rpc_endpoint") + assert FakeWeb.registered_uri?(:post, "http://example.com/rpc_endpoint") + assert FakeWeb.registered_uri?(:put, "http://example.com/rpc_endpoint") + assert FakeWeb.registered_uri?(:delete, "http://example.com/rpc_endpoint") + assert FakeWeb.registered_uri?(:any, "http://example.com/rpc_endpoint") + assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint") + end + + def test_register_uri_for_get_method_only + FakeWeb.register_uri(:get, "http://example.com/users", :string => "User list") + assert FakeWeb.registered_uri?(:get, "http://example.com/users") + assert !FakeWeb.registered_uri?(:post, "http://example.com/users") + assert !FakeWeb.registered_uri?(:put, "http://example.com/users") + assert !FakeWeb.registered_uri?(:delete, "http://example.com/users") + assert !FakeWeb.registered_uri?(:any, "http://example.com/users") + assert !FakeWeb.registered_uri?("http://example.com/users") + end + + def test_response_for_with_registered_uri + FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt') assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body end + def test_response_for_with_unknown_uri + assert_equal nil, FakeWeb.response_for(:get, 'http://example.com/') + end + + def test_response_for_with_put_method + FakeWeb.register_uri(:put, "http://example.com", :string => "response") + assert_equal 'response', FakeWeb.response_for(:put, "http://example.com").body + end + + def test_response_for_with_any_method_explicitly + FakeWeb.register_uri(:any, "http://example.com", :string => "response") + assert_equal 'response', FakeWeb.response_for(:get, "http://example.com").body + assert_equal 'response', FakeWeb.response_for(:any, "http://example.com").body + end + def test_content_for_registered_uri_with_port_and_request_with_port FakeWeb.register_uri('http://example.com:3000/', :string => 'test example content') Net::HTTP.start('example.com', 3000) do |http| response = http.get('/') assert_equal 'test example content', response.body @@ -124,19 +171,53 @@ response = http.get('/') assert_equal 'port 3000', response.body end end + def test_content_for_registered_uri_with_get_method_only + FakeWeb.allow_net_connect = false + FakeWeb.register_uri(:get, "http://example.com/", :string => "test example content") + Net::HTTP.start('example.com') do |http| + assert_equal 'test example content', http.get('/').body + assert_raises(FakeWeb::NetConnectNotAllowedError) { http.post('/', nil) } + assert_raises(FakeWeb::NetConnectNotAllowedError) { http.put('/', nil) } + assert_raises(FakeWeb::NetConnectNotAllowedError) { http.delete('/', nil) } + end + end + def test_content_for_registered_uri_with_any_method_explicitly + FakeWeb.allow_net_connect = false + FakeWeb.register_uri(:any, "http://example.com/", :string => "test example content") + Net::HTTP.start('example.com') do |http| + assert_equal 'test example content', http.get('/').body + assert_equal 'test example content', http.post('/', nil).body + assert_equal 'test example content', http.put('/', nil).body + assert_equal 'test example content', http.delete('/').body + end + end + + def test_content_for_registered_uri_with_any_method_implicitly + FakeWeb.allow_net_connect = false + FakeWeb.register_uri("http://example.com/", :string => "test example content") + Net::HTTP.start('example.com') do |http| + assert_equal 'test example content', http.get('/').body + assert_equal 'test example content', http.post('/', nil).body + assert_equal 'test example content', http.put('/', nil).body + assert_equal 'test example content', http.delete('/').body + end + end + def test_mock_request_with_block + FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt') Net::HTTP.start('mock') do |http| response = http.get('/test_example.txt') assert_equal 'test example content', response.body end end def test_mock_request_with_undocumented_full_uri_argument_style + FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt') Net::HTTP.start('mock') do |query| response = query.get('http://mock/test_example.txt') assert_equal 'test example content', response.body end end @@ -148,10 +229,11 @@ assert_equal 'test query content', response.body end end def test_mock_post + FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt') response = nil Net::HTTP.start('mock') do |query| response = query.post('/test_example.txt', '') end assert_equal 'test example content', response.body @@ -176,21 +258,21 @@ assert_equal fake_response, response end def test_mock_get_with_request_from_file_as_registered_uri - FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/test_request') + FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding') response = nil Net::HTTP.start('www.google.com') do |query| response = query.get('/') end assert_equal '200', response.code assert response.body.include?('<title>Google</title>') end def test_mock_post_with_request_from_file_as_registered_uri - FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/test_request') + FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding') response = nil Net::HTTP.start('www.google.com') do |query| response = query.post('/', '') end assert_equal "200", response.code @@ -296,10 +378,11 @@ end end end def test_mock_instance_syntax + FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt') response = nil uri = URI.parse('http://mock/test_example.txt') http = Net::HTTP.new(uri.host, uri.port) response = http.start do http.get(uri.path) @@ -310,24 +393,25 @@ def test_mock_via_nil_proxy response = nil proxy_address = nil proxy_port = nil - + FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt') uri = URI.parse('http://mock/test_example.txt') http = Net::HTTP::Proxy(proxy_address, proxy_port).new( uri.host, (uri.port or 80)) response = http.start do http.get(uri.path) end assert_equal 'test example content', response.body end - def test_reponse_type + def test_response_type + FakeWeb.register_uri('http://mock/test_example.txt', :string => "test") Net::HTTP.start('mock') do |http| - response = http.get('/test_example.txt', '') + response = http.get('/test_example.txt') assert_kind_of(Net::HTTPSuccess, response) end end def test_mock_request_that_raises_an_http_error_with_a_specific_status @@ -347,7 +431,45 @@ uri = URI.parse('http://mock/multiple_test_example.txt') 2.times { assert_equal 'test example content', Net::HTTP.get(uri) } 3.times { assert_equal 'thrice', Net::HTTP.get(uri) } 4.times { assert_equal 'ever_more', Net::HTTP.get(uri) } + end + + def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header + FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding') + response = nil + Net::HTTP.start('www.google.com') do |query| + response = query.get('/') + end + assert_not_nil response['transfer-encoding'] + assert response['transfer-encoding'] == 'chunked' + end + + def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header + FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding') + response = nil + Net::HTTP.start('www.google.com') do |query| + response = query.get('/') + end + assert !response.key?('transfer-encoding') + end + + def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header + FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl') + response = nil + Net::HTTP.start('www.google.com') do |query| + response = query.get('/') + end + assert_not_nil response['transfer-encoding'] + assert response['transfer-encoding'] == 'chunked' + end + + def test_txt_file_should_have_three_lines + FakeWeb.register_uri('http://www.google.com/', :file => File.dirname(__FILE__) + '/fixtures/test_txt_file') + response = nil + Net::HTTP.start('www.google.com') do |query| + response = query.get('/') + end + assert response.body.split(/\n/).size == 3, "response has #{response.body.split(/\n/).size} lines should have 3" end end