summaryrefslogtreecommitdiff
path: root/sapi/fpm/tests/response.inc
blob: 9888ec1b83fe2e0028e4252e1937521d2d81d07b (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php

namespace FPM;

class Response
{
    const HEADER_SEPARATOR = "\r\n\r\n";

    /**
     * @var array
     */
    private $data;

    /**
     * @var string
     */
    private $rawData;

    /**
     * @var string
     */
    private $rawHeaders;

    /**
     * @var string
     */
    private $rawBody;

    /**
     * @var array
     */
    private $headers;

    /**
     * @var bool
     */
    private $valid;

    /**
     * @var bool
     */
    private $expectInvalid;

    /**
     * @param string|array|null $data
     * @param bool $expectInvalid
     */
    public function __construct($data = null, $expectInvalid = false)
    {
        if (!is_array($data)) {
            $data = [
                'response' => $data,
                'err_response' => null,
                'out_response' => $data,
            ];
        }

        $this->data = $data;
        $this->expectInvalid = $expectInvalid;
    }

    /**
     * @param mixed $body
     * @param string $contentType
     * @return Response
     */
    public function expectBody($body, $contentType = 'text/html')
    {
        if ($multiLine = is_array($body)) {
            $body = implode("\n", $body);
        }

        if (
            $this->checkIfValid() &&
            $this->checkDefaultHeaders($contentType) &&
            $body !== $this->rawBody
        ) {
            if ($multiLine) {
                $this->error(
                    "==> The expected body:\n$body\n" .
                    "==> does not match the actual body:\n$this->rawBody"
                );
            } else {
                $this->error(
                    "The expected body '$body' does not match actual body '$this->rawBody'"
                );
            }
        }

        return $this;
    }

    /**
     * @return Response
     */
    public function expectEmptyBody()
    {
        return $this->expectBody('');
    }

    /**
     * @param string $contentType
     * @return string|null
     */
    public function getBody($contentType = 'text/html')
    {
        if ($this->checkIfValid() && $this->checkDefaultHeaders($contentType)) {
            return $this->rawBody;
        }

        return null;
    }

    /**
     * Print raw body
     */
    public function dumpBody()
    {
        var_dump($this->getBody());
    }

    /**
     * Print raw body
     */
    public function printBody()
    {
        echo $this->getBody();
    }

    /**
     * Debug response output
     */
    public function debugOutput()
    {
        echo "-------------- RESPONSE: --------------\n";
        echo "OUT:\n";
        echo $this->data['out_response'];
        echo "ERR:\n";
        echo $this->data['err_response'];
        echo "---------------------------------------\n\n";
    }

    /**
     * @return string|null
     */
    public function getErrorData()
    {
        return $this->data['err_response'];
    }

    /**
     * Check if the response is valid and if not emit error message
     *
     * @return bool
     */
    private function checkIfValid()
    {
        if ($this->isValid()) {
            return true;
        }

        if (!$this->expectInvalid) {
            $this->error("The response is invalid: $this->rawData");
        }

        return false;
    }

    /**
     * @param string $contentType
     * @return bool
     */
    private function checkDefaultHeaders($contentType)
    {
        // check default headers
        return (
            $this->checkHeader('X-Powered-By', '|^PHP/7|', true) &&
            $this->checkHeader('Content-type', '|^' . $contentType . '(;\s?charset=\w+)?|', true)
        );
    }

    /**
     * @param string $name
     * @param string $value
     * @param bool $useRegex
     * @return bool
     */
    private function checkHeader(string $name, string $value, $useRegex = false)
    {
        $lcName = strtolower($name);
        $headers = $this->getHeaders();
        if (!isset($headers[$lcName])) {
            return $this->error("The header $name is not present");
        }
        $header = $headers[$lcName];

        if (!$useRegex) {
            if ($header === $value) {
                return true;
            }
            return $this->error("The header $name value '$header' is not the same as '$value'");
        }

        if (!preg_match($value, $header)) {
            return $this->error("The header $name value '$header' does not match RegExp '$value'");
        }

        return true;
    }

    /**
     * @return array|null
     */
    private function getHeaders()
    {
        if (!$this->isValid()) {
            return null;
        }

        if (is_array($this->headers)) {
            return $this->headers;
        }

        $headerRows = explode("\r\n", $this->rawHeaders);
        $headers = [];
        foreach ($headerRows as $headerRow) {
            $colonPosition = strpos($headerRow, ':');
            if ($colonPosition === false) {
                $this->error("Invalid header row (no colon): $headerRow");
            }
            $headers[strtolower(substr($headerRow, 0, $colonPosition))] = trim(
                substr($headerRow, $colonPosition + 1)
            );
        }

        return ($this->headers = $headers);
    }

    /**
     * @return bool
     */
    private function isValid()
    {
        if ($this->valid === null) {
            $this->processData();
        }

        return $this->valid;
    }

    /**
     * Process data and set validity and raw data
     */
    private function processData()
    {
        $this->rawData = $this->data['out_response'];
        $this->valid = (
            !is_null($this->rawData) &&
            strpos($this->rawData, self::HEADER_SEPARATOR)
        );
        if ($this->valid) {
            list ($this->rawHeaders, $this->rawBody) = array_map(
                'trim',
                explode(self::HEADER_SEPARATOR, $this->rawData)
            );
        }
    }

    /**
     * Emit error message
     *
     * @param string $message
     * @return bool
     */
    private function error($message)
    {
        echo "ERROR: $message\n";

        return false;
    }
}