~ballot/wordpress/openstack-objectstorage-breaking-insight

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
<?php

namespace GuzzleHttp\Tests;

use GuzzleHttp\Query;

class QueryTest extends \PHPUnit_Framework_TestCase
{
    public function testCanCastToString()
    {
        $q = new Query(['foo' => 'baz', 'bar' => 'bam boozle']);
        $this->assertEquals('foo=baz&bar=bam%20boozle', (string) $q);
    }

    public function testCanDisableUrlEncoding()
    {
        $q = new Query(['bar' => 'bam boozle']);
        $q->setEncodingType(false);
        $this->assertEquals('bar=bam boozle', (string) $q);
    }

    public function testCanSpecifyRfc1783UrlEncodingType()
    {
        $q = new Query(['bar abc' => 'bam boozle']);
        $q->setEncodingType(Query::RFC1738);
        $this->assertEquals('bar+abc=bam+boozle', (string) $q);
    }

    public function testCanSpecifyRfc3986UrlEncodingType()
    {
        $q = new Query(['bar abc' => 'bam boozle', 'ሴ' => 'hi']);
        $q->setEncodingType(Query::RFC3986);
        $this->assertEquals('bar%20abc=bam%20boozle&%E1%88%B4=hi', (string) $q);
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testValidatesEncodingType()
    {
        (new Query(['bar' => 'bam boozle']))->setEncodingType('foo');
    }

    public function testAggregatesMultipleValues()
    {
        $q = new Query(['foo' => ['bar', 'baz']]);
        $this->assertEquals('foo%5B0%5D=bar&foo%5B1%5D=baz', (string) $q);
    }

    public function testCanSetAggregator()
    {
        $q = new Query(['foo' => ['bar', 'baz']]);
        $q->setAggregator(function (array $data) {
            return ['foo' => ['barANDbaz']];
        });
        $this->assertEquals('foo=barANDbaz', (string) $q);
    }

    public function testAllowsMultipleValuesPerKey()
    {
        $q = new Query();
        $q->add('facet', 'size');
        $q->add('facet', 'width');
        $q->add('facet.field', 'foo');
        // Use the duplicate aggregator
        $q->setAggregator($q::duplicateAggregator());
        $this->assertEquals('facet=size&facet=width&facet.field=foo', (string) $q);
    }

    public function testAllowsZeroValues()
    {
        $query = new Query(array(
            'foo' => 0,
            'baz' => '0',
            'bar' => null,
            'boo' => false
        ));
        $this->assertEquals('foo=0&baz=0&bar&boo=', (string) $query);
    }

    private $encodeData = [
        't' => [
            'v1' => ['a', '1'],
            'v2' => 'b',
            'v3' => ['v4' => 'c', 'v5' => 'd']
        ]
    ];

    public function testEncodesDuplicateAggregator()
    {
        $agg = Query::duplicateAggregator();
        $result = $agg($this->encodeData);
        $this->assertEquals(array(
            't[v1]' => ['a', '1'],
            't[v2]' => ['b'],
            't[v3][v4]' => ['c'],
            't[v3][v5]' => ['d'],
        ), $result);
    }

    public function testDuplicateEncodesNoNumericIndices()
    {
        $agg = Query::duplicateAggregator();
        $result = $agg($this->encodeData);
        $this->assertEquals(array(
            't[v1]' => ['a', '1'],
            't[v2]' => ['b'],
            't[v3][v4]' => ['c'],
            't[v3][v5]' => ['d'],
        ), $result);
    }

    public function testEncodesPhpAggregator()
    {
        $agg = Query::phpAggregator();
        $result = $agg($this->encodeData);
        $this->assertEquals(array(
            't[v1][0]' => ['a'],
            't[v1][1]' => ['1'],
            't[v2]' => ['b'],
            't[v3][v4]' => ['c'],
            't[v3][v5]' => ['d'],
        ), $result);
    }

    public function testPhpEncodesNoNumericIndices()
    {
        $agg = Query::phpAggregator(false);
        $result = $agg($this->encodeData);
        $this->assertEquals(array(
            't[v1][]' => ['a', '1'],
            't[v2]' => ['b'],
            't[v3][v4]' => ['c'],
            't[v3][v5]' => ['d'],
        ), $result);
    }

    public function testCanDisableUrlEncodingDecoding()
    {
        $q = Query::fromString('foo=bar+baz boo%20', false);
        $this->assertEquals('bar+baz boo%20', $q['foo']);
        $this->assertEquals('foo=bar+baz boo%20', (string) $q);
    }

    public function testCanChangeUrlEncodingDecodingToRfc1738()
    {
        $q = Query::fromString('foo=bar+baz', Query::RFC1738);
        $this->assertEquals('bar baz', $q['foo']);
        $this->assertEquals('foo=bar+baz', (string) $q);
    }

    public function testCanChangeUrlEncodingDecodingToRfc3986()
    {
        $q = Query::fromString('foo=bar%20baz', Query::RFC3986);
        $this->assertEquals('bar baz', $q['foo']);
        $this->assertEquals('foo=bar%20baz', (string) $q);
    }
}