HttpRequestTest.php 8.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Common\HttpRequestTest.
 */

namespace Drupal\system\Tests\Common;

use Drupal\simpletest\WebTestBase;
11
use Drupal\Core\Language\Language;
12
13
14
15
16

/**
 * Tests drupal_http_request().
 */
class HttpRequestTest extends WebTestBase {
17
18
19
20
21
22

  /**
   * Modules to enable.
   *
   * @var array
   */
23
  public static $modules = array('system_test', 'language', 'test_page_test');
24

25
26
27
28
29
30
31
32
  public static function getInfo() {
    return array(
      'name' => 'Drupal HTTP request',
      'description' => "Performs tests on Drupal's HTTP request mechanism.",
      'group' => 'Common',
    );
  }

33
34
35
  /**
   * Checks HTTP requests.
   */
36
37
38
39
40
  function testDrupalHTTPRequest() {
    global $is_https;

    // Parse URL schema.
    $missing_scheme = drupal_http_request('example.com/path');
41
42
    $this->assertEqual($missing_scheme->code, -1002, 'Returned with "-1002" error code.');
    $this->assertEqual($missing_scheme->error, 'missing schema', 'Returned with "missing schema" error message.');
43
44

    $unable_to_parse = drupal_http_request('http:///path');
45
46
    $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.');
47

48
49
    // Fetch the test page.
    $result = drupal_http_request(url('test-page', array('absolute' => TRUE)));
50
    $this->assertEqual($result->code, 200, 'Fetched page successfully.');
51
    $this->drupalSetContent($result->data);
52
    $this->assertTitle(t('Test page | @site-name', array('@site-name' => config('system.site')->get('name'))));
53

54
55
56
57
    // Test that Drupal.settings is properly parsed.
    $settings = $this->drupalGetSettings();
    $this->assertIdentical($settings['test-setting'], 'azAZ09();.,\\\/-_{}');

58
59
    // Test that code and status message is returned.
    $result = drupal_http_request(url('pagedoesnotexist', array('absolute' => TRUE)));
60
61
62
    $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"');
63
64
65
66
67
68
69
70
71
72
73
74
75
76

    // Skip the timeout tests when the testing environment is HTTPS because
    // stream_set_timeout() does not work for SSL connections.
    // @link http://bugs.php.net/bug.php?id=47929
    if (!$is_https) {
      // 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;
77
78
79
      $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.');
80
81
82
    }
  }

83
84
85
  /**
   * Tests HTTP basic authorization.
   */
86
87
88
89
90
91
92
93
94
  function testDrupalHTTPRequestBasicAuth() {
    $username = $this->randomName();
    $password = $this->randomName();
    $url = url('system-test/auth', array('absolute' => TRUE));

    $auth = str_replace('://', '://' . $username . ':' . $password . '@', $url);
    $result = drupal_http_request($auth);

    $this->drupalSetContent($result->data);
95
96
    $this->assertRaw($username, '$_SERVER["PHP_AUTH_USER"] is passed correctly.');
    $this->assertRaw($password, '$_SERVER["PHP_AUTH_PW"] is passed correctly.');
97
98
  }

99
100
101
  /**
   * Tests HTTP redirect requests.
   */
102
103
  function testDrupalHTTPRequestRedirect() {
    $redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array('max_redirects' => 1));
104
    $this->assertEqual($redirect_301->redirect_code, 301, 'drupal_http_request follows the 301 redirect.');
105
106

    $redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array('max_redirects' => 0));
107
    $this->assertFalse(isset($redirect_301->redirect_code), 'drupal_http_request does not follow 301 redirect if max_redirects = 0.');
108
109

    $redirect_invalid = drupal_http_request(url('system-test/redirect-noscheme', array('absolute' => TRUE)), array('max_redirects' => 1));
110
111
    $this->assertEqual($redirect_invalid->code, -1002, format_string('301 redirect to invalid URL returned with error code !error.', array('!error' => $redirect_invalid->error)));
    $this->assertEqual($redirect_invalid->error, 'missing schema', format_string('301 redirect to invalid URL returned with error message "!error".', array('!error' => $redirect_invalid->error)));
112
113

    $redirect_invalid = drupal_http_request(url('system-test/redirect-noparse', array('absolute' => TRUE)), array('max_redirects' => 1));
114
115
    $this->assertEqual($redirect_invalid->code, -1001, format_string('301 redirect to invalid URL returned with error message code "!error".', array('!error' => $redirect_invalid->error)));
    $this->assertEqual($redirect_invalid->error, 'unable to parse URL', format_string('301 redirect to invalid URL returned with error message "!error".', array('!error' => $redirect_invalid->error)));
116
117

    $redirect_invalid = drupal_http_request(url('system-test/redirect-invalid-scheme', array('absolute' => TRUE)), array('max_redirects' => 1));
118
119
    $this->assertEqual($redirect_invalid->code, -1003, format_string('301 redirect to invalid URL returned with error code !error.', array('!error' => $redirect_invalid->error)));
    $this->assertEqual($redirect_invalid->error, 'invalid schema ftp', format_string('301 redirect to invalid URL returned with error message "!error".', array('!error' => $redirect_invalid->error)));
120
121

    $redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array('max_redirects' => 1));
122
    $this->assertEqual($redirect_302->redirect_code, 302, 'drupal_http_request follows the 302 redirect.');
123
124

    $redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array('max_redirects' => 0));
125
    $this->assertFalse(isset($redirect_302->redirect_code), 'drupal_http_request does not follow 302 redirect if $retry = 0.');
126
127

    $redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array('max_redirects' => 1));
128
    $this->assertEqual($redirect_307->redirect_code, 307, 'drupal_http_request follows the 307 redirect.');
129
130

    $redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array('max_redirects' => 0));
131
    $this->assertFalse(isset($redirect_307->redirect_code), 'drupal_http_request does not follow 307 redirect if max_redirects = 0.');
132
133
134

    $multiple_redirect_final_url = url('system-test/multiple-redirects/0', array('absolute' => TRUE));
    $multiple_redirect_1 = drupal_http_request(url('system-test/multiple-redirects/1', array('absolute' => TRUE)), array('max_redirects' => 1));
135
    $this->assertEqual($multiple_redirect_1->redirect_url, $multiple_redirect_final_url, 'redirect_url contains the final redirection location after 1 redirect.');
136
137

    $multiple_redirect_3 = drupal_http_request(url('system-test/multiple-redirects/3', array('absolute' => TRUE)), array('max_redirects' => 3));
138
    $this->assertEqual($multiple_redirect_3->redirect_url, $multiple_redirect_final_url, 'redirect_url contains the final redirection location after 3 redirects.');
139
140
141
142
143
144
145
146
  }

  /**
   * Tests Content-language headers generated by Drupal.
   */
  function testDrupalHTTPRequestHeaders() {
    // Check the default header.
    $request = drupal_http_request(url('<front>', array('absolute' => TRUE)));
147
    $this->assertEqual($request->headers['content-language'], 'en', 'Content-Language HTTP header is English.');
148
149

    // Add French language.
150
    $language = new Language(array(
151
152
      'langcode' => 'fr',
      'name' => 'French',
153
    ));
154
155
156
157
    language_save($language);

    // Request front page in French and check for matching Content-language.
    $request = drupal_http_request(url('<front>', array('absolute' => TRUE, 'language' => $language)));
158
    $this->assertEqual($request->headers['content-language'], 'fr', 'Content-Language HTTP header is French.');
159
160
  }
}