1<?php
2
3/**
4 * This file is part of the Phalcon Framework.
5 *
6 * (c) Phalcon Team <team@phalcon.io>
7 *
8 * For the full copyright and license information, please view the LICENSE.txt
9 * file that was distributed with this source code.
10 */
11
12declare(strict_types=1);
13
14namespace Phalcon\Test\Unit\Http\Message\Request;
15
16use Codeception\Example;
17use Phalcon\Http\Message\Exception\InvalidArgumentException;
18use Phalcon\Http\Message\Request;
19use UnitTester;
20
21class WithHeaderCest
22{
23    /**
24     * Tests Phalcon\Http\Message\Request :: withHeader()
25     *
26     * @author Phalcon Team <team@phalcon.io>
27     * @since  2019-02-10
28     */
29    public function httpMessageRequestWithHeader(UnitTester $I)
30    {
31        $I->wantToTest('Http\Message\Request - withHeader()');
32
33        $data = [
34            'Accept' => ['text/html'],
35        ];
36
37        $request = new Request('GET', null, 'php://memory', $data);
38
39        $newInstance = $request->withHeader(
40            'Cache-Control',
41            [
42                'max-age=0',
43            ]
44        );
45
46        $I->assertNotEquals($request, $newInstance);
47
48        $expected = [
49            'Accept' => ['text/html'],
50        ];
51
52        $I->assertEquals(
53            $expected,
54            $request->getHeaders()
55        );
56
57        $expected = [
58            'Accept'        => ['text/html'],
59            'Cache-Control' => ['max-age=0'],
60        ];
61
62        $I->assertEquals(
63            $expected,
64            $newInstance->getHeaders()
65        );
66    }
67
68    /**
69     * Tests Phalcon\Http\Message\Request :: withHeader() - exception
70     *
71     * @author Phalcon Team <team@phalcon.io>
72     * @since  2019-02-10
73     */
74    public function httpMessageRequestWithHeaderException(UnitTester $I)
75    {
76        $I->wantToTest('Http\Message\Request - withHeader() value');
77
78        $I->expectThrowable(
79            new InvalidArgumentException(
80                'Invalid header name Cache Control'
81            ),
82            function () {
83                $request = new Request();
84
85                $newInstance = $request->withHeader(
86                    'Cache Control',
87                    [
88                        'max-age=0',
89                    ]
90                );
91            }
92        );
93    }
94
95    /**
96     * Tests Phalcon\Http\Message\Request :: withHeader() - exception value
97     *
98     * @dataProvider getExamples
99     *
100     * @author       Phalcon Team <team@phalcon.io>
101     * @since        2019-02-10
102     */
103    public function httpMessageRequestWithHeaderExceptionValue(UnitTester $I, Example $example)
104    {
105        $I->wantToTest('Http\Message\Request - withHeader() - exception value ' . $example[0]);
106
107        $I->expectThrowable(
108            new InvalidArgumentException(
109                'Invalid header value'
110            ),
111            function () use ($example) {
112                $request = new Request();
113
114                $newInstance = $request->withHeader(
115                    'Cache-Control',
116                    [
117                        $example[1],
118                    ]
119                );
120            }
121        );
122    }
123
124
125    private function getExamples(): array
126    {
127        return [
128            ['not numeric or string', true],
129            ['invalid\r\n', "some \r\n"],
130            ['invalid\r', "some \r"],
131            ['invalid\n', "some \n"],
132        ];
133    }
134}
135