Changeset View
Changeset View
Standalone View
Standalone View
src/parser/__tests__/PhutilURITestCase.php
Show All 10 Lines | public function testURIParsing() { | ||||
$this->assertEqual('user', $uri->getUser(), pht('user')); | $this->assertEqual('user', $uri->getUser(), pht('user')); | ||||
$this->assertEqual('pass', $uri->getPass(), pht('password')); | $this->assertEqual('pass', $uri->getPass(), pht('password')); | ||||
$this->assertEqual('host', $uri->getDomain(), pht('domain')); | $this->assertEqual('host', $uri->getDomain(), pht('domain')); | ||||
$this->assertEqual('99', $uri->getPort(), pht('port')); | $this->assertEqual('99', $uri->getPort(), pht('port')); | ||||
$this->assertEqual('/path/', $uri->getPath(), pht('path')); | $this->assertEqual('/path/', $uri->getPath(), pht('path')); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
array( | array( | ||||
'query' => 'value', | array( | ||||
'query', | |||||
'value', | |||||
), | |||||
), | ), | ||||
$uri->getQueryParams(), | $uri->getQueryParamsAsPairList(), | ||||
'query params'); | 'query params'); | ||||
$this->assertEqual('fragment', $uri->getFragment(), pht('fragment')); | $this->assertEqual('fragment', $uri->getFragment(), pht('fragment')); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://user:pass@host:99/path/?query=value#fragment', | 'http://user:pass@host:99/path/?query=value#fragment', | ||||
(string)$uri, | (string)$uri, | ||||
'uri'); | 'uri'); | ||||
$uri = new PhutilURI('ssh://git@example.com/example/example.git'); | $uri = new PhutilURI('ssh://git@example.com/example/example.git'); | ||||
$this->assertEqual('ssh', $uri->getProtocol(), pht('protocol')); | $this->assertEqual('ssh', $uri->getProtocol(), pht('protocol')); | ||||
$this->assertEqual('git', $uri->getUser(), pht('user')); | $this->assertEqual('git', $uri->getUser(), pht('user')); | ||||
$this->assertEqual('', $uri->getPass(), pht('password')); | $this->assertEqual('', $uri->getPass(), pht('password')); | ||||
$this->assertEqual('example.com', $uri->getDomain(), pht('domain')); | $this->assertEqual('example.com', $uri->getDomain(), pht('domain')); | ||||
$this->assertEqual('', $uri->getPort(), 'port'); | $this->assertEqual('', $uri->getPort(), 'port'); | ||||
$this->assertEqual('/example/example.git', $uri->getPath(), pht('path')); | $this->assertEqual('/example/example.git', $uri->getPath(), pht('path')); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
array(), | array(), | ||||
$uri->getQueryParams(), | $uri->getQueryParamsAsPairList(), | ||||
pht('query parameters')); | pht('query parameters')); | ||||
$this->assertEqual('', $uri->getFragment(), pht('fragment')); | $this->assertEqual('', $uri->getFragment(), pht('fragment')); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'ssh://git@example.com/example/example.git', | 'ssh://git@example.com/example/example.git', | ||||
(string)$uri, | (string)$uri, | ||||
'uri'); | 'uri'); | ||||
▲ Show 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | public function testUnusualURIs() { | ||||
$this->assertEqual('/path/to/file', $uri->getPath(), pht('path')); | $this->assertEqual('/path/to/file', $uri->getPath(), pht('path')); | ||||
$uri = new PhutilURI('idea://open?x=/'); | $uri = new PhutilURI('idea://open?x=/'); | ||||
$this->assertEqual('idea', $uri->getProtocol(), pht('protocol')); | $this->assertEqual('idea', $uri->getProtocol(), pht('protocol')); | ||||
$this->assertEqual('open', $uri->getDomain(), pht('domain')); | $this->assertEqual('open', $uri->getDomain(), pht('domain')); | ||||
$this->assertEqual('', $uri->getPath(), pht('path')); | $this->assertEqual('', $uri->getPath(), pht('path')); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
array( | array( | ||||
'x' => '/', | array( | ||||
'x', | |||||
'/', | |||||
), | |||||
), | ), | ||||
$uri->getQueryParams()); | $uri->getQueryParamsAsPairList()); | ||||
// This is not a legitimate URI and should not parse as one. | // This is not a legitimate URI and should not parse as one. | ||||
$uri = new PhutilURI('fruit.list: apple banana cherry'); | $uri = new PhutilURI('fruit.list: apple banana cherry'); | ||||
$this->assertEqual('', $uri->getDomain()); | $this->assertEqual('', $uri->getDomain()); | ||||
} | } | ||||
public function testAmbiguousURIs() { | public function testAmbiguousURIs() { | ||||
// It's important that this be detected as a Javascript URI, because that | // It's important that this be detected as a Javascript URI, because that | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | try { | ||||
$uri->setType(PhutilURI::TYPE_URI); | $uri->setType(PhutilURI::TYPE_URI); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
$caught = $ex; | $caught = $ex; | ||||
} | } | ||||
$this->assertTrue($caught instanceof Exception); | $this->assertTrue($caught instanceof Exception); | ||||
} | } | ||||
public function testDuplicateKeys() { | |||||
$uri = new PhutilURI('http://www.example.com/?x=1&x=2'); | |||||
$this->assertEqual( | |||||
'http://www.example.com/?x=1&x=2', | |||||
(string)$uri); | |||||
$uri->appendQueryParam('x', '3'); | |||||
$this->assertEqual( | |||||
'http://www.example.com/?x=1&x=2&x=3', | |||||
(string)$uri); | |||||
$uri->setQueryParam('x', '4'); | |||||
$this->assertEqual( | |||||
'http://www.example.com/?x=4', | |||||
(string)$uri); | |||||
$uri->setQueryParam('x', null); | |||||
$this->assertEqual( | |||||
'http://www.example.com/', | |||||
(string)$uri); | |||||
$uri->appendQueryParam('a', 'a'); | |||||
$uri->appendQueryParam('b', 'b'); | |||||
$uri->appendQueryParam('c', 'c'); | |||||
$uri->appendQueryParam('b', 'd'); | |||||
$this->assertEqual( | |||||
'http://www.example.com/?a=a&b=b&c=c&b=d', | |||||
(string)$uri); | |||||
$uri->setQueryParam('b', 'e'); | |||||
$this->assertEqual( | |||||
'http://www.example.com/?a=a&b=e&c=c', | |||||
(string)$uri, | |||||
pht( | |||||
'Replacing a parameter should retain position and overwrite other '. | |||||
'instances of the key.')); | |||||
} | |||||
public function testBadHTTPParameters() { | |||||
$uri = new PhutilURI('http://www.example.com/'); | |||||
$caught = null; | |||||
try { | |||||
$uri->setQueryParam(array(), 'x'); | |||||
} catch (Exception $ex) { | |||||
$caught = $ex; | |||||
} | |||||
$this->assertTrue( | |||||
(bool)$caught, | |||||
pht('Nonscalar HTTP keys should throw.')); | |||||
$caught = null; | |||||
try { | |||||
$uri->setQueryParam('x', array()); | |||||
} catch (Exception $ex) { | |||||
$caught = $ex; | |||||
} | |||||
$this->assertTrue( | |||||
(bool)$caught, | |||||
pht('Nonscalar HTTP values should throw.')); | |||||
} | |||||
public function testHTTPParameterTypes() { | |||||
// Whether you pass an integer or string, "0" should always be the same | |||||
// query parameter. | |||||
$uri = new PhutilURI('http://www.example.com/'); | |||||
$uri->appendQueryParam(0, 'a'); | |||||
$uri->appendQueryParam('0', 'b'); | |||||
$this->assertEqual( | |||||
'http://www.example.com/?0=a&0=b', | |||||
(string)$uri); | |||||
$uri->setQueryParam(0, 'c'); | |||||
$this->assertEqual( | |||||
'http://www.example.com/?0=c', | |||||
(string)$uri); | |||||
$uri->setQueryParam(0, 'a'); | |||||
$uri->appendQueryParam('0', 'b'); | |||||
$this->assertEqual( | |||||
'http://www.example.com/?0=a&0=b', | |||||
(string)$uri); | |||||
$uri->setQueryParam('0', 'c'); | |||||
$this->assertEqual( | |||||
'http://www.example.com/?0=c', | |||||
(string)$uri); | |||||
} | |||||
public function testGetQueryParamsAsMap() { | |||||
$uri = new PhutilURI('http://www.example.com/?x=1&x=2'); | |||||
$caught = null; | |||||
try { | |||||
$map = $uri->getQueryParamsAsMap(); | |||||
} catch (Exception $ex) { | |||||
$caught = $ex; | |||||
} | |||||
$this->assertTrue((bool)$caught); | |||||
} | |||||
} | } |