1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
<?php
/**
* ownCloud - App Framework
*
* @author Bernhard Posselt
* @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\AppFramework;
use OC\AppFramework\Http\Request;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\DataResponse;
class ChildController extends Controller {
public function __construct($appName, $request) {
parent::__construct($appName, $request);
$this->registerResponder('tom', function ($respone) {
return 'hi';
});
}
public function custom($in) {
$this->registerResponder('json', function ($response) {
return new JSONResponse(array(strlen($response)));
});
return $in;
}
public function customDataResponse($in) {
$response = new DataResponse($in, 300);
$response->addHeader('test', 'something');
return $response;
}
};
class ControllerTest extends \Test\TestCase {
/**
* @var Controller
*/
private $controller;
private $app;
protected function setUp(){
parent::setUp();
$request = new Request(
[
'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
'post' => ['name' => 'Jane Doe', 'nickname' => 'Janey'],
'urlParams' => ['name' => 'Johnny Weissmüller'],
'files' => ['file' => 'filevalue'],
'env' => ['PATH' => 'daheim'],
'session' => ['sezession' => 'kein'],
'method' => 'hi',
],
$this->getMock('\OCP\Security\ISecureRandom'),
$this->getMock('\OCP\Security\ICrypto'),
$this->getMock('\OCP\IConfig')
);
$this->app = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
array('getAppName'), array('test'));
$this->app->expects($this->any())
->method('getAppName')
->will($this->returnValue('apptemplate_advanced'));
$this->controller = new ChildController($this->app, $request);
}
public function testParamsGet(){
$this->assertEquals('Johnny Weissmüller', $this->controller->params('name', 'Tarzan'));
}
public function testParamsGetDefault(){
$this->assertEquals('Tarzan', $this->controller->params('Ape Man', 'Tarzan'));
}
public function testParamsFile(){
$this->assertEquals('filevalue', $this->controller->params('file', 'filevalue'));
}
public function testGetUploadedFile(){
$this->assertEquals('filevalue', $this->controller->getUploadedFile('file'));
}
public function testGetUploadedFileDefault(){
$this->assertEquals('default', $this->controller->params('files', 'default'));
}
public function testGetParams(){
$params = array(
'name' => 'Johnny Weissmüller',
'nickname' => 'Janey',
);
$this->assertEquals($params, $this->controller->getParams());
}
public function testRender(){
$this->assertTrue($this->controller->render('') instanceof TemplateResponse);
}
public function testSetParams(){
$params = array('john' => 'foo');
$response = $this->controller->render('home', $params);
$this->assertEquals($params, $response->getParams());
}
public function testRenderHeaders(){
$headers = array('one', 'two');
$response = $this->controller->render('', array(), '', $headers);
$this->assertTrue(in_array($headers[0], $response->getHeaders()));
$this->assertTrue(in_array($headers[1], $response->getHeaders()));
}
public function testGetRequestMethod(){
$this->assertEquals('hi', $this->controller->method());
}
public function testGetEnvVariable(){
$this->assertEquals('daheim', $this->controller->env('PATH'));
}
/**
* @expectedException \DomainException
*/
public function testFormatResonseInvalidFormat() {
$this->controller->buildResponse(null, 'test');
}
public function testFormat() {
$response = $this->controller->buildResponse(array('hi'), 'json');
$this->assertEquals(array('hi'), $response->getData());
}
public function testFormatDataResponseJSON() {
$expectedHeaders = [
'test' => 'something',
'Cache-Control' => 'no-cache, must-revalidate',
'Content-Type' => 'application/json; charset=utf-8',
'Content-Security-Policy' => "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data:;font-src 'self';connect-src 'self';media-src 'self'",
];
$response = $this->controller->customDataResponse(array('hi'));
$response = $this->controller->buildResponse($response, 'json');
$this->assertEquals(array('hi'), $response->getData());
$this->assertEquals(300, $response->getStatus());
$this->assertEquals($expectedHeaders, $response->getHeaders());
}
public function testCustomFormatter() {
$response = $this->controller->custom('hi');
$response = $this->controller->buildResponse($response, 'json');
$this->assertEquals(array(2), $response->getData());
}
public function testDefaultResponderToJSON() {
$responder = $this->controller->getResponderByHTTPHeader('*/*');
$this->assertEquals('json', $responder);
}
public function testResponderAcceptHeaderParsed() {
$responder = $this->controller->getResponderByHTTPHeader(
'*/*, application/tom, application/json'
);
$this->assertEquals('tom', $responder);
}
public function testResponderAcceptHeaderParsedUpperCase() {
$responder = $this->controller->getResponderByHTTPHeader(
'*/*, apPlication/ToM, application/json'
);
$this->assertEquals('tom', $responder);
}
}
|