Completed
Pull Request — master (#178)
by Georg
14:05 queued 11:55
created

ProxyControllerTest::testProxyClientException()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
nc 1
nop 0
dl 0
loc 33
rs 8.8571
c 1
b 0
f 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 $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
	public function testProxyClientException() {
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->l10n->expects($this->once())
112
			->method('t')
113
			->with($this->equalTo('The remote server did not give us access to the calendar (HTTP {%s} error)', '403'))
114
			->will($this->returnValue('translated string 1337'));
115
		$this->logger->expects($this->once())
116
			->method('debug')
117
			->with($this->equalTo('Exception Message foo bar 42'));
118
119
		$actual = $this->controller->proxy($testUrl);
120
121
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
122
		$this->assertEquals('422', $actual->getStatus());
123
		$this->assertEquals([
124
			'message' => 'translated string 1337',
125
			'proxy_code' => 403
126
		], $actual->getData());
127
	}
128
129 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...
130
		$testUrl = 'http://abc.def/foobar?123';
131
132
		$this->client->expects($this->once())
133
			->method('newClient')
134
			->will($this->returnValue($this->newClient));
135
		$this->newClient->expects($this->once())
136
			->method('get')
137
			->with($testUrl, [
138
				'stream' => true,
139
			])
140
			->will($this->throwException(new ConnectException('Exception Message foo bar 42',
141
				$this->exceptionRequest, $this->exceptionResponse)));
142
		$this->l10n->expects($this->once())
143
			->method('t')
144
			->with($this->equalTo('Error connecting to remote server'))
145
			->will($this->returnValue('translated string 1337'));
146
		$this->logger->expects($this->once())
147
			->method('debug')
148
			->with($this->equalTo('Exception Message foo bar 42'));
149
150
		$actual = $this->controller->proxy($testUrl);
151
152
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
153
		$this->assertEquals('422', $actual->getStatus());
154
		$this->assertEquals([
155
			'message' => 'translated string 1337',
156
			'proxy_code' => -1
157
		], $actual->getData());
158
	}
159
160 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...
161
		$testUrl = 'http://abc.def/foobar?123';
162
163
		$this->client->expects($this->once())
164
			->method('newClient')
165
			->will($this->returnValue($this->newClient));
166
		$this->newClient->expects($this->once())
167
			->method('get')
168
			->with($testUrl, [
169
				'stream' => true,
170
			])
171
			->will($this->throwException(new RequestException('Exception Message foo bar 42',
172
				$this->exceptionRequest, $this->exceptionResponse)));
173
		$this->l10n->expects($this->once())
174
			->method('t')
175
			->with($this->equalTo('Error requesting resource on remote server'))
176
			->will($this->returnValue('translated string 1337'));
177
		$this->logger->expects($this->once())
178
			->method('debug')
179
			->with($this->equalTo('Exception Message foo bar 42'));
180
181
		$actual = $this->controller->proxy($testUrl);
182
183
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
184
		$this->assertEquals('422', $actual->getStatus());
185
		$this->assertEquals([
186
			'message' => 'translated string 1337',
187
			'proxy_code' => -2
188
		], $actual->getData());
189
	}
190
191 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...
192
		$testUrl = 'https://abc.def/foobar?123';
193
194
		$this->client->expects($this->once())
195
			->method('newClient')
196
			->will($this->returnValue($this->newClient));
197
		$this->newClient->expects($this->once())
198
			->method('get')
199
			->with($testUrl, [
200
				'stream' => true,
201
			])
202
			->will($this->throwException(new RequestException('Exception Message foo bar 42',
203
				$this->exceptionRequest, $this->exceptionResponse)));
204
		$this->l10n->expects($this->once())
205
			->method('t')
206
			->with($this->equalTo('Error requesting resource on remote server. This could possible be related to a certificate mismatch'))
207
			->will($this->returnValue('translated string 1337'));
208
		$this->logger->expects($this->once())
209
			->method('debug')
210
			->with($this->equalTo('Exception Message foo bar 42'));
211
212
		$actual = $this->controller->proxy($testUrl);
213
214
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
215
		$this->assertEquals('422', $actual->getStatus());
216
		$this->assertEquals([
217
			'message' => 'translated string 1337',
218
			'proxy_code' => -2
219
		], $actual->getData());
220
	}
221
}
222