File: EchoHandler.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (94 lines) | stat: -rw-r--r-- 2,448 bytes parent folder | download
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
<?php

namespace MediaWiki\Tests\Rest\Handler;

use LogicException;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\RequestInterface;
use Wikimedia\ParamValidator\ParamValidator;

/**
 * Test mock for asserting parameter processing and validation in Handler and Router.
 */
class EchoHandler extends Handler {
	/** @var bool */
	private $postValidationSetupCalled = false;

	public function execute() {
		if ( !$this->postValidationSetupCalled ) {
			throw new LogicException( 'postValidationSetup was not called' );
		}

		$request = $this->getRequest();
		return [
			'method' => $request->getMethod(),
			'uri' => $request->getUri(),
			'protocolVersion' => $request->getProtocolVersion(),
			'body' => $request->getBody()->getContents(),
			'serverParams' => $request->getServerParams(),
			'cookieParams' => $request->getCookieParams(),
			'queryParams' => $request->getQueryParams(),
			'postParams' => $request->getPostParams(),
			'pathParams' => $request->getPathParams(),
			'headers' => $request->getHeaders(),
			'parsedBody' => $request->getParsedBody(),
			'validatedBody' => $this->getValidatedBody(),
			'validatedParams' => $this->getValidatedParams(),
		];
	}

	public function getBodyParamSettings(): array {
		if ( $this->getConfig()['postParam'] ?? false ) {
			// Don't mix "post" and "body" params, it confuses
			// Validator::detectExtraneousBodyFields()
			return [];
		}

		return [
			'bodyParam' => [
				self::PARAM_SOURCE => 'body',
				ParamValidator::PARAM_TYPE => 'string',
				ParamValidator::PARAM_REQUIRED => false
			],
		];
	}

	public function getParamSettings() {
		$paramSettings = [
			'q' => [
				self::PARAM_SOURCE => 'query',
				ParamValidator::PARAM_TYPE => 'string',
			],
			'pathParam' => [
				self::PARAM_SOURCE => 'path',
				ParamValidator::PARAM_TYPE => 'string',
			],
		];

		if ( $this->getConfig()['postParam'] ?? false ) {
			// Deprecated, will trigger a warning!
			$paramSettings['postParam'] = [
				self::PARAM_SOURCE => 'post',
				ParamValidator::PARAM_TYPE => 'string',
			];
		}

		return $paramSettings;
	}

	protected function postValidationSetup() {
		$this->postValidationSetupCalled = true;
	}

	public function getSupportedRequestTypes(): array {
		return [
			RequestInterface::JSON_CONTENT_TYPE,
			RequestInterface::FORM_URLENCODED_CONTENT_TYPE,
			RequestInterface::MULTIPART_FORM_DATA_CONTENT_TYPE,
		];
	}

	public function needsWriteAccess() {
		return false;
	}
}