def test_simple_param(self): expected = 'http://blank/resource/41/nested-item' str_url = 'http://blank/resource/:id/:resource' url = Purl(str_url).param({ 'resource': 'nested-item' }).param('id', 41) assert str(url) == expected expected = 'http://blank/resource/hello/nested-item' url.param('id', 'hello') assert str(url) == expected
def test_simple_url(self): str_url = 'http://blank' url = Purl(str_url) assert str(url) == str_url str_url = 'https://blank' url = Purl(str_url) assert str(url) == str_url str_url = 'http://blank.com' url = Purl(str_url) assert str(url) == str_url
def test_invalid_url(self): with pytest.raises(InvalidUrlError): Purl('bad') with pytest.raises(InvalidUrlError): Purl('bad.com/abc/def') with pytest.raises(InvalidUrlError): Purl('http://bad:xwy/one/2/three') with pytest.raises(InvalidUrlError): Purl('http://bad://?hello')
def test_param_multi(self): expected = 'http://www.site.edu:123/web52/download/new?msg=world' str_url = 'http://www.site.edu:123/:basepath/:action/:misc?msg=hello&there=true' url = Purl(str_url).param({ 'basepath': 'web52', 'action': 'download', 'misc': 'new' }).add_query('msg', 'world').delete_query('there') assert str(url) == expected expected = 'http://www.site.edu:123/web52/stream/p?msg=false' assert str(url.add_query('msg', False) .param('action', 'stream') .param('misc','p')) == expected
def test_create_with(self): expected = Purl('file://blank') url = Purl.create_with({'protocol': 'file://', 'hostname': 'blank'}) assert (str(url) == str(expected)) expected = Purl('https://blank:321/some/path') url = Purl.create_with({ 'protocol': 'https://', 'hostname': 'blank', 'port': ':321', 'path': '/some/path' }) assert (str(url) == str(expected)) expected = Purl('https://blank:321/some/path?a=1&b=2&c=false') url = Purl.create_with({ 'protocol': 'https://', 'hostname': 'blank', 'port': ':321', 'path': '/some/path?a=1&b=2&c=false' }) assert (str(url) == str(expected)) expected = Purl('http://somehost/some/path?a=1&b=2&c=false') url = Purl.create_with({ 'protocol': 'http://', 'hostname': 'somehost', 'path': '/some/path?a=1&b=2&c=false' }) assert (str(url) == str(expected))
def _getpidclass(type, baseval): baseval = baseval.strip().replace('\n', '') if type.strip().lower() == "doi": return Doi(baseval) if type.strip().lower() == "urn" or type.strip().lower( ) == "urn:nbn" or type.strip().lower() == "urn:nbn:nl" or type.strip( ).lower() == "urnnbn" or type.strip().lower() == "nbn": return Nbn(baseval) if type.strip().lower() == "arxiv": return ArXiv(baseval) if type.strip().lower() == "href" or type.strip().lower( ) == "http" or type.strip().lower() == "url" or type.strip().lower( ) == "purl": # :mods:relatedItem/@xlink:href wordt met type 'url' opgezet... if ("doi.org/" in baseval.lower()): return Doi(baseval) return Purl(baseval) if type.strip().lower() == "issn": return Issn(baseval) if type.strip().lower() == "isbn": return Isbn(baseval) if type.strip().lower() == "pmid" or type.strip().lower() == "pubmed": return Pmid(baseval) if type.strip().lower() == "pure" or "pure" in type.lower(): return Pure(baseval) if type.strip().lower() == "wos": return Wos(baseval) if type.strip().lower() == "scopus": return Scopus(baseval) if type.strip().lower() == "uri": if ("issn" in baseval.lower()): return Issn(baseval) if ("isbn" in baseval.lower()): return Isbn(baseval) if ("doi" in baseval.lower()): return Doi(baseval) if ( "/handle/" in baseval.lower() or "hdl/" in baseval.lower() or "handle.net" in baseval.lower() ): #handle may resolve locally, prefixed with local url-resolver, so return url type if given so. return Handle(baseval) if ("urn:nbn:nl" in baseval.lower()): return Nbn(baseval) if ("http" in baseval.lower() or "ftp" in baseval.lower()): return Purl(baseval) if type.strip().lower() == "handle" or type.strip().lower( ) == "handle.net" or type.strip().lower() == "hdl": return Handle(baseval) return Unknown(type.strip().replace('\n', ''), baseval)
def test_url_mirrors_valid_inputs(self): str_url = 'http://blank:1234' url = Purl(str_url) assert str(url) == str_url str_url = 'file://blank/path/to/file' url = Purl(str_url) assert str(url) == str_url str_url = 'https://blank.com/resource/1' url = Purl(str_url) assert str(url) == str_url str_url = 'http://blank.org:1234/resouce/1/other' url = Purl(str_url) assert str(url) == str_url str_url = 'file://blank.org:1234/file.txt' url = Purl(str_url) assert str(url) == str_url
def test_create_with_invalid_args(self): with pytest.raises(InvalidUrlError): Purl.create_with({}) with pytest.raises(InvalidUrlError): Purl.create_with({'hostname': 'dne'}) with pytest.raises(InvalidUrlError): Purl.create_with({'protocol': 'ssh', 'hostname': 'bad'}) with pytest.raises(InvalidUrlError): Purl.create_with({ 'protocol': 'ssh://', 'hostname': 'bad', 'port': ':abc' }) with pytest.raises(InvalidUrlError): Purl.create_with({ 'protocol': 'ssh://', 'hostname': 'bad', 'port': '321' })
def test_param_empty(self): expected = 'https://site.edu/resource/72/five?ok=true' str_url = 'https://site.edu/resource/72/five' url = Purl(str_url).param('dne', 0).add_query('ok', True) assert str(url) == expected
def test_simple_query(self): url = Purl('http://blank') url.add_query('c', 1) assert str(url) == 'http://blank?c=1' assert str(url.add_query('a', 'one')) == 'http://blank?a=one&c=1' assert str(url) == 'http://blank?a=one&c=1'
def test_delete_query(self): url = Purl('http://blank.com') url.delete_query('bat'); assert str(url) == 'http://blank.com' url = Purl('http://blank.com?') url.delete_query('bat'); assert str(url) == 'http://blank.com' url = Purl('http://blank.com?') url.add_query('bat', 'man').delete_query('bat'); assert str(url) == 'http://blank.com' url = Purl('http://blank.com?bat=man') url.delete_query('bat'); assert str(url) == 'http://blank.com' url = Purl('http://blank.com?man=nam&bat=man') assert str(url.delete_query('man')) == 'http://blank.com?bat=man'; assert str(url) == 'http://blank.com?bat=man' url = Purl('http://blank.com?man=nam&bat=man') assert str( url.delete_query('man') .delete_query('man') .delete_query('bat') ) == 'http://blank.com'; assert str(url) == 'http://blank.com' url = Purl('http://blank.com?man=nam&bat=man') assert str(url.delete_query(['man', 'bat'])) == 'http://blank.com'; assert str(url) == 'http://blank.com'
def test_query_multiple(self): url = Purl('https://hello.org') url.add_query({'c': 1}) assert str(url) == 'https://hello.org?c=1' url = Purl('https://hello.org') url.add_query({'a': 'one', 'c': 45}) assert str(url) == 'https://hello.org?a=one&c=45' url = Purl('https://hello.org?') url.add_query({'a': 'one', 'c': 45}) assert str(url) == 'https://hello.org?a=one&c=45' url = Purl('https://hello.org?d=dee') url.add_query({'a': 'one', 'c': 45}) assert str(url) == 'https://hello.org?a=one&c=45&d=dee' url = Purl('https://hello.org?a=aye&d=dee') url.add_query({'a': 'one', 'c': 45}) assert str(url) == 'https://hello.org?a=one&c=45&d=dee' url = Purl('https://hello.org?g=gamma&d=dee') url.add_query({'a': 'one', 'c': 45}) assert str(url) == 'https://hello.org?a=one&c=45&d=dee&g=gamma' url = Purl('https://hello.org?g=gamma&d=dee') url.add_query({'a': False, 'c': True}) assert str(url) == 'https://hello.org?a=false&c=true&d=dee&g=gamma'
def test_invalid_fields(self): u = Purl('http://blank') with pytest.raises(InvalidUrlError): u.protocol('') with pytest.raises(InvalidUrlError): u.protocol('://') with pytest.raises(InvalidUrlError): u.protocol('abc') with pytest.raises(InvalidUrlError): u.hostname('') with pytest.raises(InvalidUrlError): u.hostname('abcd/123/546') with pytest.raises(InvalidUrlError): u.hostname('abcd:123') with pytest.raises(InvalidUrlError): u.hostname('abcd:abcd') with pytest.raises(InvalidUrlError): u.port('80') with pytest.raises(InvalidUrlError): u.port(':abcd') with pytest.raises(InvalidUrlError): u.path('abcd/123') with pytest.raises(InvalidUrlError): u.path('abcd/123/')
def test_fields(self): url = Purl('sftp://secure-site:123') assert url.protocol() == 'sftp://' assert url.hostname() == 'secure-site' assert url.port() == ':123' assert url.path() == None url = Purl('http://nada.com') assert url.protocol() == 'http://' assert url.hostname() == 'nada.com' assert url.port() == None assert url.path() == None url = Purl('file://filesys/somefile.png') assert url.protocol() == 'file://' assert url.hostname() == 'filesys' assert url.port() == None assert url.path() == '/somefile.png' expected = 'https://firehouse.com:3333/freedom' url = Purl('http://blank') assert str( url.protocol('https://').hostname('firehouse.com').path( '/freedom').port(':3333')) == expected assert str(url) == expected