UPDATE:
Re-enabling old post, because I found more information about it.

Goal

I want to send the cookie using drupalGet.
Example:

drupalGet($path, $options, array('Cookie: my_cookie=' . $user->name, 'User-Agent: Mozilla'));

Problem

drupalGet/curl is sending two 'Cookie' headers (in two lines).
Bad example (as it's currently):

Cookie: SESSd5f6d078ecfc43ac091312829e2f7ae5=017d67075033939108c868775afea2da
Cookie: my_cookie=vk1VVdO7

What I was expecting is the following header (in one line):

Cookie: my_cookie=vk1VVdO7; SESS077a2739d9e46e582c048b5db2da58c1=08f246f1e898c24df3a28ebf263682d3

If there are two lines of 'Cookie' header, $_COOKIE global variable looks invalid.
Example (having print_r($_COOKIE); exit; in my hook_boot):

$ curl -v -H "Cookie: SESS123=123; my_cookie=vk1VVdO7" http://localhost/
Array
(
    [SESS123] => 123
    [my_cookie] => vk1VVdO7
)
CORRECT!
$ curl -v -H "Cookie: SESS123=123" -H "Cookie: my_cookie=vk1VVdO7" http://localhost/
Array
(
    [SESS123] => 123, my_cookie=vk1VVdO7
)
NOT CORRECT!

Not sure if it's reproducible in Drupal 7.x

Original issue report

Code:

    $this->drupalGet($url, NULL, array('Cookie: cookie=cookievalue;'));
    var_dump($this->drupalGetHeaders()); exit;

Result:


array(13) {
  [":status"]=>
  string(15) "HTTP/1.1 200 OK"
  ["date"]=>
  string(29) "Mon, 24 Oct 2011 14:39:26 GMT"
  ["server"]=>
  string(6) "Apache"
  ["x-powered-by"]=>
  string(9) "PHP/5.3.6"
  ["set-cookie"]=>
  string(126) "SESS60f7eff84479fe668a24e164b6291550=8bc3aeeb78109aa3438548c41f1e426c; expires=Wed, 16-Nov-2011 18:12:46 GMT; path=/; HttpOnly"
  ["expires"]=>
  string(29) "Sun, 19 Nov 1978 05:00:00 GMT"
  ["last-modified"]=>
  string(31) "Mon, 24 Oct 2011 14:39:26 +0000"
  ["cache-control"]=>
  string(52) "no-cache, must-revalidate, post-check=0, pre-check=0"
  ["etag"]=>
  string(12) ""1319467166""

I can't see 'Cookie:' in the response.
Is there any way to pass the cookie for the request?

Related topics:
http://groups.drupal.org/node/22176
#457804: Enable simpletest to manipulate cookies

CommentFileSizeAuthor
#6 drupal_web_test_case.php-1319736.patch3.52 KBkenorb

Comments

kenorb’s picture

Status: Active » Closed (works as designed)

It doesn't return in header, but the URL which is called has access to cookie via $_COOKIE.

kenorb’s picture

Issue summary: View changes

added question

kenorb’s picture

Category: support » bug
Status: Closed (works as designed) » Active
kenorb’s picture

Issue summary: View changes

updating the old issue with more investigation

kenorb’s picture

Issue summary: View changes

removing repetition

kenorb’s picture

Issue summary: View changes

changing cookie name

kenorb’s picture

Title: SimpleTest: Cookie is not set when calling drupalGet() » SimpleTest: When using drupalGet to send the cookie, it's sending Cookie header twice, so the $_COOKIE is not valid
kenorb’s picture

kenorb’s picture

Reproducing the bug in simple way

Following script test the bug:

$ cat ./curl-test.drush
#!/usr/bin/env drush

require_once drupal_get_path('module', 'simpletest') . '/drupal_web_test_case.php';

class CurlTest extends DrupalWebTestCase {
  function testCurl() {
    // Create new user to create session cookie and log in
    $user = $this->drupalCreateUser(array('access content'));
    $this->drupalLogin($user);
    // Set up curl, so we can see the sent headers later
    $this->curlInitialize();
    curl_setopt($this->curlHandle, CURLINFO_HEADER_OUT, true);
    // Open any page
    $this->drupalGet('node', array(), array("Cookie: my_cookie"));
    // Print sent headers
    print_r(curl_getinfo($this->curlHandle, CURLINFO_HEADER_OUT));
  }
}

$test = new CurlTest();
$test->run();

It should give you something like:

GET /node HTTP/1.1
User-Agent: simpletest111843;1349265652;506c28f47bbb69.14132423;tMqlFGrpITeHbToprcbYZ59LOpQ=
Host: localhost
Accept: */*
Cookie: SESSd5f6d078ecfc43ac091312829e2f7ae5=e67c7c3cff2f6f3b22b34b176c9214aa
Cookie: my_cookie

Which is wrong (Cookie section has two lines, instead of one).
If additional session cookie is not generated by drush, try pasting somewhere else, i.e. Devel PHP Code.

kenorb’s picture

Category: feature » bug
Status: Needs review » Active
StatusFileSize
new3.52 KB

Following patch fix the problem.
Tested on Drupal 6.x (Pressflow). Should be pretty the same for Drupal 7.x.

Now the testing code from #5 returns this:

GET /node HTTP/1.1
User-Agent: simpletest625291;1349267923;506c31d365eb87.60621687;RrdoinDNw96MlcoC9zbWM9/AmGI=
Host: localhost
Accept: */*
Cookie: SESSd5f6d078ecfc43ac091312829e2f7ae5=10f7d556b551084fb2dc5208cbfb6a6b; my_cookie

P.S. I'm behind the proxy, so I can't create the proper patch against the latest git version.

Workaround

As workaround (without patching) it's possible to defining own drupalGet method in the test.

  /**
   * Retrieves a Drupal path or an absolute path.
   *
   * @param $path
   *   Drupal path or URL to load into internal browser
   * @param $options
   *   Options to be forwarded to url().
   * @param $headers
   *   An array containing additional HTTP request headers, each formatted as
   *   "name: value".
   * @param $cookies
   *   The contents of the "Cookie: " header to be used in the HTTP request.
   *   Note that multiple cookies are separated with a semicolon followed by a space as
   *   "fruit=apple; colour=red".
   * @return
   *   The retrieved HTML string, also available as $this->drupalGetContent()
   */
  protected function drupalGet($path, array $options = array(), array $headers = array(), $cookies = '') {
    $options['absolute'] = TRUE;

    // We re-using a CURL connection here. If that connection still has certain
    // options set, it might change the GET into a POST. Make sure we clear out
    // previous options.
    $out = $this->curlExec(array(CURLOPT_HTTPGET => TRUE, CURLOPT_URL => url($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => $headers, CURLOPT_COOKIE => $cookies));
    $this->refreshVariables(); // Ensure that any changes to variables in the other thread are picked up.

    // Replace original page output with new output from redirected page(s).
    if ($new = $this->checkForMetaRefresh()) {
      $out = $new;
    }
    $this->verbose('GET request to: ' . $path .
                   '<hr />Ending URL: ' . $this->getUrl() .
                   '<hr />' . $out);
    return $out;
  }
kenorb’s picture

Issue summary: View changes

simplified cookie name

kenorb’s picture

Status: Needs work » Needs review

As per http://stackoverflow.com/questions/9142964/curl-cookie-value

You should use CURLOPT_COOKIE not CURLOPT_HTTPHEADER to set the cookie values sent in the request.

marking it as a feature.

Category: bug » feature
Status: Active » Needs work

The last submitted patch, drupal_web_test_case.php-1319736.patch, failed testing.

Status: Active » Needs work
kenorb’s picture

Priority: Normal » Minor

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.