Posted by texas-bronius on May 24, 2017 at 10:09pm
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
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, butdrupal_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
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
Oh hi Me
Here I am a year later revisiting the same tests and having come full circle. I don't know why I didn't commit the cache-invalidating ?timestamp solution, because that is all I am finding that works for this case. Note that ?timestamp may not be sufficient if your test runs really quickly, bc timestamp is identical from request to request. In this case, I slapped on an addition
rand(0, 999)
. Hope this helps someone.Note: I also have tried adding header cache-control: no-cache like adding to the $options array:
'headers' => array(
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
),
but http response comes back with:
[server] => Apache/2.4.6 (CentOS)
[x-content-type-options] => nosniff
[x-powered-by] => PHP/5.6.36
[x-drupal-cache] => HIT
[vary] => Cookie
[etag] => "1538167313-0"
[cache-control] => public, max-age=86400
[last-modified] => Fri, 28 Sep 2018 20:41:53 GMT
[expires] => Sun, 19 Nov 1978 05:00:00 GMT
¯_(ツ)_/¯