1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234:
<?php
namespace Guzzle\Tests\Http;
use Guzzle\Http\QueryString;
use Guzzle\Http\QueryAggregator\DuplicateAggregator;
use Guzzle\Http\QueryAggregator\CommaAggregator;
class QueryStringTest extends \Guzzle\Tests\GuzzleTestCase
{
protected $q;
public function setup()
{
$this->q = new QueryString();
}
public function testGetFieldSeparator()
{
$this->assertEquals('&', $this->q->getFieldSeparator());
}
public function testGetValueSeparator()
{
$this->assertEquals('=', $this->q->getValueSeparator());
}
public function testIsUrlEncoding()
{
$this->assertEquals('RFC 3986', $this->q->getUrlEncoding());
$this->assertTrue($this->q->isUrlEncoding());
$this->assertEquals('foo%20bar', $this->q->encodeValue('foo bar'));
$this->q->useUrlEncoding(QueryString::FORM_URLENCODED);
$this->assertTrue($this->q->isUrlEncoding());
$this->assertEquals(QueryString::FORM_URLENCODED, $this->q->getUrlEncoding());
$this->assertEquals('foo+bar', $this->q->encodeValue('foo bar'));
$this->assertSame($this->q, $this->q->useUrlEncoding(false));
$this->assertFalse($this->q->isUrlEncoding());
$this->assertFalse($this->q->isUrlEncoding());
}
public function testSetFieldSeparator()
{
$this->assertEquals($this->q, $this->q->setFieldSeparator('/'));
$this->assertEquals('/', $this->q->getFieldSeparator());
}
public function testSetValueSeparator()
{
$this->assertEquals($this->q, $this->q->setValueSeparator('/'));
$this->assertEquals('/', $this->q->getValueSeparator());
}
public function testUrlEncode()
{
$params = array(
'test' => 'value',
'test 2' => 'this is a test?',
'test3' => array('v1', 'v2', 'v3'),
'ሴ' => 'bar'
);
$encoded = array(
'test' => 'value',
'test%202' => rawurlencode('this is a test?'),
'test3%5B0%5D' => 'v1',
'test3%5B1%5D' => 'v2',
'test3%5B2%5D' => 'v3',
'%E1%88%B4' => 'bar'
);
$this->q->replace($params);
$this->assertEquals($encoded, $this->q->urlEncode());
$testData = array('test 2' => 'this is a test');
$this->q->replace($testData);
$this->q->useUrlEncoding(false);
$this->assertEquals($testData, $this->q->urlEncode());
}
public function testToString()
{
$this->assertEquals('', $this->q->__toString());
$params = array(
'test' => 'value',
'test 2' => 'this is a test?',
'test3' => array('v1', 'v2', 'v3'),
'test4' => null,
);
$this->q->replace($params);
$this->assertEquals('test=value&test%202=this%20is%20a%20test%3F&test3%5B0%5D=v1&test3%5B1%5D=v2&test3%5B2%5D=v3&test4', $this->q->__toString());
$this->q->useUrlEncoding(false);
$this->assertEquals('test=value&test 2=this is a test?&test3[0]=v1&test3[1]=v2&test3[2]=v3&test4', $this->q->__toString());
$this->q->setAggregator(new CommaAggregator());
$this->assertEquals('test=value&test 2=this is a test?&test3=v1,v2,v3&test4', $this->q->__toString());
}
public function testAllowsMultipleValuesPerKey()
{
$q = new QueryString();
$q->add('facet', 'size');
$q->add('facet', 'width');
$q->add('facet.field', 'foo');
$q->setAggregator(new DuplicateAggregator());
$this->assertEquals('facet=size&facet=width&facet.field=foo', $q->__toString());
}
public function testAllowsNestedQueryData()
{
$this->q->replace(array(
'test' => 'value',
't' => array(
'v1' => 'a',
'v2' => 'b',
'v3' => array(
'v4' => 'c',
'v5' => 'd',
)
)
));
$this->q->useUrlEncoding(false);
$this->assertEquals('test=value&t[v1]=a&t[v2]=b&t[v3][v4]=c&t[v3][v5]=d', $this->q->__toString());
}
public function parseQueryProvider()
{
return array(
array('q=a&q=b', array('q' => array('a', 'b'))),
array('q[]=a&q[]=b', array('q' => array('a', 'b'))),
array('q[]=a', array('q' => array('a'))),
array('q.a=a&q.b=b', array(
'q.a' => 'a',
'q.b' => 'b'
)),
array('q%20a=a%20b', array('q a' => 'a b')),
array('q&a', array('q' => false, 'a' => false)),
);
}
public function testParsesQueryStrings($query, $data)
{
$query = QueryString::fromString($query);
$this->assertEquals($data, $query->getAll());
}
public function testProperlyDealsWithDuplicateQueryStringValues()
{
$query = QueryString::fromString('foo=a&foo=b&?µ=c');
$this->assertEquals(array('a', 'b'), $query->get('foo'));
$this->assertEquals('c', $query->get('?µ'));
}
public function testAllowsBlankQueryStringValues()
{
$query = QueryString::fromString('foo');
$this->assertEquals('foo', (string) $query);
$query->set('foo', QueryString::BLANK);
$this->assertEquals('foo', (string) $query);
}
public function testAllowsFalsyQueryStringValues()
{
$query = QueryString::fromString('0');
$this->assertEquals('0', (string) $query);
$query->set('0', QueryString::BLANK);
$this->assertSame('0', (string) $query);
}
public function testFromStringIgnoresQuestionMark()
{
$query = QueryString::fromString('foo=baz&bar=boo');
$this->assertEquals('foo=baz&bar=boo', (string) $query);
}
public function testConvertsPlusSymbolsToSpaces()
{
$query = QueryString::fromString('var=foo+bar');
$this->assertEquals('foo bar', $query->get('var'));
}
public function testFromStringDoesntMangleZeroes()
{
$query = QueryString::fromString('var=0');
$this->assertSame('0', $query->get('var'));
}
public function testAllowsZeroValues()
{
$query = new QueryString(array(
'foo' => 0,
'baz' => '0',
'bar' => null,
'boo' => false,
'bam' => ''
));
$this->assertEquals('foo=0&baz=0&bar&boo&bam=', (string) $query);
}
public function testFromStringDoesntStripTrailingEquals()
{
$query = QueryString::fromString('data=mF0b3IiLCJUZWFtIERldiJdfX0=');
$this->assertEquals('mF0b3IiLCJUZWFtIERldiJdfX0=', $query->get('data'));
}
public function testGuessesIfDuplicateAggregatorShouldBeUsed()
{
$query = QueryString::fromString('test=a&test=b');
$this->assertEquals('test=a&test=b', (string) $query);
}
public function testGuessesIfDuplicateAggregatorShouldBeUsedAndChecksForPhpStyle()
{
$query = QueryString::fromString('test[]=a&test[]=b');
$this->assertEquals('test%5B0%5D=a&test%5B1%5D=b', (string) $query);
}
}