/**
 * Services requests based on the incoming request path
 *
 * @param   string $incoming_request_uri  The URI being visited in this script
 * @param   array  $options               Options used for Clever API requests
 */
function process_incoming_requests($incoming_request_uri, array $options)
{
    if (preg_match('/oauth/', $incoming_request_uri)) {
        try {
            $me = process_client_redirect($_GET['code'], $options);
            echo "<p>Here's some information about the user:</p>";
            echo "<ul>";
            $fields = array('type' => 'User type', 'id' => 'User ID', 'district' => 'District ID');
            foreach ($fields as $key => $label) {
                echo "<li>{$label}: {$me['data'][$key]}";
            }
            echo "</ul>";
        } catch (Exception $e) {
            echo "<p>Something exceptional happened while interacting with Clever.";
            echo "<pre>";
            print_r($e);
            echo "</pre>";
        }
    } else {
        // Our home page route will create a Clever Instant Login button for users
        $sign_in_link = generate_sign_in_with_clever_link($options);
        echo "<h1>clever_oauth_examples: Login!</h1>";
        echo '<p>' . $sign_in_link . '</p>';
        echo "<p>Ready to handle OAuth 2.0 client redirects on {$options['client_redirect_url']}.</p>";
    }
}
 public function testProcessClientRedirect()
 {
     $mock_request_options = prepare_options_for_clever();
     $token_hash_response = array('access_token' => 'abcd');
     $token_string_response = json_encode($token_hash_response);
     $this->http->mock->when()->methodIs('POST')->pathIs('/oauth/tokens')->then()->body($token_string_response)->end();
     $me_hash_response = prepare_me_response_hash();
     $me_string_response = json_encode($me_hash_response);
     $this->http->mock->when()->methodIs('GET')->pathIs('/me')->then()->body($me_string_response);
     $this->http->setUp();
     $me_response = process_client_redirect('xyz', $mock_request_options);
     $this->assertEquals($me_response, $me_hash_response);
 }