Completed
Pull Request — master (#178)
by Georg
04:24
created

ProxyControllerTest::testProxyException()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 2
b 0
f 1
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 $response;
38
	private $exceptionRequest;
39
	private $exceptionResponse;
40
41
	private $controller;
42
43
	public function setUp() {
44
		$this->appName = 'calendar';
45
		$this->request = $this->getMockBuilder('\OCP\IRequest')
46
			->disableOriginalConstructor()
47
			->getMock();
48
		$this->client = $this->getMockBuilder('\OCP\Http\Client\IClientService')
49
			->disableOriginalConstructor()
50
			->getMock();
51
		$this->l10n = $this->getMockBuilder('\OCP\IL10N')
52
			->disableOriginalConstructor()
53
			->getMock();
54
		$this->logger = $this->getMockBuilder('\OCP\ILogger')
55
			->disableOriginalConstructor()
56
			->getMock();
57
58
		$this->newClient = $this->getMockBuilder('\OCP\Http\Client\IClient')
59
			->disableOriginalConstructor()
60
			->getMock();
61
		$this->response = $this->getMockBuilder('\OCP\Http\Client\IResponse')
62
			->disableOriginalConstructor()
63
			->getMock();
64
65
		$this->exceptionRequest = $this->getMockBuilder('GuzzleHttp\Message\RequestInterface')
66
			->disableOriginalConstructor()
67
			->getMock();
68
		$this->exceptionResponse = $this->getMockBuilder('GuzzleHttp\Message\ResponseInterface')
69
			->disableOriginalConstructor()
70
			->getMock();
71
72
		$this->controller = new ProxyController($this->appName, $this->request,
73
			$this->client, $this->l10n, $this->logger);
74
	}
75
76
	public function testProxy() {
77
		$testUrl = 'http://abc.def/foobar?123';
78
79
		$this->client->expects($this->once())
80
			->method('newClient')
81
			->will($this->returnValue($this->newClient));
82
		$this->newClient->expects($this->once())
83
			->method('get')
84
			->with($testUrl, [
85
				'stream' => true,
86
			])
87
			->will($this->returnValue($this->response));
88
89
		$actual = $this->controller->proxy($testUrl);
90
91
		$this->assertInstanceOf('OCA\Calendar\Http\StreamResponse', $actual);
92
		$this->assertEquals('text/calendar', $actual->getHeaders()['Content-Type']);
93
	}
94
95 View Code Duplication
	public function testProxyClientException() {
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...
96
		$testUrl = 'http://abc.def/foobar?123';
97
98
		$this->client->expects($this->once())
99
			->method('newClient')
100
			->will($this->returnValue($this->newClient));
101
		$this->newClient->expects($this->once())
102
			->method('get')
103
			->with($testUrl, [
104
				'stream' => true,
105
			])
106
			->will($this->throwException(new ClientException('Exception Message foo bar 42',
107
				$this->exceptionRequest, $this->exceptionResponse)));
108
		$this->exceptionResponse->expects($this->once())
109
			->method('getStatusCode')
110
			->will($this->returnValue(403));
111
		$this->logger->expects($this->once())
112
			->method('debug')
113
			->with($this->equalTo('Exception Message foo bar 42'));
114
115
		$actual = $this->controller->proxy($testUrl);
116
117
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
118
		$this->assertEquals('422', $actual->getStatus());
119
		$this->assertEquals([
120
			'message' => 'Exception Message foo bar 42',
121
			'proxy_code' => 403
122
		], $actual->getData());
123
	}
124
125 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...
126
		$testUrl = 'http://abc.def/foobar?123';
127
128
		$this->client->expects($this->once())
129
			->method('newClient')
130
			->will($this->returnValue($this->newClient));
131
		$this->newClient->expects($this->once())
132
			->method('get')
133
			->with($testUrl, [
134
				'stream' => true,
135
			])
136
			->will($this->throwException(new ConnectException('Exception Message foo bar 42',
137
				$this->exceptionRequest, $this->exceptionResponse)));
138
		$this->l10n->expects($this->once())
139
			->method('t')
140
			->with($this->equalTo('Error connecting to remote server'))
141
			->will($this->returnValue('translated string 1337'));
142
		$this->logger->expects($this->once())
143
			->method('debug')
144
			->with($this->equalTo('Exception Message foo bar 42'));
145
146
		$actual = $this->controller->proxy($testUrl);
147
148
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
149
		$this->assertEquals('422', $actual->getStatus());
150
		$this->assertEquals([
151
			'message' => 'translated string 1337',
152
			'proxy_code' => -1
153
		], $actual->getData());
154
	}
155
156 View Code Duplication
	public function testProxyRequestException() {
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...
157
		$testUrl = 'http://abc.def/foobar?123';
158
159
		$this->client->expects($this->once())
160
			->method('newClient')
161
			->will($this->returnValue($this->newClient));
162
		$this->newClient->expects($this->once())
163
			->method('get')
164
			->with($testUrl, [
165
				'stream' => true,
166
			])
167
			->will($this->throwException(new RequestException('Exception Message foo bar 42',
168
				$this->exceptionRequest, $this->exceptionResponse)));
169
		$this->l10n->expects($this->once())
170
			->method('t')
171
			->with($this->equalTo('Error requesting resource on 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' => -2
184
		], $actual->getData());
185
	}
186
}
187