require File.dirname(__FILE__) + '/../test_helper' require 'fake_web' # To Do: # submitting a page with an empty body seems to confuse the w3c validator -- # sending it into more than one redirect. Seems like and edge case but this # should be handled (or at least tested). class PageModelExtensionsTest < Test::Unit::TestCase fixtures :pages, :page_parts, :snippets, :layouts # Confirms that pages are truly validated using the W3 validator. Since it is # possible that they may change their SOAP output in the future, we will not # try to confirm each message -- just pass or fail. This also confirms that # the extensions properly render the page source to be validated. We take # this a step further by also checking the source produced by @page and # and comparing it with a saved file in 'test/fixtures/' # # NOTE: this test requires network access to the validator to work properly def test_page_validity @page = pages(:valid_markup) validation_data = @page.validate_html_markup assert_equal true, validation_data['completed'] assert_equal true, validation_data['valid'] @page = pages(:invalid_markup) validation_data = @page.validate_html_markup assert_equal true, validation_data['completed'] assert_equal false, validation_data['valid'] end # Confirms that the parsing of SOAP input by mocking requests to the validator # and, instead, returning stored SOAP files stored in 'test/fixtures/' def test_soap_validation_parsing @page = pages(:homepage) # process validator SOAP output for google.com (stored locally in fixtures) FakeWeb.register_uri('http://validator.w3.org/check', :file => File.dirname(__FILE__) + '/../fixtures/google.com.validation.soap12') validation_data = @page.validate_html_markup assert_validation_data(validation_data, { 'completed' => true, 'valid' => false, 'prevalidation_messages_length' => 2, 'messages_length' => 35, 'errorcount' => 30, 'warningcount' => 7, 'doctype' => '', 'charset' => 'utf-8', 'checked_by' => 'http://validator.w3.org/' }, 'google.com validation input' ) FakeWeb.clean_registry FakeWeb.register_uri('http://validator.w3.org/check', :file => File.dirname(__FILE__) + '/../fixtures/rubyonrails.org.validation.soap12') # process validator SOAP output for rubyonrails.org (stored locally in fixtures) validation_data = @page.validate_html_markup assert_validation_data(validation_data, { 'completed' => true, 'valid' => false, 'prevalidation_messages_length' => 0, 'messages_length' => 8, 'errorcount' => 8, 'warningcount' => 0, 'doctype' => 'XHTML 1.0 Transitional', 'charset' => 'utf-8', 'checked_by' => 'http://validator.w3.org/' }, 'rubyonrails.org validation input' ) FakeWeb.clean_registry # process validator SOAP output for api.rubyonrails.org (stored locally in fixtures) FakeWeb.register_uri('http://validator.w3.org/check', :file => File.dirname(__FILE__) + '/../fixtures/yahoo.com.validation.soap12') validation_data = @page.validate_html_markup assert_validation_data(validation_data, { 'completed' => true, 'valid' => false, 'prevalidation_messages_length' => 0, 'messages_length' => 339, 'errorcount' => 281, 'warningcount' => 58, 'doctype' => 'HTML 4.01', # should say 'Strict' 'charset' => 'utf-8', 'checked_by' => 'http://validator.w3.org/' }, 'yahoo.com validation input' ) FakeWeb.clean_registry end # Tests the Net::HTTP response handling. We confirm that it will properly # store the fact that the validation never completed and pass on the HTTP # status codes def test_http_response_errors @page = pages(:homepage) problem_http_statii = [ [100, 'Continue'], [101, 'Switching Protocols'], [400, 'Bad Request'], [401, 'Unauthorized'], [402, 'Payment Required'], [403, 'Forbidden'], [404, 'Not Found'], [405, 'Method Not Allowed'], [406, 'Not Acceptable'], [407, 'Proxy Authentication Required'], [408, 'Request Timeout'], [409, 'Conflict'], [410, 'Gone'], [411, 'Length Required'], [412, 'Precondition Failed'], [413, 'Request Entity Too Large'], [414, 'Request-URI Too Long'], [415, 'Unsupported Media Type'], [416, 'Requested Range Not Satisfiable'], [417, 'Expectation Failed'], [500, 'Internal Server Error'], [501, 'Not Implemented'], [502, 'Bad Gateway'], [503, 'Service Unavailable'], [504, 'Gateway Timeout'], [505, 'HTTP Version Not Supported'], [777, 'Not a Real HTTP Response Code'] ] problem_http_statii.each { |status| FakeWeb.register_uri('http://validator.w3.org/check', :status => status) validation_data = @page.validate_html_markup assert_validation_data(validation_data, { 'completed' => false, 'error_code' => status[0].to_s, 'error_code_description' => status[1] }, 'http bad status (' + status[0].to_s + ')' ) FakeWeb.clean_registry } end # Tests the Net::HTTP response handling. This time, we confirm that it will # follow a single redirect correctly. def test_good_http_redirects @page = pages(:homepage) redirect_http_statii = [ [300, 'Multiple Choices'], [301, 'Moved Permanently'], [302, 'Found'], [303, 'See Other'], [307, 'Temporary Redirect'] ] redirect_http_statii.each { |status| redirect_response = Net::HTTPRedirection.new('1.1', status[0], status[1]) redirect_response['location'] = 'http://validator.w3.org/redirected' FakeWeb.register_uri('http://validator.w3.org/check', :response => redirect_response) FakeWeb.register_uri('http://validator.w3.org/redirected', :file => File.dirname(__FILE__) + '/../fixtures/rubyonrails.org.validation.soap12') validation_data = @page.validate_html_markup assert_validation_data(validation_data, { 'completed' => true, 'valid' => false, 'prevalidation_messages_length' => 0, 'messages_length' => 8, 'errorcount' => 8, 'warningcount' => 0, 'doctype' => 'XHTML 1.0 Transitional', 'charset' => 'utf-8', 'checked_by' => 'http://validator.w3.org/' }, 'good redirect (' + status[0].to_s + ')' ) FakeWeb.clean_registry } end # Tests the Net::HTTP response handling. This time, we confirm that it will # not follow more than one redirect (we just test for 2 redirects here). def test_bad_http_redirects @page = pages(:homepage) redirect_http_statii = [ [300, 'Multiple Choices'], [301, 'Moved Permanently'], [302, 'Found'], [303, 'See Other'], [307, 'Temporary Redirect'] ] redirect_http_statii.each { |status| redirect_response_1 = Net::HTTPRedirection.new('1.1', status[0], status[1]) redirect_response_1['location'] = 'http://validator.w3.org/redirected' FakeWeb.register_uri('http://validator.w3.org/check', :response => redirect_response_1) redirect_response_2 = Net::HTTPRedirection.new('1.1', status[0], status[1]) redirect_response_2['location'] = 'http://validator.w3.org/redirected/again' FakeWeb.register_uri('http://validator.w3.org/redirected', :response => redirect_response_2) FakeWeb.register_uri('http://validator.w3.org/redirected/again', :file => File.dirname(__FILE__) + '/../fixtures/rubyonrails.org.validation.soap12') validation_data = @page.validate_html_markup assert_validation_data(validation_data, { 'completed' => false, 'error_code' => status[0], 'error_code_description' => status[1] + ' (redirect: ' + redirect_response_2['location'] + ')' }, 'bad redirect (' + status[0].to_s + ')' ) FakeWeb.clean_registry } end end