1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
use GuzzleHttp\Middleware;
21
use Psr\Http\Message\ResponseInterface;
22
use Symfony\Component\Console\Logger\ConsoleLogger;
23
use Symfony\Component\Console\Output\ConsoleOutput;
24

25
/**
26
 * A HTTP request task.
27
 * Making an HTTP request and try to match the response against an provided
28
 * regular expression.
29
 *
30
 * @package phing.tasks.ext
31
 * @author  Benjamin Schultz <bschultz@proqrent.de>
32
 * @since   2.4.1
33
 */
34
class HttpRequestTask extends HttpTask
35
{
36
    /**
37
     * Holds the regular expression that should match the response
38
     *
39
     * @var string
40
     */
41
    protected $responseRegex = '';
42

43
    /**
44
     * Holds the regular expression that should match the response code
45
     *
46
     * @var string
47
     */
48
    protected $responseCodeRegex = '';
49

50
    /**
51
     * Whether to enable detailed logging
52
     *
53
     * @var boolean
54
     */
55
    protected $verbose = false;
56

57
    /**
58
     * Holds additional post parameters for the request
59
     *
60
     * @var Parameter[]
61
     */
62
    protected $postParameters = [];
63

64
    /**
65
     * @var Regexp
66
     */
67
    private $regexp;
68

69
    /**
70
     * Sets the response regex
71
     *
72
     * @param string $regex
73
     */
74 2
    public function setResponseRegex($regex)
75
    {
76 2
        $this->responseRegex = $regex;
77
    }
78

79
    /**
80
     * Sets the response code regex
81
     *
82
     * @param string $regex
83
     */
84 2
    public function setResponseCodeRegex($regex)
85
    {
86 2
        $this->responseCodeRegex = $regex;
87
    }
88

89
    /**
90
     * Sets whether to enable detailed logging
91
     *
92
     * @param boolean $verbose
93
     */
94 0
    public function setVerbose($verbose)
95
    {
96 0
        $this->verbose = StringHelper::booleanValue($verbose);
97
    }
98

99
    /**
100
     * The setter for the method
101
     *
102
     * @param $method
103
     */
104 2
    public function setMethod($method)
105
    {
106 2
        $this->method = $method;
107
    }
108

109
    /**
110
     * Creates post body parameters for this request
111
     *
112
     * @return Parameter The created post parameter
113
     */
114 2
    public function createPostParameter()
115
    {
116 2
        $num = array_push($this->postParameters, new Parameter());
117

118 2
        return $this->postParameters[$num - 1];
119
    }
120

121
    /**
122
     * Load the necessary environment for running this task.
123
     *
124
     * @throws BuildException
125
     */
126 2
    public function init()
127
    {
128 2
        parent::init();
129

130 2
        $this->regexp = new Regexp();
131
    }
132

133
    /**
134
     * Creates, configures, and sends a request
135
     *
136
     * @param array $options
137
     * @return ResponseInterface
138
     */
139 2
    protected function request($options = [])
140
    {
141 2
        if ($this->method === 'POST') {
142 2
            $idx = ($this->isHeaderSet('content-type', 'application/json') ? 'json' : 'form_params');
143 2
            $options[$idx] = array_reduce(
144 2
                $this->postParameters,
145
                function ($carry, Parameter $postParameter) {
146 2
                    return $carry + [$postParameter->getName() => $postParameter->getValue()];
147 2
                },
148 2
                []
149
            );
150
        }
151

152 2
        if ($this->verbose) {
153 0
            self::getHandlerStack()->push(Middleware::log(new ConsoleLogger(new ConsoleOutput()), new \GuzzleHttp\MessageFormatter()));
154
        }
155

156 2
        return parent::request($options);
157
    }
158

159 2
    private function isHeaderSet($headerName, $headerValue)
160
    {
161 2
        $isSet = false;
162

163 2
        foreach ($this->headers as $header) {
164 0
            if ($header->getName() === $headerName && $header->getValue() === $headerValue) {
165 0
                $isSet = true;
166
            }
167
        }
168

169 2
        return $isSet;
170
    }
171

172
    /**
173
     * Checks whether response body or status-code matches the given regexp
174
     *
175
     * @param ResponseInterface $response
176
     * @return void
177
     * @throws RegexpException
178
     */
179 2
    protected function processResponse(ResponseInterface $response)
180
    {
181 2
        if ($this->responseRegex !== '') {
182 2
            $this->regexp->setPattern($this->responseRegex);
183

184 2
            if (!$this->regexp->matches($response->getBody())) {
185 2
                throw new BuildException('The received response body did not match the given regular expression');
186
            }
187

188 2
            $this->log('The response body matched the provided regex.');
189
        }
190

191 2
        if ($this->responseCodeRegex !== '') {
192 2
            $this->regexp->setPattern($this->responseCodeRegex);
193

194 2
            if (!$this->regexp->matches($response->getStatusCode())) {
195 0
                throw new BuildException('The received response status-code did not match the given regular expression');
196
            }
197

198 2
            $this->log('The response status-code matched the provided regex.');
199
        }
200
    }
201
}

Read our documentation on viewing source code .

Loading