Completed
Pull Request — master (#1911)
by Sam
03:08
created

HttpTest::testBodyReuse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
90
    {
91
        $this->checkProxy($this->_getProxyUrl());
92
        \putenv('http_proxy='.$this->_getProxyUrl().'/');
93
94
        $client = $this->_getClient();
95
        $transferInfo = $client->request('_nodes')->getTransferInfo();
96
        $this->assertEquals(200, $transferInfo['http_code']);
97
98
        $client->getConnection()->setProxy(null); // will not change anything
99
        $transferInfo = $client->request('_nodes')->getTransferInfo();
100
        $this->assertEquals(200, $transferInfo['http_code']);
101
102
        \putenv('http_proxy=');
103
    }
104
105
    /**
106
     * @group functional
107
     */
108 View Code Duplication
    public function testWithEnabledEnvironmentalProxy(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
109
    {
110
        $this->checkProxy($this->_getProxyUrl403());
111
        \putenv('http_proxy='.$this->_getProxyUrl403().'/');
112
        $client = $this->_getClient();
113
        $transferInfo = $client->request('_nodes')->getTransferInfo();
114
        $this->assertEquals(403, $transferInfo['http_code']);
115
        $client = $this->_getClient();
116
        $client->getConnection()->setProxy('');
117
        $transferInfo = $client->request('_nodes')->getTransferInfo();
118
        $this->assertEquals(200, $transferInfo['http_code']);
119
        \putenv('http_proxy=');
120
    }
121
122
    /**
123
     * @group functional
124
     */
125
    public function testWithProxy(): void
126
    {
127
        $this->checkProxy($this->_getProxyUrl());
128
        $client = $this->_getClient();
129
        $client->getConnection()->setProxy($this->_getProxyUrl());
130
131
        $transferInfo = $client->request('_nodes')->getTransferInfo();
132
        $this->assertEquals(200, $transferInfo['http_code']);
133
    }
134
135
    /**
136
     * @group functional
137
     */
138
    public function testWithoutProxy(): void
139
    {
140
        $client = $this->_getClient();
141
        $client->getConnection()->setProxy('');
142
143
        $transferInfo = $client->request('_nodes')->getTransferInfo();
144
        $this->assertEquals(200, $transferInfo['http_code']);
145
    }
146
147
    /**
148
     * @group functional
149
     */
150
    public function testBodyReuse(): void
151
    {
152
        $client = $this->_getClient();
153
154
        $index = $client->getIndex('elastica_body_reuse_test');
155
        $index->create([], [
156
            'recreate' => true,
157
        ]);
158
        $this->_waitForAllocation($index);
159
160
        $index->addDocument(new Document(1, ['test' => 'test']));
161
162
        $index->refresh();
163
164
        $resultSet = $index->search([
165
            'query' => [
166
                'query_string' => [
167
                    'query' => 'pew pew pew',
168
                ],
169
            ],
170
        ]);
171
172
        $this->assertEquals(0, $resultSet->getTotalHits());
173
174
        $response = $index->request('/_search', 'POST');
175
176
        $builder = new DefaultBuilder();
177
        $resultSet = $builder->buildResultSet($response, Query::create([]));
178
179
        $this->assertEquals(1, $resultSet->getTotalHits());
180
    }
181
182
    /**
183
     * @group functional
184
     */
185 View Code Duplication
    public function testRequestSuccessWithHttpCompressionEnabled(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
186
    {
187
        $client = $this->_getClient(['transport' => ['type' => 'Http', 'compression' => true, 'curl' => [\CURLINFO_HEADER_OUT => true]]]);
188
189
        $index = $client->getIndex('elastica_request_with_body_and_http_compression_enabled');
190
191
        $createIndexResponse = $index->create([], [
192
            'recreate' => true,
193
        ]);
194
195
        $createIndexResponseTransferInfo = $createIndexResponse->getTransferInfo();
196
        if (\method_exists($this, 'assertMatchesRegularExpression')) {
197
            $this->assertMatchesRegularExpression('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']);
198
        } else {
199
            $this->assertRegExp('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertRegExp() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/4086

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
200
        }
201
        $this->assertArrayHasKey('acknowledged', $createIndexResponse->getData());
202
    }
203
204
    /**
205
     * @group functional
206
     */
207 View Code Duplication
    public function testRequestSuccessWithHttpCompressionDisabled(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
208
    {
209
        $client = $this->_getClient(['transport' => ['type' => 'Http', 'compression' => false, 'curl' => [\CURLINFO_HEADER_OUT => true]]]);
210
211
        $index = $client->getIndex('elastica_request_with_body_and_http_compression_disabled');
212
213
        $createIndexResponse = $index->create([], [
214
            'recreate' => true,
215
        ]);
216
217
        $createIndexResponseTransferInfo = $createIndexResponse->getTransferInfo();
218
        if (\method_exists($this, 'assertMatchesRegularExpression')) {
219
            $this->assertMatchesRegularExpression('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']);
220
        } else {
221
            $this->assertRegExp('/Accept-Encoding:\ (gzip|deflate)/', $createIndexResponseTransferInfo['request_header']);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertRegExp() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/4086

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
222
        }
223
        $this->assertArrayHasKey('acknowledged', $createIndexResponse->getData());
224
    }
225
226
    protected function checkProxy($url): void
227
    {
228
        $url = \parse_url($url);
229
        $this->_checkConnection($url['host'], $url['port']);
230
    }
231
}
232