function DrupalHTTPRequestTestCase::testDrupalHTTPRequest in Drupal 7
File
- modules/simpletest/tests/common.test, line 1116
- Tests for common.inc functionality.
Class
- DrupalHTTPRequestTestCase
- Test drupal_http_request().
Code
function testDrupalHTTPRequest() {
global $is_https;
$missing_scheme = drupal_http_request('example.com/path');
$this
->assertEqual($missing_scheme->code, -1002, 'Returned with "-1002" error code.');
$this
->assertEqual($missing_scheme->error, 'missing schema', 'Returned with "missing schema" error message.');
$unable_to_parse = drupal_http_request('http:///path');
$this
->assertEqual($unable_to_parse->code, -1001, 'Returned with "-1001" error code.');
$this
->assertEqual($unable_to_parse->error, 'unable to parse URL', 'Returned with "unable to parse URL" error message.');
$data_array = array(
$this
->randomName() => $this
->randomString() . ' "\'',
);
$data_string = drupal_http_build_query($data_array);
$result = drupal_http_request(url('node', array(
'absolute' => TRUE,
)), array(
'data' => $data_array,
));
$this
->assertEqual($result->code, 200, 'Fetched page successfully.');
$this
->assertTrue(substr($result->request, -strlen($data_string)) === $data_string, 'Request ends with URL-encoded data when drupal_http_request() called using array.');
$result = drupal_http_request(url('node', array(
'absolute' => TRUE,
)), array(
'data' => $data_string,
));
$this
->assertTrue(substr($result->request, -strlen($data_string)) === $data_string, 'Request ends with URL-encoded data when drupal_http_request() called using string.');
$this
->drupalSetContent($result->data);
$this
->assertTitle(t('Welcome to @site-name | @site-name', array(
'@site-name' => variable_get('site_name', 'Drupal'),
)), 'Site title matches.');
$result = drupal_http_request(url('pagedoesnotexist', array(
'absolute' => TRUE,
)));
$this
->assertTrue(!empty($result->protocol), 'Result protocol is returned.');
$this
->assertEqual($result->code, '404', 'Result code is 404');
$this
->assertEqual($result->status_message, 'Not Found', 'Result status message is "Not Found"');
if (!$is_https) {
timer_start(__METHOD__);
$result = drupal_http_request(url('system-test/sleep/10', array(
'absolute' => TRUE,
)), array(
'timeout' => 2,
));
$time = timer_read(__METHOD__) / 1000;
$this
->assertTrue(1.8 < $time && $time < 5, format_string('Request timed out (%time seconds).', array(
'%time' => $time,
)));
$this
->assertTrue($result->error, 'An error message was returned.');
$this
->assertEqual($result->code, HTTP_REQUEST_TIMEOUT, 'Proper error code was returned.');
}
}