Skip to content

Commit

Permalink
test(Unit/Service/Downloader/ReactDownloaderTest.php): Create multipl…
Browse files Browse the repository at this point in the history
…e tests to check that stream context is correctly built when using http proxy

[repman-http-proxy]
  • Loading branch information
Yoann MOROCUTTI committed Jun 7, 2023
1 parent 283ec36 commit 8d05acd
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/Unit/Service/Downloader/ReactDownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

use Buddy\Repman\Kernel;
use Buddy\Repman\Service\Downloader\ReactDownloader;
use Munus\Control\Option;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -55,4 +56,89 @@ public function testAsyncContent(): void
});
$downloader->run();
}

/**
* @throws \ReflectionException
*/
public function testStreamContext(): void
{
$_SERVER['HTTP_PROXY'] = $_SERVER['http_proxy'] = $_SERVER['HTTPS_PROXY'] = $_SERVER['https_proxy'] = null;

$downloader = new ReactDownloader();
$createContextMethod = new \ReflectionMethod(ReactDownloader::class, 'createContext');
$createContextMethod->setAccessible(true);

$context = $createContextMethod->invoke($downloader, 'https://repman.io');
$options = stream_context_get_options($context);
self::assertEquals(20, $options['http']['max_redirects']);
self::assertEquals(1, $options['http']['follow_location']);
self::assertEquals(
sprintf(
'User-Agent: Repman/%s (%s; %s; %s)',
Kernel::REPMAN_VERSION,
php_uname('s'),
php_uname('r'),
'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION
),
$options['http']['header'][0]
);
self::assertArrayNotHasKey('proxy', $options['http']);
}

/**
* @throws \ReflectionException
*/
public function testStreamContextHttpProxy(): void
{
$_SERVER['HTTP_PROXY'] = $_SERVER['http_proxy'] = $_SERVER['HTTPS_PROXY'] = $_SERVER['https_proxy'] = 'http://proxy.repman.io';

$downloader = new ReactDownloader();
$createContextMethod = new \ReflectionMethod(ReactDownloader::class, 'createContext');
$createContextMethod->setAccessible(true);

$context = $createContextMethod->invoke($downloader, 'https://repman.io');
$options = stream_context_get_options($context);
self::assertEquals(20, $options['http']['max_redirects']);
self::assertEquals(1, $options['http']['follow_location']);
self::assertEquals(
sprintf(
'User-Agent: Repman/%s (%s; %s; %s)',
Kernel::REPMAN_VERSION,
php_uname('s'),
php_uname('r'),
'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION
),
$options['http']['header'][0]
);
self::assertEquals('tcp://proxy.repman.io:80', $options['http']['proxy']);
}

/**
* @throws \ReflectionException
*/
public function testStreamContextNoProxy(): void
{
$_SERVER['HTTP_PROXY'] = $_SERVER['http_proxy'] = $_SERVER['HTTPS_PROXY'] = $_SERVER['https_proxy'] = 'http://proxy.repman.io';
$_SERVER['NO_PROXY'] = $_SERVER['no_proxy'] = '.repman.io';

$downloader = new ReactDownloader();
$createContextMethod = new \ReflectionMethod(ReactDownloader::class, 'createContext');
$createContextMethod->setAccessible(true);

$context = $createContextMethod->invoke($downloader, 'https://repman.io');
$options = stream_context_get_options($context);
self::assertEquals(20, $options['http']['max_redirects']);
self::assertEquals(1, $options['http']['follow_location']);
self::assertEquals(
sprintf(
'User-Agent: Repman/%s (%s; %s; %s)',
Kernel::REPMAN_VERSION,
php_uname('s'),
php_uname('r'),
'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION
),
$options['http']['header'][0]
);
self::assertArrayNotHasKey('proxy', $options['http']);
}
}

0 comments on commit 8d05acd

Please sign in to comment.