def test_ssl(self): parsed = parse_url('www.example.com') self.assertFalse(parsed.ssl) parsed = parse_url('http://www.example.com') self.assertFalse(parsed.ssl) parsed = parse_url('https://www.example.com') self.assertTrue(parsed.ssl)
def test_ssl(self): parsed = parse_url('www.example.com') self.assertFalse(parsed.ssl) parsed = parse_url('http://www.example.com') self.assertFalse(parsed.ssl) parsed = parse_url('https://www.example.com') self.assertTrue(parsed.ssl)
def test_target(self): parsed = parse_url('www.example.com') self.assertEqual(parsed.target, '/') parsed = parse_url('www.example.com/foo') self.assertEqual(parsed.target, '/foo') parsed = parse_url('www.example.com?bar') self.assertEqual(parsed.target, '/?bar') parsed = parse_url('www.example.com/foo?bar') self.assertEqual(parsed.target, '/foo?bar')
def test_target(self): parsed = parse_url('www.example.com') self.assertEqual(parsed.target, '/') parsed = parse_url('www.example.com/foo') self.assertEqual(parsed.target, '/foo') parsed = parse_url('www.example.com?bar') self.assertEqual(parsed.target, '/?bar') parsed = parse_url('www.example.com/foo?bar') self.assertEqual(parsed.target, '/foo?bar')
def test_default_scheme(self): parsed = parse_url('www.example.com') self.assertEqual(parsed, URL('http', 'www.example.com')) parsed = parse_url('http://www.example.com') self.assertEqual(parsed, URL('http', 'www.example.com')) parsed = parse_url('www.example.com', default_scheme='https') self.assertEqual(parsed, URL('https', 'www.example.com')) parsed = parse_url('https://www.example.com', default_scheme='https') self.assertEqual(parsed, URL('https', 'www.example.com'))
def test_default_scheme(self): parsed = parse_url('www.example.com') self.assertEqual(parsed, URL('http', 'www.example.com')) parsed = parse_url('http://www.example.com') self.assertEqual(parsed, URL('http', 'www.example.com')) parsed = parse_url('www.example.com', default_scheme='https') self.assertEqual(parsed, URL('https', 'www.example.com')) parsed = parse_url('https://www.example.com', default_scheme='https') self.assertEqual(parsed, URL('https', 'www.example.com'))
def test_addr(self): parsed = parse_url('www.example.com') self.assertEqual(parsed.addr, ('www.example.com', 80)) parsed = parse_url('https://www.example.com') self.assertEqual(parsed.addr, ('www.example.com', 443))
def test_userinfo_port(self): parsed = parse_url('http://*****:*****@example.com:80') self.assertEqual( parsed, URL('http', 'example.com', port='80', userinfo='user:pass'))
def test_port(self): parsed = parse_url('http://example.com:80') self.assertEqual(parsed, URL('http', 'example.com', port='80'))
def test_asterisk(self): parsed = parse_url('*') self.assertEqual(parsed, URL(path='*'))
def test_authority(self): parsed = parse_url('example.com:80', is_connect=True) self.assertEqual(parsed, URL('', 'example.com', port='80'))
def test_port(self): parsed = parse_url('http://example.com:80') self.assertEqual(parsed, URL('http', 'example.com', port='80'))
def test_origin(self): parsed = parse_url('/path') self.assertEqual(parsed, URL(path='/path'))
def test_origin(self): parsed = parse_url('/path') self.assertEqual(parsed, URL(path='/path'))
def test_authority(self): parsed = parse_url('example.com:80', is_connect=True) self.assertEqual(parsed, URL('', 'example.com', port='80'))
def test_addr(self): parsed = parse_url('www.example.com') self.assertEqual(parsed.addr, ('www.example.com', 80)) parsed = parse_url('https://www.example.com') self.assertEqual(parsed.addr, ('www.example.com', 443))
def test_userinfo_port(self): parsed = parse_url('http://*****:*****@example.com:80') self.assertEqual(parsed, URL('http', 'example.com', port='80', userinfo='user:pass'))
def test_absolute(self): parsed = parse_url('http://example.com/path') self.assertEqual(parsed, URL('http', 'example.com', '/path'))
def test_absolute(self): parsed = parse_url('http://example.com/path') self.assertEqual(parsed, URL('http', 'example.com', '/path'))
# Gruvi example program: a cURL like URL downloader import sys import argparse from gruvi.http import HttpClient, parse_url parser = argparse.ArgumentParser() parser.add_argument('url') args = parser.parse_args() url = parse_url(args.url) client = HttpClient() client.connect(url.addr, ssl=url.ssl) client.request('GET', url.target) resp = client.getresponse() if not 200 <= resp.status_code <= 399: sys.stderr.write('Error: got status {}\n'.format(resp.status_code)) sys.exit(1) charset = resp.charset or 'iso8859-1' while True: buf = resp.body.read(4096) if not buf: break sys.stdout.write(buf.decode(charset))
def test_asterisk(self): parsed = parse_url('*') self.assertEqual(parsed, URL(path='*'))