def test_external_basic_auth_handler1(self): """test external basic auth handler""" handler = self._setup_ext_basic_auth_handler('http://localhost', 'foo', 'bar') r = Urllib2HTTPRequest('http://localhost', handlers=[handler]) resp = r.get('/test') self.assertEqual(resp.read(), 'foo')
def test4(self): """simple get with response validation (validation fails)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) self.assertRaises(etree.DocumentInvalid, r.get, '/source', schema=self.fixture_file('directory.xsd'))
def test19(self): """test content type (PUT)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.put('/source/prj/_meta', data='<xml/>', content_type='application/xml') self.assertEqual(resp.read(), 'foobar')
def test_basic_auth_handler6(self): """send credentials, if the redirect location's host did not change""" r = Urllib2HTTPRequest('http://localhost', username='******', password='******') resp = r.get('/test') self.assertEqual(resp.read(), 'foo')
def test_basic_auth_handler7(self): """test a https request""" r = Urllib2HTTPRequest('https://localhost', username='******', password='******') resp = r.get('/test') self.assertEqual(resp.read(), 'foo')
def test_basic_auth_handler5(self): """do not send credentials to an arbitrary redirect location""" r = Urllib2HTTPRequest('http://localhost', username='******', password='******') resp = r.get('/test') self.assertEqual(resp.read(), 'foo')
def test_basic_auth_handler4(self): """do not send credentials to an arbitrary host""" r = Urllib2HTTPRequest('http://localhost', username='******', password='******') resp = r.get('/source', apiurl='http://api.opensuse.org') self.assertEqual(resp.read(), 'foo')
def test_basic_auth_handler1(self): """test the default basic auth handler""" r = Urllib2HTTPRequest('http://localhost', username='******', password='******') resp = r.get('/test') self.assertEqual(resp.read(), 'foo')
def test11(self): """test response object""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.get('source') self.assertEqual(resp.url, 'http://localhost/source') self.assertEqual(resp.code, 200) self.assertEqual(resp.headers.get('header1'), 'foo') self.assertEqual(resp.headers['x'], '42')
def test_basic_auth_handler3(self): """always send credentials (even if a cookie is used)""" r = Urllib2HTTPRequest('http://api.opensuse.org', username='******', password='******', cookie_filename=self.fixture_file('cookie')) resp = r.get('/source') self.assertEqual(resp.read(), 'foo')
def test14(self): """test exception handling (check exception object)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) with self.assertRaises(HTTPError) as cm: r.get('/source') self.assertEqual(cm.exception.url, 'http://localhost/source') self.assertEqual(cm.exception.code, 403) self.assertEqual(cm.exception.headers['foo'], 'bar') self.assertIsNotNone(cm.exception.orig_exc)
def test21(self): """test content type and urlencoded (POST)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) self.assertRaises(ValueError, r.post, '/source', data='bar', content_type='foo', urlencoded=True)
def test6(self): """simple put (filename, uses mmap)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False, True, 20) resp = r.put('/source/foo/bar/file', filename=self.fixture_file('putfile'), x='foo bar', foo='bar') self.assertEqual(resp.read(), 'ok') self.assertIsNone(resp._sio)
def test23(self): """test ignore empty query keys""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.get('/test', binary=['', 'foo'], test='4', x='', y=None, z=[''], a=['', None]) self.assertEqual(resp.read(), 'foo')
def test9(self): """simple post (filename, uses mmap) - validation fails""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False, True, 20) self.assertRaises(etree.DocumentInvalid, r.post, '/source/foo/bar/file', filename=self.fixture_file('putfile'), schema=self.fixture_file('directory.xsd'), x='foo bar', foo='bar')
def test8(self): """simple post (filename, uses mmap) - validate response""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False, True, 20) resp = r.post('/source/foo/bar/file', filename=self.fixture_file('putfile'), schema=self.fixture_file('directory.xsd'), x='foo bar', foo='bar') self.assertEqual(resp.read(), self.read_file('prj_list.xml')) self.assertIsNotNone(resp._sio)
def test_external_basic_auth_handler2(self): """test external + default basic auth handler (pathological case)""" # the default basic auth handler does not override an existing # Authorization header handler = self._setup_ext_basic_auth_handler('http://localhost', 'foo', 'foobar') r = Urllib2HTTPRequest('http://localhost', username='******', password='******', handlers=[handler]) resp = r.get('/test') self.assertEqual(resp.read(), 'foo')
def __init__(self, apiurl, username='', password='', request_object=None, debug=False, validate=True): super(Osc, self).__init__() if username and request_object is not None: raise ValueError('either specify username or request_object') self.request_object = request_object if request_object is None: self.request_object = Urllib2HTTPRequest(apiurl, username=username, password=password, validate=validate, debug=debug) Osc._osc = self
def test16(self): """test optional apiurl (DELETE)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.delete('/source', apiurl='http://api') self.assertEqual(resp.read(), 'foobar')
def test10(self): """simple delete""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.delete('/source/project') self.assertEqual(resp.read(), 'foobar') self.assertIsNone(resp._sio)
def test7(self): """simple post (urlencoded data)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.post('/dummy', data='simple text', urlencoded=True) self.assertEqual(resp.read(), 'ok') self.assertIsNone(resp._sio)
def test12(self): """test exception handling (get)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) self.assertRaises(HTTPError, r.get, 'source')
def test5(self): """simple put""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.put('/source/foo/bar/file', data='this is a test') self.assertEqual(resp.read(), 'ok') self.assertIsNone(resp._sio)
def test2(self): """simple get with query""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.get('/source/server:mail', foo='bar', esc='foo&bar') self.assertEqual(resp.read(), 'foobar') self.assertIsNone(resp._sio)
def test3(self): """simple get with response validation""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.get('/source', schema=self.fixture_file('directory.xsd')) self.assertEqual(resp.read(), self.read_file('prj_list.xml')) self.assertIsNotNone(resp._sio)
def test13(self): """test exception handling (put)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) self.assertRaises(HTTPError, r.put, 'source', data='foo bar')
def test22(self): """test use same query parameter more than once""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.get('/test', binary=['foo', 'bar', 'foobar'], other='ok') self.assertEqual(resp.read(), 'foobar')
def test_1(self): """simple get""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.get('/source') self.assertEqual(resp.read(), 'foobar') self.assertIsNone(resp._sio)
def test18(self): """test optional apiurl (PUT)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.put('/source', data='foo', apiurl='http://url') self.assertEqual(resp.read(), 'foobar')
def test20(self): """test content type (POST)""" r = Urllib2HTTPRequest('http://localhost', True, '', '', '', False) resp = r.put('/source', data='asdf', content_type='foo') self.assertEqual(resp.read(), 'foobar')