1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Transport; |
4
|
|
|
|
5
|
|
|
use Elastica\Document; |
6
|
|
|
use Elastica\Query; |
7
|
|
|
use Elastica\ResultSet\DefaultBuilder; |
8
|
|
|
use Elastica\Test\Base as BaseTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal |
12
|
|
|
*/ |
13
|
|
|
class HttpTest extends BaseTest |
14
|
|
|
{ |
15
|
|
|
protected function tearDown(): void |
16
|
|
|
{ |
17
|
|
|
parent::tearDown(); |
18
|
|
|
\putenv('http_proxy='); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @group functional |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
public function testCurlNobodyOptionIsResetAfterHeadRequest(): void |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$client = $this->_getClient(); |
27
|
|
|
$index = $client->getIndex('curl_test'); |
28
|
|
|
$index->create([], [ |
29
|
|
|
'recreate' => true, |
30
|
|
|
]); |
31
|
|
|
$this->_waitForAllocation($index); |
32
|
|
|
|
33
|
|
|
// Force HEAD request to set CURLOPT_NOBODY = true |
34
|
|
|
$index->exists(); |
35
|
|
|
|
36
|
|
|
$id = 1; |
37
|
|
|
$data = ['id' => $id, 'name' => 'Item 1']; |
38
|
|
|
$doc = new Document($id, $data); |
39
|
|
|
|
40
|
|
|
$index->addDocument($doc); |
41
|
|
|
|
42
|
|
|
$index->refresh(); |
43
|
|
|
|
44
|
|
|
$doc = $index->getDocument($id); |
45
|
|
|
|
46
|
|
|
// Document should be retrieved correctly |
47
|
|
|
$this->assertSame($data, $doc->getData()); |
48
|
|
|
$this->assertEquals($id, $doc->getId()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @group functional |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function testUnicodeData(): void |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$client = $this->_getClient(); |
57
|
|
|
$index = $client->getIndex('curl_test'); |
58
|
|
|
$index->create([], [ |
59
|
|
|
'recreate' => true, |
60
|
|
|
]); |
61
|
|
|
$this->_waitForAllocation($index); |
62
|
|
|
|
63
|
|
|
// Force HEAD request to set CURLOPT_NOBODY = true |
64
|
|
|
$index->exists(); |
65
|
|
|
|
66
|
|
|
$id = 22; |
67
|
|
|
$data = ['id' => $id, 'name' => ' |
68
|
|
|
Сегодня, я вижу, особенно грустен твой взгляд, / |
69
|
|
|
И руки особенно тонки, колени обняв. / |
70
|
|
|
Послушай: далеко, далеко, на озере Чад / |
71
|
|
|
Изысканный бродит жираф.']; |
72
|
|
|
|
73
|
|
|
$doc = new Document($id, $data); |
74
|
|
|
|
75
|
|
|
$index->addDocument($doc); |
76
|
|
|
|
77
|
|
|
$index->refresh(); |
78
|
|
|
|
79
|
|
|
$doc = $index->getDocument($id); |
80
|
|
|
|
81
|
|
|
// Document should be retrieved correctly |
82
|
|
|
$this->assertSame($data, $doc->getData()); |
83
|
|
|
$this->assertEquals($id, $doc->getId()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @group functional |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function testWithEnvironmentalProxy(): void |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
\putenv('http_proxy='.$this->_getProxyUrl().'/'); |
92
|
|
|
|
93
|
|
|
$client = $this->_getClient(); |
94
|
|
|
$transferInfo = $client->request('_nodes')->getTransferInfo(); |
95
|
|
|
$this->assertEquals(200, $transferInfo['http_code']); |
96
|
|
|
|
97
|
|
|
$client->getConnection()->setProxy(null); // will not change anything |
98
|
|
|
$transferInfo = $client->request('_nodes')->getTransferInfo(); |
99
|
|
|
$this->assertEquals(200, $transferInfo['http_code']); |
100
|
|
|
|
101
|
|
|
\putenv('http_proxy='); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @group functional |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function testWithEnabledEnvironmentalProxy(): void |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
\putenv('http_proxy='.$this->_getProxyUrl403().'/'); |
110
|
|
|
$client = $this->_getClient(); |
111
|
|
|
$transferInfo = $client->request('_nodes')->getTransferInfo(); |
112
|
|
|
$this->assertEquals(403, $transferInfo['http_code']); |
113
|
|
|
$client = $this->_getClient(); |
114
|
|
|
$client->getConnection()->setProxy(''); |
115
|
|
|
$transferInfo = $client->request('_nodes')->getTransferInfo(); |
116
|
|
|
$this->assertEquals(200, $transferInfo['http_code']); |
117
|
|
|
\putenv('http_proxy='); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @group functional |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function testWithProxy(): void |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$client = $this->_getClient(); |
126
|
|
|
$client->getConnection()->setProxy($this->_getProxyUrl()); |
127
|
|
|
|
128
|
|
|
$transferInfo = $client->request('_nodes')->getTransferInfo(); |
129
|
|
|
$this->assertEquals(200, $transferInfo['http_code']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @group functional |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function testWithoutProxy(): void |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$client = $this->_getClient(); |
138
|
|
|
$client->getConnection()->setProxy(''); |
139
|
|
|
|
140
|
|
|
$transferInfo = $client->request('_nodes')->getTransferInfo(); |
141
|
|
|
$this->assertEquals(200, $transferInfo['http_code']); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @group functional |
146
|
|
|
*/ |
147
|
|
|
public function testBodyReuse(): void |
148
|
|
|
{ |
149
|
|
|
$client = $this->_getClient(); |
150
|
|
|
|
151
|
|
|
$index = $client->getIndex('elastica_body_reuse_test'); |
152
|
|
|
$index->create([], [ |
153
|
|
|
'recreate' => true, |
154
|
|
|
]); |
155
|
|
|
$this->_waitForAllocation($index); |
156
|
|
|
|
157
|
|
|
$index->addDocument(new Document(1, ['test' => 'test'])); |
158
|
|
|
|
159
|
|
|
$index->refresh(); |
160
|
|
|
|
161
|
|
|
$resultSet = $index->search([ |
162
|
|
|
'query' => [ |
163
|
|
|
'query_string' => [ |
164
|
|
|
'query' => 'pew pew pew', |
165
|
|
|
], |
166
|
|
|
], |
167
|
|
|
]); |
168
|
|
|
|
169
|
|
|
$this->assertEquals(0, $resultSet->getTotalHits()); |
170
|
|
|
|
171
|
|
|
$response = $index->request('/_search', 'POST'); |
172
|
|
|
|
173
|
|
|
$builder = new DefaultBuilder(); |
174
|
|
|
$resultSet = $builder->buildResultSet($response, Query::create([])); |
175
|
|
|
|
176
|
|
|
$this->assertEquals(1, $resultSet->getTotalHits()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @group functional |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
public function testRequestSuccessWithHttpCompressionEnabled(): void |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$client = $this->_getClient(['transport' => ['type' => 'Http', 'compression' => true, 'curl' => [\CURLINFO_HEADER_OUT => true]]]); |
185
|
|
|
|
186
|
|
|
$index = $client->getIndex('elastica_request_with_body_and_http_compression_enabled'); |
187
|
|
|
|
188
|
|
|
$createIndexResponse = $index->create([], [ |
189
|
|
|
'recreate' => true, |
190
|
|
|
]); |
191
|
|
|
|
192
|
|
|
$createIndexResponseTransferInfo = $createIndexResponse->getTransferInfo(); |
193
|
|
|
if (\method_exists($this, 'assertMatchesRegularExpression')) { |
194
|
|
|
$this->assertMatchesRegularExpression('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']); |
195
|
|
|
} else { |
196
|
|
|
$this->assertRegExp('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
$this->assertArrayHasKey('acknowledged', $createIndexResponse->getData()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @group functional |
203
|
|
|
*/ |
204
|
|
View Code Duplication |
public function testRequestSuccessWithHttpCompressionDisabled(): void |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
$client = $this->_getClient(['transport' => ['type' => 'Http', 'compression' => false, 'curl' => [\CURLINFO_HEADER_OUT => true]]]); |
207
|
|
|
|
208
|
|
|
$index = $client->getIndex('elastica_request_with_body_and_http_compression_disabled'); |
209
|
|
|
|
210
|
|
|
$createIndexResponse = $index->create([], [ |
211
|
|
|
'recreate' => true, |
212
|
|
|
]); |
213
|
|
|
|
214
|
|
|
$createIndexResponseTransferInfo = $createIndexResponse->getTransferInfo(); |
215
|
|
|
if (\method_exists($this, 'assertMatchesRegularExpression')) { |
216
|
|
|
$this->assertMatchesRegularExpression('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']); |
217
|
|
|
} else { |
218
|
|
|
$this->assertRegExp('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
$this->assertArrayHasKey('acknowledged', $createIndexResponse->getData()); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.