function DrupalHTTPRequestTestCase::testDrupalHTTPRequest in SimpleTest 7
File
- tests/
common.test, line 761 - Tests for common.inc functionality.
Class
Code
function testDrupalHTTPRequest() {
// Parse URL schema.
$missing_scheme = drupal_http_request('example.com/path');
$this
->assertEqual($missing_scheme->code, -1002, t('Returned with "-1002" error code.'));
$this
->assertEqual($missing_scheme->error, 'missing schema', t('Returned with "missing schema" error message.'));
$unable_to_parse = drupal_http_request('http:///path');
$this
->assertEqual($unable_to_parse->code, -1001, t('Returned with "-1001" error code.'));
$this
->assertEqual($unable_to_parse->error, 'unable to parse URL', t('Returned with "unable to parse URL" error message.'));
// Fetch page.
$result = drupal_http_request(url('node', array(
'absolute' => TRUE,
)));
$this
->assertEqual($result->code, 200, t('Fetched page successfully.'));
$this
->drupalSetContent($result->data);
$this
->assertTitle(t('Welcome to @site-name | @site-name', array(
'@site-name' => variable_get('site_name', 'Drupal'),
)), t('Site title matches.'));
// Test that code and status message is returned.
$result = drupal_http_request(url('pagedoesnotexist', array(
'absolute' => TRUE,
)));
$this
->assertTrue(!empty($result->protocol), t('Result protocol is returned.'));
$this
->assertEqual($result->code, '404', t('Result code is 404'));
$this
->assertEqual($result->status_message, 'Not Found', t('Result status message is "Not Found"'));
// Test that timeout is respected. The test machine is expected to be able
// to make the connection (i.e. complete the fsockopen()) in 2 seconds and
// return within a total of 5 seconds. If the test machine is extremely
// slow, the test will fail. fsockopen() has been seen to time out in
// slightly less than the specified timeout, so allow a little slack on the
// minimum expected time (i.e. 1.8 instead of 2).
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, t('Request timed out (%time seconds).', array(
'%time' => $time,
)));
$this
->assertTrue($result->error, t('An error message was returned.'));
$this
->assertEqual($result->code, HTTP_REQUEST_TIMEOUT, t('Proper error code was returned.'));
}