require File.join(File.dirname(__FILE__), "test_helper") class TestFakeWeb < Test::Unit::TestCase def test_register_uri FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "example") assert FakeWeb.registered_uri?(:get, '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", :body => "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(:get, 'test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt') end end def test_register_uri_with_port_and_check_with_port FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo') assert FakeWeb.registered_uri?(:get, 'http://example.com:3000/') end def test_register_uri_with_port_and_check_without_port FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo') assert !FakeWeb.registered_uri?(:get, 'http://example.com/') end def test_register_uri_with_default_port_for_http_and_check_without_port FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'foo') assert FakeWeb.registered_uri?(:get, 'http://example.com/') end def test_register_uri_with_default_port_for_https_and_check_without_port FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'foo') assert FakeWeb.registered_uri?(:get, 'https://example.com/') end def test_register_uri_with_no_port_for_http_and_check_with_default_port FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo') assert FakeWeb.registered_uri?(:get, 'http://example.com:80/') end def test_register_uri_with_no_port_for_https_and_check_with_default_port FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo') assert FakeWeb.registered_uri?(:get, 'https://example.com:443/') end def test_register_uri_with_no_port_for_https_and_check_with_443_on_http FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo') assert !FakeWeb.registered_uri?(:get, 'http://example.com:443/') end def test_register_uri_with_no_port_for_http_and_check_with_80_on_https FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo') assert !FakeWeb.registered_uri?(:get, 'https://example.com:80/') end def test_register_uri_for_any_method_explicitly FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :body => "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") capture_stderr do # silence deprecation warning assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint") end end def test_register_uri_for_get_method_only FakeWeb.register_uri(:get, "http://example.com/users", :body => "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") capture_stderr do # silence deprecation warning assert !FakeWeb.registered_uri?("http://example.com/users") end end def test_response_for_with_registered_uri FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt') assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body end def test_response_for_with_unknown_uri assert_nil FakeWeb.response_for(:get, 'http://example.com/') end def test_response_for_with_put_method FakeWeb.register_uri(:put, "http://example.com", :body => "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", :body => "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(:get, 'http://example.com:3000/', :body => 'test example content') response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') } assert_equal 'test example content', response.body end def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'test example content') response = Net::HTTP.start('example.com') { |http| http.get('/') } assert_equal 'test example content', response.body end def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port FakeWeb.register_uri(:get, 'http://example.com/', :body => 'test example content') response = Net::HTTP.start('example.com', 80) { |http| http.get('/') } assert_equal 'test example content', response.body end def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'test example content') http = Net::HTTP.new('example.com', 443) http.use_ssl = true response = http.get('/') assert_equal 'test example content', response.body end def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port FakeWeb.register_uri(:get, 'https://example.com/', :body => 'test example content') http = Net::HTTP.new('example.com', 443) http.use_ssl = true response = http.get('/') assert_equal 'test example content', response.body end def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000') FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80') response = Net::HTTP.start('example.com') { |http| http.get('/') } assert_equal 'port 80', response.body end def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000') FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80') response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') } assert_equal 'port 3000', response.body end def test_content_for_registered_uri_with_get_method_only FakeWeb.allow_net_connect = false FakeWeb.register_uri(:get, "http://example.com/", :body => "test example content") http = Net::HTTP.new('example.com') 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('/') } end def test_content_for_registered_uri_with_any_method_explicitly FakeWeb.allow_net_connect = false FakeWeb.register_uri(:any, "http://example.com/", :body => "test example content") http = Net::HTTP.new('example.com') 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 def test_content_for_registered_uri_with_any_method_implicitly FakeWeb.allow_net_connect = false capture_stderr do # silence deprecation warning FakeWeb.register_uri("http://example.com/", :body => "test example content") end http = Net::HTTP.new('example.com') 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 def test_mock_request_with_block FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt') response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') } assert_equal 'test example content', response.body end def test_request_with_registered_body_yields_the_response_body_to_a_request_block FakeWeb.register_uri(:get, "http://example.com", :body => "content") body = nil Net::HTTP.start("example.com") do |http| http.get("/") do |response_body| body = response_body end end assert_equal "content", body end def test_request_with_registered_response_yields_the_response_body_to_a_request_block fake_response = Net::HTTPOK.new('1.1', '200', 'OK') fake_response.instance_variable_set(:@body, "content") FakeWeb.register_uri(:get, 'http://example.com', :response => fake_response) body = nil Net::HTTP.start("example.com") do |http| http.get("/") do |response_body| body = response_body end end assert_equal "content", body end def test_mock_request_with_undocumented_full_uri_argument_style FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt') response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt') } assert_equal 'test example content', response.body end def test_mock_request_with_undocumented_full_uri_argument_style_and_query FakeWeb.register_uri(:get, 'http://mock/test_example.txt?a=b', :body => 'test query content') response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt?a=b') } assert_equal 'test query content', response.body end def test_mock_post FakeWeb.register_uri(:post, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt') response = Net::HTTP.start('mock') { |query| query.post('/test_example.txt', '') } assert_equal 'test example content', response.body end def test_mock_post_with_string_as_registered_uri FakeWeb.register_uri(:post, 'http://mock/test_string.txt', :body => 'foo') response = Net::HTTP.start('mock') { |query| query.post('/test_string.txt', '') } assert_equal 'foo', response.body end def test_mock_get_with_request_as_registered_uri fake_response = Net::HTTPOK.new('1.1', '200', 'OK') FakeWeb.register_uri(:get, 'http://mock/test_response', :response => fake_response) response = Net::HTTP.start('mock') { |query| query.get('/test_response') } assert_equal fake_response, response end def test_mock_get_with_request_from_file_as_registered_uri FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding') response = Net::HTTP.start('www.google.com') { |query| query.get('/') } assert_equal '200', response.code assert response.body.include?('