|
4 | 4 |
|
5 | 5 | namespace Buddy\Repman\Tests\Unit\Service\Downloader;
|
6 | 6 |
|
| 7 | +use Buddy\Repman\Kernel; |
7 | 8 | use Buddy\Repman\Service\Downloader\ReactDownloader;
|
8 | 9 | use Munus\Control\Option;
|
9 | 10 | use PHPUnit\Framework\TestCase;
|
@@ -55,4 +56,89 @@ public function testAsyncContent(): void
|
55 | 56 | });
|
56 | 57 | $downloader->run();
|
57 | 58 | }
|
| 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 | + } |
58 | 144 | }
|
0 commit comments