testProxyRequestExceptionHTTP()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
dl 31
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use GuzzleHttp\Exception\RequestException;
25
use GuzzleHttp\Exception\ClientException;
26
use GuzzleHttp\Exception\ConnectException;
27
28
class ProxyControllerTest extends \PHPUnit_Framework_TestCase {
29
30
	private $appName;
31
	private $request;
32
	private $client;
33
	private $l10n;
34
	private $logger;
35
36
	private $newClient;
37
	private $response0;
38
	private $response1;
39
	private $response2;
40
	private $response3;
41
	private $response4;
42
	private $response5;
43
	private $exceptionRequest;
44
	private $exceptionResponse;
45
46
	private $controller;
47
48
	public function setUp() {
49
		$this->appName = 'calendar';
50
		$this->request = $this->getMockBuilder('\OCP\IRequest')
51
			->disableOriginalConstructor()
52
			->getMock();
53
		$this->client = $this->getMockBuilder('\OCP\Http\Client\IClientService')
54
			->disableOriginalConstructor()
55
			->getMock();
56
		$this->l10n = $this->getMockBuilder('\OCP\IL10N')
57
			->disableOriginalConstructor()
58
			->getMock();
59
		$this->logger = $this->getMockBuilder('\OCP\ILogger')
60
			->disableOriginalConstructor()
61
			->getMock();
62
63
		$this->newClient = $this->getMockBuilder('\OCP\Http\Client\IClient')
64
			->disableOriginalConstructor()
65
			->getMock();
66
		$this->response0 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
67
			->disableOriginalConstructor()
68
			->getMock();
69
		$this->response1 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
70
			->disableOriginalConstructor()
71
			->getMock();
72
		$this->response2 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
73
			->disableOriginalConstructor()
74
			->getMock();
75
		$this->response3 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
76
			->disableOriginalConstructor()
77
			->getMock();
78
		$this->response4 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
79
			->disableOriginalConstructor()
80
			->getMock();
81
		$this->response5 = $this->getMockBuilder('\OCP\Http\Client\IResponse')
82
			->disableOriginalConstructor()
83
			->getMock();
84
85
		$this->exceptionRequest = $this->getMockBuilder('GuzzleHttp\Message\RequestInterface')
86
			->disableOriginalConstructor()
87
			->getMock();
88
		$this->exceptionResponse = $this->getMockBuilder('GuzzleHttp\Message\ResponseInterface')
89
			->disableOriginalConstructor()
90
			->getMock();
91
92
		$this->controller = new ProxyController($this->appName, $this->request,
93
			$this->client, $this->l10n, $this->logger);
94
	}
95
96
	public function testProxy() {
97
		$testUrl = 'http://abc.def/foobar?123';
98
99
		$this->client->expects($this->once())
100
			->method('newClient')
101
			->will($this->returnValue($this->newClient));
102
		$this->newClient->expects($this->once())
103
			->method('get')
104
			->with($testUrl, [
105
				'stream' => true,
106
				'allow_redirects' => false,
107
			])
108
			->will($this->returnValue($this->response0));
109
		$this->response0->expects($this->once())
110
			->method('getStatusCode')
111
			->with()
112
			->will($this->returnValue(200));
113
114
		$actual = $this->controller->proxy($testUrl);
115
116
		$this->assertInstanceOf('OCA\Calendar\Http\StreamResponse', $actual);
117
		$this->assertEquals('text/calendar', $actual->getHeaders()['Content-Type']);
118
	}
119
120
	public function testProxyClientException() {
121
		$testUrl = 'http://abc.def/foobar?123';
122
123
		$this->client->expects($this->once())
124
			->method('newClient')
125
			->will($this->returnValue($this->newClient));
126
		$this->newClient->expects($this->once())
127
			->method('get')
128
			->with($testUrl, [
129
				'stream' => true,
130
				'allow_redirects' => false,
131
			])
132
			->will($this->throwException(new ClientException('Exception Message foo bar 42',
133
				$this->exceptionRequest, $this->exceptionResponse)));
134
		$this->exceptionResponse->expects($this->once())
135
			->method('getStatusCode')
136
			->will($this->returnValue(403));
137
		$this->l10n->expects($this->once())
138
			->method('t')
139
			->with($this->equalTo('The remote server did not give us access to the calendar (HTTP {%s} error)', '403'))
140
			->will($this->returnValue('translated string 1337'));
141
		$this->logger->expects($this->once())
142
			->method('debug')
143
			->with($this->equalTo('Exception Message foo bar 42'));
144
145
		$actual = $this->controller->proxy($testUrl);
146
147
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
148
		$this->assertEquals('422', $actual->getStatus());
149
		$this->assertEquals([
150
			'message' => 'translated string 1337',
151
			'proxy_code' => 403
152
		], $actual->getData());
153
	}
154
155 View Code Duplication
	public function testProxyConnectException() {
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...
156
		$testUrl = 'http://abc.def/foobar?123';
157
158
		$this->client->expects($this->once())
159
			->method('newClient')
160
			->will($this->returnValue($this->newClient));
161
		$this->newClient->expects($this->once())
162
			->method('get')
163
			->with($testUrl, [
164
				'stream' => true,
165
				'allow_redirects' => false,
166
			])
167
			->will($this->throwException(new ConnectException('Exception Message foo bar 42',
168
				$this->exceptionRequest, $this->exceptionResponse)));
169
		$this->l10n->expects($this->once())
170
			->method('t')
171
			->with($this->equalTo('Error connecting to remote server'))
172
			->will($this->returnValue('translated string 1337'));
173
		$this->logger->expects($this->once())
174
			->method('debug')
175
			->with($this->equalTo('Exception Message foo bar 42'));
176
177
		$actual = $this->controller->proxy($testUrl);
178
179
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
180
		$this->assertEquals('422', $actual->getStatus());
181
		$this->assertEquals([
182
			'message' => 'translated string 1337',
183
			'proxy_code' => -1
184
		], $actual->getData());
185
	}
186
187 View Code Duplication
	public function testProxyRequestExceptionHTTP() {
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...
188
		$testUrl = 'http://abc.def/foobar?123';
189
190
		$this->client->expects($this->once())
191
			->method('newClient')
192
			->will($this->returnValue($this->newClient));
193
		$this->newClient->expects($this->once())
194
			->method('get')
195
			->with($testUrl, [
196
				'stream' => true,
197
				'allow_redirects' => false,
198
			])
199
			->will($this->throwException(new RequestException('Exception Message foo bar 42',
200
				$this->exceptionRequest, $this->exceptionResponse)));
201
		$this->l10n->expects($this->once())
202
			->method('t')
203
			->with($this->equalTo('Error requesting resource on remote server'))
204
			->will($this->returnValue('translated string 1337'));
205
		$this->logger->expects($this->once())
206
			->method('debug')
207
			->with($this->equalTo('Exception Message foo bar 42'));
208
209
		$actual = $this->controller->proxy($testUrl);
210
211
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
212
		$this->assertEquals('422', $actual->getStatus());
213
		$this->assertEquals([
214
			'message' => 'translated string 1337',
215
			'proxy_code' => -2
216
		], $actual->getData());
217
	}
218
219 View Code Duplication
	public function testProxyRequestExceptionHTTPS() {
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...
220
		$testUrl = 'https://abc.def/foobar?123';
221
222
		$this->client->expects($this->once())
223
			->method('newClient')
224
			->will($this->returnValue($this->newClient));
225
		$this->newClient->expects($this->once())
226
			->method('get')
227
			->with($testUrl, [
228
				'stream' => true,
229
				'allow_redirects' => false,
230
			])
231
			->will($this->throwException(new RequestException('Exception Message foo bar 42',
232
				$this->exceptionRequest, $this->exceptionResponse)));
233
		$this->l10n->expects($this->once())
234
			->method('t')
235
			->with($this->equalTo('Error requesting resource on remote server. This could possibly be related to a certificate mismatch'))
236
			->will($this->returnValue('translated string 1337'));
237
		$this->logger->expects($this->once())
238
			->method('debug')
239
			->with($this->equalTo('Exception Message foo bar 42'));
240
241
		$actual = $this->controller->proxy($testUrl);
242
243
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
244
		$this->assertEquals('422', $actual->getStatus());
245
		$this->assertEquals([
246
			'message' => 'translated string 1337',
247
			'proxy_code' => -2
248
		], $actual->getData());
249
	}
250
251 View Code Duplication
	public function testProxyRedirect() {
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...
252
		$testUrl = 'http://abc.def/foobar?123';
253
254
		$this->client->expects($this->once())
255
			->method('newClient')
256
			->will($this->returnValue($this->newClient));
257
		$this->newClient->expects($this->at(0))
258
			->method('get')
259
			->with($testUrl, [
260
				'stream' => true,
261
				'allow_redirects' => false,
262
			])
263
			->will($this->returnValue($this->response0));
264
		$this->response0->expects($this->at(0))
265
			->method('getStatusCode')
266
			->with()
267
			->will($this->returnValue(301));
268
		$this->response0->expects($this->at(1))
269
			->method('getHeader')
270
			->with('Location')
271
			->will($this->returnValue('http://def.abc/foobar?456'));
272
		$this->newClient->expects($this->at(1))
273
			->method('get')
274
			->with('http://def.abc/foobar?456', [
275
				'stream' => true,
276
				'allow_redirects' => false,
277
			])
278
			->will($this->returnValue($this->response0));
279
		$this->response0->expects($this->at(2))
280
			->method('getStatusCode')
281
			->with()
282
			->will($this->returnValue(200));
283
284
		$actual = $this->controller->proxy($testUrl);
285
286
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
287
		$this->assertEquals([
288
			'proxy_code' => -4,
289
			'new_url' => 'http://def.abc/foobar?456',
290
		], $actual->getData());
291
	}
292
293
	public function testProxyRedirectNonPermanent() {
294
		$testUrl = 'http://abc.def/foobar?123';
295
296
		$this->client->expects($this->once())
297
			->method('newClient')
298
			->will($this->returnValue($this->newClient));
299
		$this->newClient->expects($this->at(0))
300
			->method('get')
301
			->with($testUrl, [
302
				'stream' => true,
303
				'allow_redirects' => false,
304
			])
305
			->will($this->returnValue($this->response0));
306
		$this->response0->expects($this->at(0))
307
			->method('getStatusCode')
308
			->with()
309
			->will($this->returnValue(307));
310
		$this->newClient->expects($this->at(1))
311
			->method('get')
312
			->with('http://abc.def/foobar?123' , [
313
				'stream' => true,
314
				'allow_redirects' => [
315
					'max' => 5,
316
				],
317
			])
318
			->will($this->returnValue($this->response1));
319
		$this->response1->expects($this->at(0))
320
			->method('getStatusCode')
321
			->with()
322
			->will($this->returnValue(200));
323
324
		$actual = $this->controller->proxy($testUrl);
325
326
		$this->assertInstanceOf('OCA\Calendar\Http\StreamResponse', $actual);
327
		$this->assertEquals('text/calendar', $actual->getHeaders()['Content-Type']);
328
	}
329
330
	public function testProxyMultipleRedirects() {
331
		$testUrl = 'http://abc.def/foobar?123';
332
333
		$this->client->expects($this->once())
334
			->method('newClient')
335
			->will($this->returnValue($this->newClient));
336
		$this->newClient->expects($this->at(0))
337
			->method('get')
338
			->with($testUrl, [
339
				'stream' => true,
340
				'allow_redirects' => false,
341
			])
342
			->will($this->returnValue($this->response0));
343
		$this->newClient->expects($this->at(1))
344
			->method('get')
345
			->with('http://def.abc/foobar?456' , [
346
				'stream' => true,
347
				'allow_redirects' => false,
348
			])
349
			->will($this->returnValue($this->response1));
350
		$this->newClient->expects($this->at(2))
351
			->method('get')
352
			->with('http://xyz.abc/foobar?789' , [
353
				'stream' => true,
354
				'allow_redirects' => false,
355
			])
356
			->will($this->returnValue($this->response2));
357
		$this->response0->expects($this->at(0))
358
			->method('getStatusCode')
359
			->with()
360
			->will($this->returnValue(301));
361
		$this->response0->expects($this->at(1))
362
			->method('getHeader')
363
			->with('Location')
364
			->will($this->returnValue('http://def.abc/foobar?456'));
365
		$this->response1->expects($this->at(0))
366
			->method('getStatusCode')
367
			->with()
368
			->will($this->returnValue(301));
369
		$this->response1->expects($this->at(1))
370
			->method('getHeader')
371
			->with('Location')
372
			->will($this->returnValue('http://xyz.abc/foobar?789'));
373
		$this->response2->expects($this->at(0))
374
			->method('getStatusCode')
375
			->with()
376
			->will($this->returnValue(200));
377
378
		$actual = $this->controller->proxy($testUrl);
379
380
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
381
		$this->assertEquals([
382
			'proxy_code' => -4,
383
			'new_url' => 'http://xyz.abc/foobar?789',
384
		], $actual->getData());
385
	}
386
387 View Code Duplication
	public function testProxyMultipleRedirectsNonPermanent() {
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...
388
		$testUrl = 'http://abc.def/foobar?123';
389
390
		$this->client->expects($this->once())
391
			->method('newClient')
392
			->will($this->returnValue($this->newClient));
393
		$this->newClient->expects($this->at(0))
394
			->method('get')
395
			->with($testUrl, [
396
				'stream' => true,
397
				'allow_redirects' => false,
398
			])
399
			->will($this->returnValue($this->response0));
400
		$this->newClient->expects($this->at(1))
401
			->method('get')
402
			->with('http://def.abc/foobar?456' , [
403
				'stream' => true,
404
				'allow_redirects' => false,
405
			])
406
			->will($this->returnValue($this->response1));
407
		$this->response0->expects($this->at(0))
408
			->method('getStatusCode')
409
			->with()
410
			->will($this->returnValue(301));
411
		$this->response0->expects($this->at(1))
412
			->method('getHeader')
413
			->with('Location')
414
			->will($this->returnValue('http://def.abc/foobar?456'));
415
		$this->response1->expects($this->at(0))
416
			->method('getStatusCode')
417
			->with()
418
			->will($this->returnValue(307));
419
420
		$actual = $this->controller->proxy($testUrl);
421
422
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
423
		$this->assertEquals([
424
			'proxy_code' => -4,
425
			'new_url' => 'http://def.abc/foobar?456',
426
		], $actual->getData());
427
	}
428
429
	public function testProxyAtMostFiveRedirects() {
430
		$testUrl = 'http://abc.def/foobar?123';
431
432
		$this->client->expects($this->once())
433
			->method('newClient')
434
			->will($this->returnValue($this->newClient));
435
		$this->newClient->expects($this->at(0))
436
			->method('get')
437
			->with($testUrl, [
438
				'stream' => true,
439
				'allow_redirects' => false,
440
			])
441
			->will($this->returnValue($this->response0));
442
		$this->newClient->expects($this->at(1))
443
			->method('get')
444
			->with('http://def.abc/foobar?456-0', [
445
				'stream' => true,
446
				'allow_redirects' => false,
447
			])
448
			->will($this->returnValue($this->response1));
449
		$this->newClient->expects($this->at(2))
450
			->method('get')
451
			->with('http://def.abc/foobar?456-1', [
452
				'stream' => true,
453
				'allow_redirects' => false,
454
			])
455
			->will($this->returnValue($this->response2));
456
		$this->newClient->expects($this->at(3))
457
			->method('get')
458
			->with('http://def.abc/foobar?456-2', [
459
				'stream' => true,
460
				'allow_redirects' => false,
461
			])
462
			->will($this->returnValue($this->response3));
463
		$this->newClient->expects($this->at(4))
464
			->method('get')
465
			->with('http://def.abc/foobar?456-3', [
466
				'stream' => true,
467
				'allow_redirects' => false,
468
			])
469
			->will($this->returnValue($this->response4));
470
		$this->newClient->expects($this->at(5))
471
			->method('get')
472
			->with('http://def.abc/foobar?456-4', [
473
				'stream' => true,
474
				'allow_redirects' => false,
475
			])
476
			->will($this->returnValue($this->response5));
477
		$this->response0->expects($this->at(0))
478
			->method('getStatusCode')
479
			->with()
480
			->will($this->returnValue(301));
481
		$this->response0->expects($this->at(1))
482
			->method('getHeader')
483
			->with('Location')
484
			->will($this->returnValue('http://def.abc/foobar?456-0'));
485
		$this->response1->expects($this->at(0))
486
			->method('getStatusCode')
487
			->with()
488
			->will($this->returnValue(301));
489
		$this->response1->expects($this->at(1))
490
			->method('getHeader')
491
			->with('Location')
492
			->will($this->returnValue('http://def.abc/foobar?456-1'));
493
		$this->response2->expects($this->at(0))
494
			->method('getStatusCode')
495
			->with()
496
			->will($this->returnValue(301));
497
		$this->response2->expects($this->at(1))
498
			->method('getHeader')
499
			->with('Location')
500
			->will($this->returnValue('http://def.abc/foobar?456-2'));
501
		$this->response3->expects($this->at(0))
502
			->method('getStatusCode')
503
			->with()
504
			->will($this->returnValue(301));
505
		$this->response3->expects($this->at(1))
506
			->method('getHeader')
507
			->with('Location')
508
			->will($this->returnValue('http://def.abc/foobar?456-3'));
509
		$this->response4->expects($this->at(0))
510
			->method('getStatusCode')
511
			->with()
512
			->will($this->returnValue(301));
513
		$this->response4->expects($this->at(1))
514
			->method('getHeader')
515
			->with('Location')
516
			->will($this->returnValue('http://def.abc/foobar?456-4'));
517
		$this->response5->expects($this->at(0))
518
			->method('getStatusCode')
519
			->with()
520
			->will($this->returnValue(301));
521
		$this->response5->expects($this->at(1))
522
			->method('getHeader')
523
			->with('Location')
524
			->will($this->returnValue('http://def.abc/foobar?456-5'));
525
		$this->l10n->expects($this->once())
526
			->method('t')
527
			->with($this->equalTo('Too many redirects. Aborting ...'))
528
			->will($this->returnValue('translated string 1337'));
529
		$this->newClient->expects($this->exactly(6))
530
			->method('get');
531
532
		$actual = $this->controller->proxy($testUrl);
533
534
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
535
		$this->assertEquals([
536
			'proxy_code' => -3,
537
			'message' => 'translated string 1337',
538
		], $actual->getData());
539
	}
540
541
542
}
543