Skip to content

Commit 8d05acd

Browse files
author
Yoann MOROCUTTI
committed
test(Unit/Service/Downloader/ReactDownloaderTest.php): Create multiple tests to check that stream context is correctly built when using http proxy
[repman-http-proxy]
1 parent 283ec36 commit 8d05acd

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

tests/Unit/Service/Downloader/ReactDownloaderTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Buddy\Repman\Tests\Unit\Service\Downloader;
66

7+
use Buddy\Repman\Kernel;
78
use Buddy\Repman\Service\Downloader\ReactDownloader;
89
use Munus\Control\Option;
910
use PHPUnit\Framework\TestCase;
@@ -55,4 +56,89 @@ public function testAsyncContent(): void
5556
});
5657
$downloader->run();
5758
}
59+
60+
/**
61+
* @throws \ReflectionException
62+
*/
63+
public function testStreamContext(): void
64+
{
65+
$_SERVER['HTTP_PROXY'] = $_SERVER['http_proxy'] = $_SERVER['HTTPS_PROXY'] = $_SERVER['https_proxy'] = null;
66+
67+
$downloader = new ReactDownloader();
68+
$createContextMethod = new \ReflectionMethod(ReactDownloader::class, 'createContext');
69+
$createContextMethod->setAccessible(true);
70+
71+
$context = $createContextMethod->invoke($downloader, 'https://repman.io');
72+
$options = stream_context_get_options($context);
73+
self::assertEquals(20, $options['http']['max_redirects']);
74+
self::assertEquals(1, $options['http']['follow_location']);
75+
self::assertEquals(
76+
sprintf(
77+
'User-Agent: Repman/%s (%s; %s; %s)',
78+
Kernel::REPMAN_VERSION,
79+
php_uname('s'),
80+
php_uname('r'),
81+
'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION
82+
),
83+
$options['http']['header'][0]
84+
);
85+
self::assertArrayNotHasKey('proxy', $options['http']);
86+
}
87+
88+
/**
89+
* @throws \ReflectionException
90+
*/
91+
public function testStreamContextHttpProxy(): void
92+
{
93+
$_SERVER['HTTP_PROXY'] = $_SERVER['http_proxy'] = $_SERVER['HTTPS_PROXY'] = $_SERVER['https_proxy'] = 'http://proxy.repman.io';
94+
95+
$downloader = new ReactDownloader();
96+
$createContextMethod = new \ReflectionMethod(ReactDownloader::class, 'createContext');
97+
$createContextMethod->setAccessible(true);
98+
99+
$context = $createContextMethod->invoke($downloader, 'https://repman.io');
100+
$options = stream_context_get_options($context);
101+
self::assertEquals(20, $options['http']['max_redirects']);
102+
self::assertEquals(1, $options['http']['follow_location']);
103+
self::assertEquals(
104+
sprintf(
105+
'User-Agent: Repman/%s (%s; %s; %s)',
106+
Kernel::REPMAN_VERSION,
107+
php_uname('s'),
108+
php_uname('r'),
109+
'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION
110+
),
111+
$options['http']['header'][0]
112+
);
113+
self::assertEquals('tcp://proxy.repman.io:80', $options['http']['proxy']);
114+
}
115+
116+
/**
117+
* @throws \ReflectionException
118+
*/
119+
public function testStreamContextNoProxy(): void
120+
{
121+
$_SERVER['HTTP_PROXY'] = $_SERVER['http_proxy'] = $_SERVER['HTTPS_PROXY'] = $_SERVER['https_proxy'] = 'http://proxy.repman.io';
122+
$_SERVER['NO_PROXY'] = $_SERVER['no_proxy'] = '.repman.io';
123+
124+
$downloader = new ReactDownloader();
125+
$createContextMethod = new \ReflectionMethod(ReactDownloader::class, 'createContext');
126+
$createContextMethod->setAccessible(true);
127+
128+
$context = $createContextMethod->invoke($downloader, 'https://repman.io');
129+
$options = stream_context_get_options($context);
130+
self::assertEquals(20, $options['http']['max_redirects']);
131+
self::assertEquals(1, $options['http']['follow_location']);
132+
self::assertEquals(
133+
sprintf(
134+
'User-Agent: Repman/%s (%s; %s; %s)',
135+
Kernel::REPMAN_VERSION,
136+
php_uname('s'),
137+
php_uname('r'),
138+
'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION
139+
),
140+
$options['http']['header'][0]
141+
);
142+
self::assertArrayNotHasKey('proxy', $options['http']);
143+
}
58144
}

0 commit comments

Comments
 (0)