Skip to content

Commit 3e7fa92

Browse files
committed
tests for connection auth params
1 parent d140440 commit 3e7fa92

File tree

1 file changed

+191
-4
lines changed

1 file changed

+191
-4
lines changed

tests/Elasticsearch/Tests/Connections/ConnectionTest.php

Lines changed: 191 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ protected function setUp()
2323
$this->serializer = $this->createMock(SerializerInterface::class);
2424
}
2525

26+
/**
27+
* @covers \Connection
28+
*/
2629
public function testConstructor()
2730
{
28-
$params = [];
2931
$host = [
3032
'host' => 'localhost'
3133
];
@@ -34,7 +36,7 @@ public function testConstructor()
3436
function () {
3537
},
3638
$host,
37-
$params,
39+
[],
3840
$this->serializer,
3941
$this->logger,
4042
$this->trace
@@ -43,6 +45,11 @@ function () {
4345
$this->assertInstanceOf(Connection::class, $connection);
4446
}
4547

48+
/**
49+
* @depends testConstructor
50+
*
51+
* @covers \Connection::getHeaders
52+
*/
4653
public function testGetHeadersContainUserAgent()
4754
{
4855
$params = [];
@@ -60,12 +67,19 @@ function () {
6067
$this->trace
6168
);
6269

63-
$headers = $connection->getHeaders();
70+
$headers = $connection->getHeaders();
6471

6572
$this->assertArrayHasKey('User-Agent', $headers);
6673
$this->assertContains('elasticsearch-php/'. Client::VERSION, $headers['User-Agent'][0]);
6774
}
6875

76+
/**
77+
* @depends testGetHeadersContainUserAgent
78+
*
79+
* @covers \Connection::getHeaders
80+
* @covers \Connection::performRequest
81+
* @covers \Connection::getLastRequestInfo
82+
*/
6983
public function testUserAgentHeaderIsSent()
7084
{
7185
$params = [];
@@ -81,9 +95,182 @@ public function testUserAgentHeaderIsSent()
8195
$this->logger,
8296
$this->trace
8397
);
84-
$result = $connection->performRequest('GET', '/');
98+
$result = $connection->performRequest('GET', '/');
8599
$request = $connection->getLastRequestInfo()['request'];
100+
86101
$this->assertArrayHasKey('User-Agent', $request['headers']);
87102
$this->assertContains('elasticsearch-php/'. Client::VERSION, $request['headers']['User-Agent'][0]);
88103
}
104+
105+
/**
106+
* @depends testConstructor
107+
*
108+
* @covers \Connection::getHeaders
109+
* @covers \Connection::performRequest
110+
* @covers \Connection::getLastRequestInfo
111+
*/
112+
public function testGetHeadersContainsHostArrayConfig()
113+
{
114+
$host = [
115+
'host' => 'localhost',
116+
'user' => 'foo',
117+
'pass' => 'bar',
118+
];
119+
120+
$connection = new Connection(
121+
ClientBuilder::defaultHandler(),
122+
$host,
123+
[],
124+
$this->serializer,
125+
$this->logger,
126+
$this->trace
127+
);
128+
$result = $connection->performRequest('GET', '/');
129+
$request = $connection->getLastRequestInfo()['request'];
130+
131+
$this->assertArrayHasKey(CURLOPT_HTTPAUTH, $request['client']['curl']);
132+
$this->assertArrayHasKey(CURLOPT_USERPWD, $request['client']['curl']);
133+
$this->assertArrayNotHasKey('Authorization', $request['headers']);
134+
$this->assertContains('foo:bar', $request['client']['curl'][CURLOPT_USERPWD]);
135+
}
136+
137+
/**
138+
* @depends testGetHeadersContainsHostArrayConfig
139+
*
140+
* @covers \Connection::getHeaders
141+
* @covers \Connection::performRequest
142+
* @covers \Connection::getLastRequestInfo
143+
*/
144+
public function testGetHeadersContainApiKeyAuth()
145+
{
146+
$params = ['client' => ['headers' => [
147+
'Authorization' => [
148+
'ApiKey ' . base64_encode(sha1((string)time()))
149+
]
150+
] ] ];
151+
$host = [
152+
'host' => 'localhost'
153+
];
154+
155+
$connection = new Connection(
156+
ClientBuilder::defaultHandler(),
157+
$host,
158+
$params,
159+
$this->serializer,
160+
$this->logger,
161+
$this->trace
162+
);
163+
$result = $connection->performRequest('GET', '/');
164+
$request = $connection->getLastRequestInfo()['request'];
165+
166+
$this->assertArrayHasKey('Authorization', $request['headers']);
167+
$this->assertArrayNotHasKey(CURLOPT_HTTPAUTH, $request['headers']);
168+
$this->assertContains($params['client']['headers']['Authorization'][0], $request['headers']['Authorization'][0]);
169+
}
170+
171+
/**
172+
* @depends testGetHeadersContainApiKeyAuth
173+
*
174+
* @covers \Connection::getHeaders
175+
* @covers \Connection::performRequest
176+
* @covers \Connection::getLastRequestInfo
177+
*/
178+
public function testGetHeadersContainApiKeyAuthOverHostArrayConfig()
179+
{
180+
$params = ['client' => ['headers' => [
181+
'Authorization' => [
182+
'ApiKey ' . base64_encode(sha1((string)time()))
183+
]
184+
] ] ];
185+
$host = [
186+
'host' => 'localhost',
187+
'user' => 'foo',
188+
'pass' => 'bar',
189+
];
190+
191+
$connection = new Connection(
192+
ClientBuilder::defaultHandler(),
193+
$host,
194+
$params,
195+
$this->serializer,
196+
$this->logger,
197+
$this->trace
198+
);
199+
$result = $connection->performRequest('GET', '/');
200+
$request = $connection->getLastRequestInfo()['request'];
201+
202+
$this->assertArrayHasKey('Authorization', $request['headers']);
203+
$this->assertArrayNotHasKey(CURLOPT_HTTPAUTH, $request['headers']);
204+
$this->assertContains($params['client']['headers']['Authorization'][0], $request['headers']['Authorization'][0]);
205+
}
206+
207+
/**
208+
* @depends testGetHeadersContainsHostArrayConfig
209+
*
210+
* @covers \Connection::getHeaders
211+
* @covers \Connection::performRequest
212+
* @covers \Connection::getLastRequestInfo
213+
*/
214+
public function testGetHeadersContainBasicAuth()
215+
{
216+
$params = ['client' => ['curl' => [
217+
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
218+
CURLOPT_USERPWD => 'username:password',
219+
] ] ];
220+
$host = [
221+
'host' => 'localhost'
222+
];
223+
224+
$connection = new Connection(
225+
ClientBuilder::defaultHandler(),
226+
$host,
227+
$params,
228+
$this->serializer,
229+
$this->logger,
230+
$this->trace
231+
);
232+
$result = $connection->performRequest('GET', '/');
233+
$request = $connection->getLastRequestInfo()['request'];
234+
235+
$this->assertArrayHasKey(CURLOPT_HTTPAUTH, $request['client']['curl']);
236+
$this->assertArrayHasKey(CURLOPT_USERPWD, $request['client']['curl']);
237+
$this->assertArrayNotHasKey('Authorization', $request['headers']);
238+
$this->assertContains($params['client']['curl'][CURLOPT_USERPWD], $request['client']['curl'][CURLOPT_USERPWD]);
239+
}
240+
241+
/**
242+
* @depends testGetHeadersContainBasicAuth
243+
*
244+
* @covers \Connection::getHeaders
245+
* @covers \Connection::performRequest
246+
* @covers \Connection::getLastRequestInfo
247+
*/
248+
public function testGetHeadersContainBasicAuthOverHostArrayConfig()
249+
{
250+
$params = ['client' => ['curl' => [
251+
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
252+
CURLOPT_USERPWD => 'username:password',
253+
] ] ];
254+
$host = [
255+
'host' => 'localhost',
256+
'user' => 'foo',
257+
'pass' => 'bar',
258+
];
259+
260+
$connection = new Connection(
261+
ClientBuilder::defaultHandler(),
262+
$host,
263+
$params,
264+
$this->serializer,
265+
$this->logger,
266+
$this->trace
267+
);
268+
$result = $connection->performRequest('GET', '/');
269+
$request = $connection->getLastRequestInfo()['request'];
270+
271+
$this->assertArrayHasKey(CURLOPT_HTTPAUTH, $request['client']['curl']);
272+
$this->assertArrayHasKey(CURLOPT_USERPWD, $request['client']['curl']);
273+
$this->assertArrayNotHasKey('Authorization', $request['headers']);
274+
$this->assertContains('username:password', $request['client']['curl'][CURLOPT_USERPWD]);
275+
}
89276
}

0 commit comments

Comments
 (0)