Skip to content

Commit

Permalink
MDL-49497 curl: Add configurable User-Agent unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Cooper authored and stronk7 committed Jun 23, 2015
1 parent 300fcae commit cbb7c6a
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions lib/tests/filelib_test.php
Expand Up @@ -878,4 +878,80 @@ public function test_get_mimetypes_array() {
$this->assertEquals('image', $mimeinfo['png']['string']);
$this->assertEquals(true, $mimeinfo['txt']['defaulticon']);
}

/**
* Test curl agent settings.
*/
public function test_curl_useragent() {
$curl = new curl_extended();
$options = $curl->get_options();
$this->assertNotEmpty($options);

$curl->call_apply_opt($options);
$this->assertTrue(in_array('User-Agent: MoodleBot/1.0', $curl->header));
$this->assertFalse(in_array('User-Agent: Test/1.0', $curl->header));

$options['CURLOPT_USERAGENT'] = 'Test/1.0';
$curl->call_apply_opt($options);
$this->assertTrue(in_array('User-Agent: Test/1.0', $curl->header));
$this->assertFalse(in_array('User-Agent: MoodleBot/1.0', $curl->header));

$curl->set_option('CURLOPT_USERAGENT', 'AnotherUserAgent/1.0');
$curl->call_apply_opt();
$this->assertTrue(in_array('User-Agent: AnotherUserAgent/1.0', $curl->header));
$this->assertFalse(in_array('User-Agent: Test/1.0', $curl->header));

$curl->set_option('CURLOPT_USERAGENT', 'AnotherUserAgent/1.1');
$options = $curl->get_options();
$curl->call_apply_opt($options);
$this->assertTrue(in_array('User-Agent: AnotherUserAgent/1.1', $curl->header));
$this->assertFalse(in_array('User-Agent: AnotherUserAgent/1.0', $curl->header));

$curl->unset_option('CURLOPT_USERAGENT');
$curl->call_apply_opt();
$this->assertTrue(in_array('User-Agent: MoodleBot/1.0', $curl->header));
}
}

/**
* Test-specific class to allow easier testing of curl functions.
*
* @copyright 2015 Dave Cooper
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class curl_extended extends curl {
/**
* Accessor for options array.
*/
public function get_options() {
return $this->options;
}

/**
* Setter for options array.
* @param string $option
* @param string $value
*/
public function set_option($option, $value) {
$this->options[$option] = $value;
}

/**
* Unsets an option on the curl object
* @param string $option
*/
public function unset_option($option) {
unset($this->options[$option]);
}

/**
* Wrapper to access the curl::apply_opt() function
*
* @param array $options
* @return resource The curl handle
*/
public function call_apply_opt($options = null) {
$ch = curl_init();
return $this->apply_opt($ch, $options);
}
}

0 comments on commit cbb7c6a

Please sign in to comment.