Skip to content

Commit

Permalink
MDL-72203 curl: Remove duplicate unit tests for emulated redirects
Browse files Browse the repository at this point in the history
Before, we had each redirect test duplicated: one for the native
redirects via native cURL, second for our emulated implementation. Now
all redirects are always emulated so there is no need to have them
tested twice.
  • Loading branch information
mudrd8mz authored and mickhawkins committed Jul 27, 2021
1 parent 92b066b commit 6b558e9
Showing 1 changed file with 12 additions and 93 deletions.
105 changes: 12 additions & 93 deletions lib/tests/filelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ public function test_curl_basics_with_security_helper() {
public function test_curl_redirects() {
global $CFG;

// Test full URL redirects.
$testurl = $this->getExternalTestFileUrl('/test_redir.php');

$curl = new curl();
Expand All @@ -273,6 +272,7 @@ public function test_curl_redirects() {
$this->assertSame(2, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

// All redirects are emulated now. Enabling "emulateredirects" explicitly does not have effect.
$curl = new curl();
$curl->emulateredirects = true;
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
Expand All @@ -282,6 +282,17 @@ public function test_curl_redirects() {
$this->assertSame(2, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

// All redirects are emulated now. Attempting to disable "emulateredirects" explicitly causes warning.
$curl = new curl();
$curl->emulateredirects = false;
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS' => 2));
$response = $curl->getResponse();
$this->assertDebuggingCalled('Attempting to disable emulated redirects has no effect any more!');
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $curl->get_errno());
$this->assertSame(2, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

// This test was failing for people behind Squid proxies. Squid does not
// fully support HTTP 1.1, so converts things to HTTP 1.0, where the name
// of the status code is different.
Expand All @@ -301,21 +312,6 @@ public function test_curl_redirects() {
$this->assertSame('', $contents);

$curl = new curl();
$curl->emulateredirects = true;
$contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
$response = $curl->getResponse();
$this->assertSame($responsecode302, reset($response));
$this->assertSame(0, $curl->get_errno());
$this->assertSame(302, $curl->info['http_code']);
$this->assertSame('', $contents);

$curl = new curl();
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
$this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
$this->assertNotEmpty($contents);

$curl = new curl();
$curl->emulateredirects = true;
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
$this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
$this->assertNotEmpty($contents);
Expand All @@ -331,18 +327,6 @@ public function test_curl_redirects() {
$this->assertSame('done', file_get_contents($tofile));
@unlink($tofile);

$curl = new curl();
$curl->emulateredirects = true;
$tofile = "$CFG->tempdir/test.html";
@unlink($tofile);
$fp = fopen($tofile, 'w');
$result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
$this->assertTrue($result);
fclose($fp);
$this->assertFileExists($tofile);
$this->assertSame('done', file_get_contents($tofile));
@unlink($tofile);

$curl = new curl();
$tofile = "$CFG->tempdir/test.html";
@unlink($tofile);
Expand All @@ -351,16 +335,6 @@ public function test_curl_redirects() {
$this->assertFileExists($tofile);
$this->assertSame('done', file_get_contents($tofile));
@unlink($tofile);

$curl = new curl();
$curl->emulateredirects = true;
$tofile = "$CFG->tempdir/test.html";
@unlink($tofile);
$result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
$this->assertTrue($result);
$this->assertFileExists($tofile);
$this->assertSame('done', file_get_contents($tofile));
@unlink($tofile);
}

public function test_curl_relative_redirects() {
Expand All @@ -375,15 +349,6 @@ public function test_curl_relative_redirects() {
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

$curl = new curl();
$curl->emulateredirects = true;
$contents = $curl->get($testurl);
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $curl->get_errno());
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

// Test different redirect types.
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');

Expand All @@ -395,15 +360,6 @@ public function test_curl_relative_redirects() {
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

$curl = new curl();
$curl->emulateredirects = true;
$contents = $curl->get("$testurl?type=301");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $curl->get_errno());
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

$curl = new curl();
$contents = $curl->get("$testurl?type=302");
$response = $curl->getResponse();
Expand All @@ -412,15 +368,6 @@ public function test_curl_relative_redirects() {
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

$curl = new curl();
$curl->emulateredirects = true;
$contents = $curl->get("$testurl?type=302");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $curl->get_errno());
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

$curl = new curl();
$contents = $curl->get("$testurl?type=303");
$response = $curl->getResponse();
Expand All @@ -430,24 +377,6 @@ public function test_curl_relative_redirects() {
$this->assertSame('done', $contents);

$curl = new curl();
$curl->emulateredirects = true;
$contents = $curl->get("$testurl?type=303");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $curl->get_errno());
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

$curl = new curl();
$contents = $curl->get("$testurl?type=307");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $curl->get_errno());
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

$curl = new curl();
$curl->emulateredirects = true;
$contents = $curl->get("$testurl?type=307");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
Expand All @@ -462,16 +391,6 @@ public function test_curl_relative_redirects() {
$this->assertSame(0, $curl->get_errno());
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

$curl = new curl();
$curl->emulateredirects = true;
$contents = $curl->get("$testurl?type=308");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $curl->get_errno());
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);

}

public function test_curl_proxybypass() {
Expand Down

0 comments on commit 6b558e9

Please sign in to comment.