Services REST Server: Handling custom headers from within Simpletest

texas-bronius's picture

My REST server endpoint is an API that requires a security token be included in a custom header. In my callback (or access callback or anywhere) I can check for this custom header value against getallheaders():
This works and tests beautifully when hitting the API via Postman (and, assumed, a client app). But, I've been unable to get my SimpleTest WebTestCase() tests to set the header and have it be read. I am not sure what is holding me down, but I have to suspect it's something with Simpletest.

I've tried

<?php
 
...
$options['headers'] = array(
 
'myToken' => 'the_token',
);
$result = drupal_http_request($path, $options);
?>

I've tried
<?php
 
...
 
$headers = array(
   
'Content-Type:application/json',
   
'myToken . ':the_token',
  );
  $result = $this->drupalGet($path, array(), $headers);
?>

and
<?php
...
   
$ch = curl_init();
   
curl_setopt($ch, CURLOPT_URL, $path);
   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     
'Content-Type: application/json',
     
'myToken:the_token',
    ));
   
$output = curl_exec($ch);
   
$info = curl_getinfo($ch);
   
curl_close($ch);
?>

But the snippet that listens for this custom header is just not seeing it, and where I'm expecting to see rich and happy headers in my requests log like I do for Postman, instead, the only thing that ever shows in headers in Services Log output (with a small modification) is:
Array (
    [User-Agent] => Drupal (+http://drupal.org/)
    [Host] => www.mysite.vm
)

WHAT GIVES??!! .. :-/

[edit]Update: It's not cli in general, nor is it drupal_http_request: I can hit and log a request like:

drush ev "print_r( drupal_http_request('http://www.mysite.vm/mobile/v1/users/123', array('method' => 'GET', 'headers' => array('Content-Type'=>'application/json', 'myToken'=>'the_token'))) );"

All headers (with the tweak to System Log module linked above) show including my custom header myToken=>the_token. What is it about Simpletest?? :(

Thanks
-Bronius

Comments

WHAT.Ok this is not

texas-bronius's picture

WHAT.
Ok this is not Services.. sorry for off-topic, but I've already got a nice narrative here, and we've all been there, right?

Latest what I see:
The header in drupal_http_request($path, $options) does not show in headers in my Services Log, but drupal_http_request($path."?123", $options) does: Am I skipping cache somewhere? Maybe SimpleTest cache? . . is there such a thing?

-Bronius

To cache or not to cache

texas-bronius's picture

Ok . . I'll leave it at this: Seems that either SimpleTest is caching outbound drupal_http_request(), or the request itself is. I even ran:

<?php
$result
= drupal_http_get($path, $options);
$result = drupal_http_get($path, $options);
$result = drupal_http_get($path, $options);
$result = drupal_http_get($path, $options);
$result = drupal_http_get($path, $options);
?>

and Services Log recorded only one request. Any insight into what's going on? In the interim, I am rewriting my series of GET requests with some cache-invalidating ?timestamp or something.

Cheers
-Bronius