Commit 3181bcd3 authored by Jonathan Hunt's avatar Jonathan Hunt

#53 Add API text callback to exercise Vary header.

parent ed1da52b
......@@ -31,6 +31,53 @@
include_once 'climbnz_api.features.inc';
/**
* Callback for handling API testing, esp. Vary header.
*
* Implements hook_menu().
*/
function climbnz_api_menu() {
$items = array();
$items['api-test'] = array(
'title' => t('API Test'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'page callback' => 'climbnz_api_test',
);
return $items;
}
/**
* @todo: accept should check anywhere in accept header, not just first chars.
* @return string
*/
function climbnz_api_test() {
flog_it(__FUNCTION__ . ': $_SERVER=' . print_r($_SERVER, true));
drupal_add_http_header('Vary', 'Accept', TRUE);
if (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') !== FALSE) {
//drupal_add_http_header('Vary', 'Accept', TRUE);
return '<html><head><title>API Test</title></head><body><p>This is an HTML response.</p></body></html>';
}
if (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== FALSE) {
//drupal_add_http_header('Vary', 'Accept', TRUE);
drupal_add_http_header('Content-Type', 'application/json');
// Output version info.
$output = array('payload' => 'This is an API test.');
$output['meta']['version'] = '2';
$output['jsonapi'] = array(
'version' => '1.1',
);
drupal_json_output($output);
//return str_replace('\\/', '/', json_encode($output));
}
// Treat as HTML?
else {
return '<html><head><title>API Test</title></head><body><p>This is an HTML response.</p></body></html>';
}
}
/**
* Map request media type to formatter (handler).
* Define combined HAL+JSON response formatter.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment