| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\router;
|
|
|
18 |
|
|
|
19 |
use core\di;
|
|
|
20 |
use core\exception\access_denied_exception;
|
|
|
21 |
use core\exception\response_aware_exception;
|
|
|
22 |
use core\router\schema\response\payload_response;
|
|
|
23 |
use core\router\schema\response\view_response;
|
|
|
24 |
use GuzzleHttp\Psr7\Request;
|
|
|
25 |
use GuzzleHttp\Psr7\Response;
|
|
|
26 |
use GuzzleHttp\Psr7\ServerRequest;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Tests for \core\router\response_handler.
|
|
|
30 |
*
|
|
|
31 |
* @package core
|
|
|
32 |
* @category test
|
|
|
33 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
* @covers \core\router\response_handler
|
|
|
36 |
*/
|
|
|
37 |
final class response_handler_test extends \advanced_testcase {
|
|
|
38 |
public function test_standardise_response_from_response(): void {
|
|
|
39 |
$response = new Response();
|
|
|
40 |
|
|
|
41 |
$handler = di::get(response_handler::class);
|
|
|
42 |
|
|
|
43 |
$result = $handler->standardise_response($response);
|
|
|
44 |
$this->assertEquals($response, $result);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
public function test_standardise_response_from_payload_response(): void {
|
|
|
48 |
$request = new ServerRequest('GET', 'http://example.com');
|
|
|
49 |
$payload = new payload_response(['key' => 'value'], $request);
|
|
|
50 |
|
|
|
51 |
$handler = di::get(response_handler::class);
|
|
|
52 |
|
|
|
53 |
$result = $handler->standardise_response($payload);
|
|
|
54 |
$this->assertInstanceOf(Response::class, $result);
|
|
|
55 |
|
|
|
56 |
// The body should be json and contain the same data.
|
|
|
57 |
$value = json_decode($result->getBody());
|
|
|
58 |
$this->assertSame(
|
|
|
59 |
['key' => 'value'],
|
|
|
60 |
(array) $value,
|
|
|
61 |
);
|
|
|
62 |
|
|
|
63 |
// The content type should be application/json.
|
|
|
64 |
$this->assertStringContainsString('application/json', $result->getHeaderLine('Content-Type'));
|
|
|
65 |
|
|
|
66 |
// The status code should be 200.
|
|
|
67 |
$this->assertEquals(200, $result->getStatusCode());
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public function test_standardise_response_from_payload_response_and_response(): void {
|
|
|
71 |
$request = new ServerRequest('GET', 'http://example.com');
|
|
|
72 |
$response = new Response();
|
|
|
73 |
|
|
|
74 |
// Add some test headers.
|
|
|
75 |
$response = $response->withAddedHeader('Content-Type', 'text/plain')
|
|
|
76 |
->withAddedHeader('X-Example', 'example-value');
|
|
|
77 |
|
|
|
78 |
$payload = new payload_response(['key' => 'value'], $request, $response);
|
|
|
79 |
|
|
|
80 |
$handler = di::get(response_handler::class);
|
|
|
81 |
|
|
|
82 |
$result = $handler->standardise_response($payload);
|
|
|
83 |
$this->assertInstanceOf(Response::class, $result);
|
|
|
84 |
|
|
|
85 |
// The body should be json and contain the same data.
|
|
|
86 |
$value = json_decode($result->getBody());
|
|
|
87 |
$this->assertSame(
|
|
|
88 |
['key' => 'value'],
|
|
|
89 |
(array) $value,
|
|
|
90 |
);
|
|
|
91 |
|
|
|
92 |
// The content type should be application/json and the text/plain header should have been replaced.
|
|
|
93 |
$this->assertStringContainsString('application/json', $result->getHeaderLine('Content-Type'));
|
|
|
94 |
|
|
|
95 |
// The status code should be 200.
|
|
|
96 |
$this->assertEquals(200, $result->getStatusCode());
|
|
|
97 |
|
|
|
98 |
// The X-Example header should be present.
|
|
|
99 |
$this->assertEquals('example-value', $result->getHeaderLine('X-Example'));
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public function test_standardise_response_from_view_response(): void {
|
|
|
103 |
global $OUTPUT;
|
|
|
104 |
|
|
|
105 |
$request = new ServerRequest('GET', 'http://example.com');
|
|
|
106 |
|
|
|
107 |
// Add some test headers.
|
|
|
108 |
$initialresponse = new Response();
|
|
|
109 |
$initialresponse = $initialresponse->withAddedHeader('Content-Type', 'application/json')
|
|
|
110 |
->withAddedHeader('X-Example', 'example-value');
|
|
|
111 |
|
|
|
112 |
$response = new view_response(
|
|
|
113 |
template: 'core/welcome',
|
|
|
114 |
parameters: [
|
|
|
115 |
'welcomemessage' => 'Hello, everybody!',
|
|
|
116 |
],
|
|
|
117 |
request: $request,
|
|
|
118 |
response: $initialresponse,
|
|
|
119 |
);
|
|
|
120 |
|
|
|
121 |
$handler = di::get(response_handler::class);
|
|
|
122 |
|
|
|
123 |
$result = $handler->standardise_response($response);
|
|
|
124 |
$this->assertInstanceOf(Response::class, $result);
|
|
|
125 |
|
|
|
126 |
// The content type should be application/json and the text/plain header should have been replaced.
|
|
|
127 |
$this->assertStringContainsString('text/html', $result->getHeaderLine('Content-Type'));
|
|
|
128 |
|
|
|
129 |
// The status code should be 200.
|
|
|
130 |
$this->assertEquals(200, $result->getStatusCode());
|
|
|
131 |
|
|
|
132 |
// The X-Example header should be present.
|
|
|
133 |
$this->assertEquals('example-value', $result->getHeaderLine('X-Example'));
|
|
|
134 |
|
|
|
135 |
$body = (string) $result->getBody();
|
|
|
136 |
$this->assertStringContainsString('Hello, everybody!', $body);
|
|
|
137 |
$this->assertEquals(
|
|
|
138 |
$OUTPUT->render_from_template('core/welcome', ['welcomemessage' => 'Hello, everybody!']),
|
|
|
139 |
$body,
|
|
|
140 |
);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Test that the response handler can get a response from an exception.
|
|
|
145 |
*/
|
|
|
146 |
public function test_get_response_from_exception(): void {
|
|
|
147 |
$request = new ServerRequest('GET', 'http://example.com');
|
|
|
148 |
$exception = new \Exception('Test exception');
|
|
|
149 |
|
|
|
150 |
$handler = di::get(response_handler::class);
|
|
|
151 |
|
|
|
152 |
$result = $handler->get_response_from_exception($request, $exception);
|
|
|
153 |
$this->assertInstanceOf(Response::class, $result);
|
|
|
154 |
|
|
|
155 |
// The body should be json and contain the exception message.
|
|
|
156 |
$value = json_decode($result->getBody(), true);
|
|
|
157 |
$this->assertArrayHasKey('message', (array) $value);
|
|
|
158 |
$this->assertArrayHasKey('stacktrace', (array) $value);
|
|
|
159 |
|
|
|
160 |
$this->assertEquals(
|
|
|
161 |
'Test exception',
|
|
|
162 |
$value['message'],
|
|
|
163 |
);
|
|
|
164 |
|
|
|
165 |
// The content type should be application/json.
|
|
|
166 |
$this->assertStringContainsString('application/json', $result->getHeaderLine('Content-Type'));
|
|
|
167 |
|
|
|
168 |
// The status code should be 500.
|
|
|
169 |
$this->assertEquals(500, $result->getStatusCode());
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Test that the response handler can get a response from an exception.
|
|
|
174 |
*/
|
|
|
175 |
public function test_get_response_from_response_aware_exception(): void {
|
|
|
176 |
$request = new ServerRequest('GET', 'http://example.com');
|
|
|
177 |
|
|
|
178 |
$exception = new access_denied_exception('Test exception');
|
|
|
179 |
$handler = di::get(response_handler::class);
|
|
|
180 |
|
|
|
181 |
$result = $handler->get_response_from_exception($request, $exception);
|
|
|
182 |
$this->assertInstanceOf(Response::class, $result);
|
|
|
183 |
|
|
|
184 |
// The body should be json and contain the exception message.
|
|
|
185 |
$value = json_decode($result->getBody(), true);
|
|
|
186 |
$this->assertArrayHasKey('message', (array) $value);
|
|
|
187 |
$this->assertArrayHasKey('stacktrace', (array) $value);
|
|
|
188 |
|
|
|
189 |
// The content type should be application/json.
|
|
|
190 |
$this->assertStringContainsString('application/json', $result->getHeaderLine('Content-Type'));
|
|
|
191 |
|
|
|
192 |
// The status code should be 500.
|
|
|
193 |
$this->assertEquals(403, $result->getStatusCode());
|
|
|
194 |
}
|
|
|
195 |
}
|