File: RequestProxyTest.php

package info (click to toggle)
composer 2.8.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,752 kB
  • sloc: php: 77,487; makefile: 59; xml: 39
file content (189 lines) | stat: -rw-r--r-- 6,085 bytes parent folder | download | duplicates (2)
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
<?php declare(strict_types=1);

/*
 * This file is part of Composer.
 *
 * (c) Nils Adermann <naderman@naderman.de>
 *     Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Test\Util\Http;

use Composer\Util\Http\RequestProxy;
use Composer\Test\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class RequestProxyTest extends TestCase
{
    public function testFactoryNone(): void
    {
        $proxy = RequestProxy::none();

        $options = extension_loaded('curl') ? [CURLOPT_PROXY => ''] : [];
        self::assertSame($options, $proxy->getCurlOptions([]));
        self::assertNull($proxy->getContextOptions());
        self::assertSame('', $proxy->getStatus());
    }

    public function testFactoryNoProxy(): void
    {
        $proxy = RequestProxy::noProxy();

        $options = extension_loaded('curl') ? [CURLOPT_PROXY => ''] : [];
        self::assertSame($options, $proxy->getCurlOptions([]));
        self::assertNull($proxy->getContextOptions());
        self::assertSame('excluded by no_proxy', $proxy->getStatus());
    }

    /**
     * @param ?non-empty-string $url
     */
    #[DataProvider('dataSecure')]
    public function testIsSecure(?string $url, bool $expected): void
    {
        $proxy = new RequestProxy($url, null, null, null);
        self::assertSame($expected, $proxy->isSecure());
    }

    /**
     * @return array<string, array{0: ?non-empty-string, 1: bool}>
     */
    public static function dataSecure(): array
    {
        // url, expected
        return [
            'basic' => ['http://proxy.com:80', false],
            'secure' => ['https://proxy.com:443', true],
            'none' => [null, false],
        ];
    }

    public function testGetStatusThrowsOnBadFormatSpecifier(): void
    {
        $proxy = new RequestProxy('http://proxy.com:80', null, null, 'http://proxy.com:80');
        self::expectException('InvalidArgumentException');
        $proxy->getStatus('using proxy');
    }

    /**
     * @param ?non-empty-string $url
     */
    #[DataProvider('dataStatus')]
    public function testGetStatus(?string $url, ?string $format, string $expected): void
    {
        $proxy = new RequestProxy($url, null, null, $url);

        if ($format === null) {
            // try with and without optional param
            self::assertSame($expected, $proxy->getStatus());
            self::assertSame($expected, $proxy->getStatus($format));
        } else {
            self::assertSame($expected, $proxy->getStatus($format));
        }
    }

    /**
     * @return array<string, array{0: ?non-empty-string, 1: ?string, 2: string}>
     */
    public static function dataStatus(): array
    {
        $format = 'proxy (%s)';

        // url, format, expected
        return [
            'no-proxy' => [null, $format, ''],
            'null-format' => ['http://proxy.com:80', null, 'http://proxy.com:80'],
            'with-format' => ['http://proxy.com:80', $format, 'proxy (http://proxy.com:80)'],
        ];
    }

    /**
     * This test avoids HTTPS proxies so that it can be run on PHP < 7.3
     *
     * @requires extension curl
     *
     * @param ?non-empty-string $url
     * @param ?non-empty-string $auth
     * @param array<int, string|int> $expected
     */
    #[DataProvider('dataCurlOptions')]
    public function testGetCurlOptions(?string $url, ?string $auth, array $expected): void
    {
        $proxy = new RequestProxy($url, $auth, null, null);
        self::assertSame($expected, $proxy->getCurlOptions([]));
    }

    /**
     * @return list<array{0: ?string, 1: ?string, 2: array<int, string|int>}>
     */
    public static function dataCurlOptions(): array
    {
        // url, auth, expected
        return [
            [null, null, [CURLOPT_PROXY => '']],
            ['http://proxy.com:80', null,
                [
                    CURLOPT_PROXY => 'http://proxy.com:80',
                    CURLOPT_NOPROXY => '',
                ],
            ],
            ['http://proxy.com:80', 'user:p%40ss',
                [
                    CURLOPT_PROXY => 'http://proxy.com:80',
                    CURLOPT_NOPROXY => '',
                    CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
                    CURLOPT_PROXYUSERPWD => 'user:p%40ss',
                ],
            ],
        ];
    }

    /**
     * @requires PHP >= 7.3.0
     * @requires extension curl >= 7.52.0
     *
     * @param non-empty-string $url
     * @param ?non-empty-string $auth
     * @param array<string, string> $sslOptions
     * @param array<int, string|int> $expected
     */
    #[DataProvider('dataCurlSSLOptions')]
    public function testGetCurlOptionsWithSSL(string $url, ?string $auth, array $sslOptions, array $expected): void
    {
        $proxy = new RequestProxy($url, $auth, null, null);
        self::assertSame($expected, $proxy->getCurlOptions($sslOptions));
    }

    /**
     * @return list<array{0: string, 1: ?string, 2: array<string, string>, 3: array<int, string|int>}>
     */
    public static function dataCurlSSLOptions(): array
    {
        // for PHPStan on PHP < 7.3
        $caInfo = 10246; // CURLOPT_PROXY_CAINFO
        $caPath = 10247; // CURLOPT_PROXY_CAPATH

        // url, auth, sslOptions, expected
        return [
            ['https://proxy.com:443', null, ['cafile' => '/certs/bundle.pem'],
                [
                    CURLOPT_PROXY => 'https://proxy.com:443',
                    CURLOPT_NOPROXY => '',
                    $caInfo => '/certs/bundle.pem',
                ],
            ],
            ['https://proxy.com:443', 'user:p%40ss', ['capath' => '/certs'],
                [
                    CURLOPT_PROXY => 'https://proxy.com:443',
                    CURLOPT_NOPROXY => '',
                    CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
                    CURLOPT_PROXYUSERPWD => 'user:p%40ss',
                    $caPath => '/certs',
                ],
            ],
        ];
    }
}