Example #1
0
// config setup
define('BASEPATH', '.');
include '../database.php';
$ji_db = new PDO('mysql:host=' . $db['default']['hostname'] . ';dbname=' . $db['default']['database'], $db['default']['username'], $db['default']['password']);
// Set the correct charset for this connection
$ji_db->query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
$ji_db->query('SET CHARACTER SET utf8');
// collect URL and headers
$request = new Request();
// set some default parameters
$request->parameters['resultsperpage'] = $request->getParameter('resultsperpage', 20);
$request->parameters['start'] = $request->getParameter('start', 0);
// identify our user if applicable
$headers = apache_request_headers();
if (isset($headers['Authorization'])) {
    $request->identifyUser($ji_db, $headers['Authorization']);
}
// Which content type to return? Parameter takes precedence over accept headers
// with final fall back to json
$format_choices = array('application/json', 'text/html');
$header_format = $request->preferredContentTypeOutOf($format_choices);
$format = $request->getParameter('format', $header_format);
switch ($format) {
    case 'text/html':
    case 'html':
        $request->view = new HtmlView();
        break;
    case 'application/json':
    case 'json':
    default:
        // JSONP?
Example #2
0
 /**
  * Ensures that identifyUser method sets a user id on the request model when
  * using the bearer token type
  *
  * @return void
  *
  * @test
  */
 public function identifyUserWithBearerTokenTypeSetsUserIdForValidHeader()
 {
     $request = new \Request($this->config, ['HTTPS' => 'on']);
     $mockOauth = $this->getMock('OAuthModel', array(), array(), '', false);
     $mockOauth->expects($this->once())->method('verifyAccessToken')->with('authPart')->will($this->returnValue('TheUserId'));
     $request->setOauthModel($mockOauth);
     $request->identifyUser(null, 'Bearer authPart');
     $this->assertEquals('TheUserId', $request->user_id);
     $this->assertEquals('TheUserId', $request->getUserId());
 }