Changeset View
Changeset View
Standalone View
Standalone View
src/parser/__tests__/PhutilURITestCase.php
Show First 20 Lines • Show All 286 Lines • ▼ Show 20 Lines | $this->assertEqual( | ||||
'http://www.example.com/?x=1&x=2', | 'http://www.example.com/?x=1&x=2', | ||||
(string)$uri); | (string)$uri); | ||||
$uri->appendQueryParam('x', '3'); | $uri->appendQueryParam('x', '3'); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://www.example.com/?x=1&x=2&x=3', | 'http://www.example.com/?x=1&x=2&x=3', | ||||
(string)$uri); | (string)$uri); | ||||
$uri->setQueryParam('x', '4'); | $uri->replaceQueryParam('x', '4'); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://www.example.com/?x=4', | 'http://www.example.com/?x=4', | ||||
(string)$uri); | (string)$uri); | ||||
$uri->setQueryParam('x', null); | $uri->removeQueryParam('x'); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://www.example.com/', | 'http://www.example.com/', | ||||
(string)$uri); | (string)$uri); | ||||
$uri->appendQueryParam('a', 'a'); | $uri->appendQueryParam('a', 'a'); | ||||
$uri->appendQueryParam('b', 'b'); | $uri->appendQueryParam('b', 'b'); | ||||
$uri->appendQueryParam('c', 'c'); | $uri->appendQueryParam('c', 'c'); | ||||
$uri->appendQueryParam('b', 'd'); | $uri->appendQueryParam('b', 'd'); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://www.example.com/?a=a&b=b&c=c&b=d', | 'http://www.example.com/?a=a&b=b&c=c&b=d', | ||||
(string)$uri); | (string)$uri); | ||||
$uri->setQueryParam('b', 'e'); | $uri->replaceQueryParam('b', 'e'); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://www.example.com/?a=a&b=e&c=c', | 'http://www.example.com/?a=a&c=c&b=e', | ||||
(string)$uri, | (string)$uri, | ||||
pht( | pht( | ||||
'Replacing a parameter should retain position and overwrite other '. | 'Replacing a parameter should overwrite other instances of the key.')); | ||||
'instances of the key.')); | |||||
} | } | ||||
public function testBadHTTPParameters() { | public function testBadHTTPParameters() { | ||||
$uri = new PhutilURI('http://www.example.com/'); | $uri = new PhutilURI('http://www.example.com/'); | ||||
$caught = null; | $caught = null; | ||||
try { | try { | ||||
$uri->setQueryParam(array(), 'x'); | $uri->replaceQueryParam(array(), 'x'); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
$caught = $ex; | $caught = $ex; | ||||
} | } | ||||
$this->assertTrue( | $this->assertTrue( | ||||
(bool)$caught, | (bool)$caught, | ||||
pht('Nonscalar HTTP keys should throw.')); | pht('Nonscalar HTTP keys should throw.')); | ||||
$caught = null; | $caught = null; | ||||
try { | try { | ||||
$uri->setQueryParam('x', array()); | $uri->replaceQueryParam('x', array()); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
$caught = $ex; | $caught = $ex; | ||||
} | } | ||||
$this->assertTrue( | $this->assertTrue( | ||||
(bool)$caught, | (bool)$caught, | ||||
pht('Nonscalar HTTP values should throw.')); | pht('Nonscalar HTTP values should throw.')); | ||||
} | } | ||||
public function testHTTPParameterTypes() { | public function testHTTPParameterTypes() { | ||||
// Whether you pass an integer or string, "0" should always be the same | // Whether you pass an integer or string, "0" should always be the same | ||||
// query parameter. | // query parameter. | ||||
$uri = new PhutilURI('http://www.example.com/'); | $uri = new PhutilURI('http://www.example.com/'); | ||||
$uri->appendQueryParam(0, 'a'); | $uri->appendQueryParam(0, 'a'); | ||||
$uri->appendQueryParam('0', 'b'); | $uri->appendQueryParam('0', 'b'); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://www.example.com/?0=a&0=b', | 'http://www.example.com/?0=a&0=b', | ||||
(string)$uri); | (string)$uri); | ||||
$uri->setQueryParam(0, 'c'); | $uri->replaceQueryParam(0, 'c'); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://www.example.com/?0=c', | 'http://www.example.com/?0=c', | ||||
(string)$uri); | (string)$uri); | ||||
$uri->setQueryParam(0, 'a'); | $uri->replaceQueryParam(0, 'a'); | ||||
$uri->appendQueryParam('0', 'b'); | $uri->appendQueryParam('0', 'b'); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://www.example.com/?0=a&0=b', | 'http://www.example.com/?0=a&0=b', | ||||
(string)$uri); | (string)$uri); | ||||
$uri->setQueryParam('0', 'c'); | $uri->replaceQueryParam('0', 'c'); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'http://www.example.com/?0=c', | 'http://www.example.com/?0=c', | ||||
(string)$uri); | (string)$uri); | ||||
} | } | ||||
public function testGetQueryParamsAsMap() { | public function testGetQueryParamsAsMap() { | ||||
$uri = new PhutilURI('http://www.example.com/?x=1&x=2'); | $uri = new PhutilURI('http://www.example.com/?x=1&x=2'); | ||||
$caught = null; | $caught = null; | ||||
try { | try { | ||||
$map = $uri->getQueryParamsAsMap(); | $map = $uri->getQueryParamsAsMap(); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
$caught = $ex; | $caught = $ex; | ||||
} | } | ||||
$this->assertTrue((bool)$caught); | $this->assertTrue((bool)$caught); | ||||
} | } | ||||
public function testQueryURIConstruction() { | |||||
$uri = new PhutilURI('http://example.com/', array('y' => '1')); | |||||
$this->assertEqual( | |||||
'http://example.com/?y=1', | |||||
(string)$uri); | |||||
$uri = new PhutilURI('http://example.com/?x=2', array('y' => '1')); | |||||
$this->assertEqual( | |||||
'http://example.com/?x=2&y=1', | |||||
(string)$uri); | |||||
$caught = null; | |||||
try { | |||||
$uri = new PhutilURI('http://example.com/?y=3', array('y' => '1')); | |||||
} catch (InvalidArgumentException $ex) { | |||||
$caught = $ex; | |||||
} | |||||
$this->assertTrue((bool)$caught); | |||||
$uri = new PhutilURI('http://example.com/?a=1', array('b' => '2')); | |||||
$uri = new PhutilURI($uri, array('c' => '3')); | |||||
$this->assertEqual( | |||||
'http://example.com/?a=1&b=2&c=3', | |||||
(string)$uri); | |||||
} | |||||
} | } |