1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Phiremock. |
4
|
|
|
* |
5
|
|
|
* Phiremock is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* Phiremock is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with Phiremock. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Server\Utils\Strategies; |
20
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Common\StringStream; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Expectation; |
23
|
|
|
use Mcustiel\Phiremock\Domain\HttpResponse; |
24
|
|
|
use Mcustiel\Phiremock\Server\Model\ScenarioStorage; |
25
|
|
|
use Mcustiel\Phiremock\Server\Utils\Strategies\Utils\RegexReplacer; |
26
|
|
|
use Psr\Http\Message\ResponseInterface; |
27
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
28
|
|
|
use Psr\Log\LoggerInterface; |
29
|
|
|
|
30
|
|
|
class RegexResponseStrategy extends AbstractResponse implements ResponseStrategyInterface |
31
|
|
|
{ |
32
|
|
|
/** @var RegexReplacer */ |
33
|
|
|
private $regexReplacer; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
ScenarioStorage $scenarioStorage, |
37
|
|
|
LoggerInterface $logger, |
38
|
|
|
RegexReplacer $regexReplacer |
39
|
|
|
) { |
40
|
|
|
parent::__construct($scenarioStorage, $logger); |
41
|
|
|
|
42
|
|
|
$this->regexReplacer = $regexReplacer; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function createResponse(Expectation $expectation, ResponseInterface $httpResponse, ServerRequestInterface $request): ResponseInterface |
46
|
|
|
{ |
47
|
|
|
$httpResponse = $this->getResponseWithReplacedBody( |
48
|
|
|
$expectation, |
49
|
|
|
$httpResponse, |
50
|
|
|
$request |
51
|
|
|
); |
52
|
|
|
$httpResponse = $this->getResponseWithReplacedHeaders( |
53
|
|
|
$expectation, |
54
|
|
|
$httpResponse, |
55
|
|
|
$request |
56
|
|
|
); |
57
|
|
|
/** @var HttpResponse $responseConfig */ |
58
|
|
|
$responseConfig = $expectation->getResponse(); |
59
|
|
|
$httpResponse = $this->getResponseWithStatusCode($responseConfig, $httpResponse); |
60
|
|
|
$this->processScenario($expectation); |
61
|
|
|
$this->processDelay($responseConfig); |
62
|
|
|
|
63
|
|
|
return $httpResponse; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function getResponseWithReplacedBody( |
67
|
|
|
Expectation $expectation, |
68
|
|
|
ResponseInterface $httpResponse, |
69
|
|
|
ServerRequestInterface $httpRequest |
70
|
|
|
): ResponseInterface { |
71
|
|
|
/** @var HttpResponse $responseConfig */ |
72
|
|
|
$responseConfig = $expectation->getResponse(); |
73
|
|
|
|
74
|
|
|
if ($responseConfig->hasBody()) { |
75
|
|
|
$bodyString = $responseConfig->getBody()->asString(); |
76
|
|
|
$bodyString = $this->regexReplacer->fillWithUrlMatches($expectation, $httpRequest, $bodyString); |
77
|
|
|
$bodyString = $this->regexReplacer->fillWithBodyMatches($expectation, $httpRequest, $bodyString); |
78
|
|
|
$httpResponse = $httpResponse->withBody(new StringStream($bodyString)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $httpResponse; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getResponseWithReplacedHeaders( |
85
|
|
|
Expectation $expectation, |
86
|
|
|
ResponseInterface $httpResponse, |
87
|
|
|
ServerRequestInterface $httpRequest |
88
|
|
|
): ResponseInterface { |
89
|
|
|
/** @var HttpResponse $responseConfig */ |
90
|
|
|
$responseConfig = $expectation->getResponse(); |
91
|
|
|
$headers = $responseConfig->getHeaders(); |
92
|
|
|
|
93
|
|
|
if ($headers === null || $headers->isEmpty()) { |
94
|
|
|
return $httpResponse; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($headers as $header) { |
98
|
|
|
$headerValue = $header->getValue()->asString(); |
99
|
|
|
$headerValue = $this->regexReplacer->fillWithUrlMatches($expectation, $httpRequest, $headerValue); |
100
|
|
|
$headerValue = $this->regexReplacer->fillWithBodyMatches($expectation, $httpRequest, $headerValue); |
101
|
|
|
$httpResponse = $httpResponse->withHeader($header->getName()->asString(), $headerValue); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $httpResponse; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|