def test_cache_http_errors(self): settings = opener_settings.OpenerSettings() settings.build_openers() opener = settings.get_custom_opener() url = URL('http://w3af.org/foo-bar-not-exists.htm') request = HTTPRequest(url, cache=False) with patch('w3af.core.data.url.handlers.cache.CacheClass') as cc_mock: store_in_cache = Mock() cc_mock.attach_mock(store_in_cache, 'store_in_cache') # If there is a response we should store it, even if it is a 404 try: response = opener.open(request) except urllib2.HTTPError: pass # Make sure the right call was made _call = _Call(('store_in_cache', (request, response))) self.assertEqual(cc_mock.mock_calls, [_call]) cc_mock.reset_mock() # And make sure the response was a 404 self.assertEqual(response.status, 404)
def test_handler_integration(self): """ Integration test with w3af's URL opener. """ test_param = 'test_handler_integration' settings = opener_settings.OpenerSettings() settings.set_url_parameter(test_param) settings.build_openers() opener = settings.get_custom_opener() for proto in {'http', 'https'}: test_url = URL('%s://mock/abc/def.html' % proto) test_url_param = URL('%s://mock/abc/def.html;%s' % (proto, test_param)) request = HTTPRequest(test_url) httpretty.register_uri(httpretty.GET, test_url.url_string, body='FAIL') httpretty.register_uri(httpretty.GET, test_url_param.url_string, body='SUCCESS') response = opener.open(request) self.assertIn('SUCCESS', response.read())
def get_http_settings(self): """ Get the http settings options. :return: The http settings in an OptionList """ import w3af.core.data.url.opener_settings as opener_settings url_settings = opener_settings.OpenerSettings() return self._get_x_settings('http-settings', url_settings)
def test_handler_order(self): """Get an instance of the extended urllib and verify that the redirect handler still works, even when mixed with all the other handlers.""" # Configure the handler redirect_url = URL(get_moth_http('/audit/global_redirect/redirect-header-302.py?url=/')) settings = opener_settings.OpenerSettings() settings.build_openers() opener = settings.get_custom_opener() request = HTTPRequest(redirect_url) response = opener.open(request) self.assertEqual(response.code, FOUND) self.assertEqual(response.id, 1)
def test_error_handler_id(self): """ Verify that the error handler works as expected, in other words, do NOT crash on response codes not in range 200-300. """ fail_url = URL(get_moth_http('/abc/def/do-not-exist.foo')) settings = opener_settings.OpenerSettings() settings.build_openers() opener = settings.get_custom_opener() request = HTTPRequest(fail_url) try: opener.open(request) except urllib2.HTTPError, response: self.assertEqual(response.code, NOT_FOUND) self.assertEqual(response.id, 1)
def test_handler_order_block(self): """Get an instance of the extended urllib and verify that the blacklist handler still works, even when mixed with all the other handlers.""" # Configure the handler blocked_url = URL(get_moth_http('/abc/def/')) cf.cf.save('non_targets', [blocked_url,]) settings = opener_settings.OpenerSettings() settings.build_openers() opener = settings.get_custom_opener() request = HTTPRequest(blocked_url) request.url_object = blocked_url request.cookies = True request.get_from_cache = False response = opener.open(request) self.assertEqual(response.code, NO_CONTENT) self.assertEqual(response.id, 1)
def test_handler_order_block(self): httpretty.register_uri(httpretty.GET, self.MOCK_URL, body=self.MOCK_BODY, status=200) blocked_url = URL(self.MOCK_URL) cf.cf.save('blacklist_http_request', [blocked_url]) # Get an instance of the extended urllib and verify that the blacklist # handler still works, even when mixed with all the other handlers. settings = opener_settings.OpenerSettings() settings.build_openers() opener = settings.get_custom_opener() request = HTTPRequest(blocked_url) response = opener.open(request) self.assertEqual(response.code, NO_CONTENT) self.assertEqual(response.id, 1) self.assertIsInstance(httpretty.last_request(), httpretty.core.HTTPrettyRequestEmpty)
def test_handler_order_pass_with_both_methods(self): httpretty.register_uri(httpretty.GET, self.MOCK_URL_BLOCK, body=self.MOCK_BODY, status=200) httpretty.register_uri(httpretty.GET, self.MOCK_URL_PASS, body=self.MOCK_BODY, status=200) blocked_url = URL(self.MOCK_URL_BLOCK) safe_url = URL(self.MOCK_URL_PASS) cf.cf.save('blacklist_http_request', [blocked_url]) ignore_regex = re.compile('.*blo.*') cf.cf.save('ignore_regex', ignore_regex) # Get an instance of the extended urllib and verify that the blacklist # handler still works, even when mixed with all the other handlers. settings = opener_settings.OpenerSettings() settings.build_openers() opener = settings.get_custom_opener() request = HTTPRequest(safe_url) response = opener.open(request) last_request = httpretty.last_request() self.assertEqual(response.code, 200) self.assertEqual(response.id, 1) request = HTTPRequest(blocked_url) response = opener.open(request) self.assertEqual(response.code, 204) self.assertEqual(response.id, 2) self.assertIs(last_request, httpretty.last_request())