<?php

/**
 * Class SAML2_BindingTest
 */
class SAML2_BindingTest extends PHPUnit_Framework_TestCase
{

    /**
     * Test getting binding objects from string.
     */
    public function testGetBinding()
    {
        $bind = SAML2_Binding::getBinding(SAML2_Const::BINDING_HTTP_POST);
        $this->assertInstanceOf('SAML2_HTTPPost', $bind);
        $bind = SAML2_Binding::getBinding(SAML2_Const::BINDING_HTTP_REDIRECT);
        $this->assertInstanceOf('SAML2_HTTPRedirect', $bind);
        $bind = SAML2_Binding::getBinding(SAML2_Const::BINDING_HTTP_ARTIFACT);
        $this->assertInstanceOf('SAML2_HTTPArtifact', $bind);
        $bind = SAML2_Binding::getBinding(SAML2_Const::BINDING_HOK_SSO);
        $this->assertInstanceOf('SAML2_HTTPPost', $bind);

        $this->setExpectedException('Exception', 'Unsupported binding:');
        $bind = SAML2_Binding::getBinding('nonsense');
    }

    /**
     * Test binding guessing functionality.
     */
    public function testBindingGuesserGET()
    {
        $_SERVER['REQUEST_METHOD'] = 'GET';
        $_GET = array('SAMLRequest' => 'pVJNjxMxDP0ro9yn88FOtRu1lcpWiEoLVNvCgQtKE6eNlHGG2IHl35OZLmLZQy%2BcrNh%2Bz88vXpDq%2FSDXic%2F4CN8TEBdPvUeSU2EpUkQZFDmSqHogyVru1x8eZDur5RADBx28eAG5jlBEENkFFMV2sxTfzO0dmKa11namPuoc39hba%2BfqpqlbM6%2Fb5mZ%2B1LWtj6L4ApEycikyUYYTJdgisULOqbrpyqYt67tD28iulV33VRSbvI1DxRPqzDyQrCrAk0OYUYpWB4QnnqGvVN4fkJ2emitnhoocnjyU5E5YjnrXf6TfB6TUQ9xD%2FOE0fH58%2BEueHbHOv2Yn1w8eRneqPpiU68M5DxjfdIltqTRNWQNWJc8lDaLYPfv71qHJaq5be7w0kXx%2FOOzK3af9QawWI7ecrIqr%2F9HYAyujWL2SuKheDlhcbuljlrbd7IJ3%2BlfxLsRe8XXlY8aZ0k6tkqNCcvkzsuXeh5%2F3ERTDUnBMIKrVZeS%2FF7v6DQ%3D%3D');
        $bind = SAML2_Binding::getCurrentBinding();
        $this->assertInstanceOf('SAML2_HTTPRedirect', $bind);

        $_GET = array('SAMLResponse' => 'vVdbc6rIGn2fX2G5H1MJd0RrJ1UgKmDQIIiXl1NANzcRCA2i%2FvppNLpNJsnMnqo5T9qL%2Fq7r62bxEznbJO%2FNIMqzFMHWfpukqHcCH9tVkfYyB0WolzpbiHql1zNF%2FblHP5C9vMjKzMuS9o3J9xYOQrAooyxtt1T5sd2fzqxplxVIhoIMCbkuIEnehSRJ0xRJdroU1fFc6HWA4Hiw3bJhgbDtYxu7wg4QqqCaotJJSwyRFHdP0fcUb1Fsj2V7pLBut2SIyih1ypNVWJY56hFEGW6iexSBh7y8Z4WHqiweUFX4XpJV4CFNiC1Mkiwl8gyVl57gaOnlv5U9tv9HdzsSTQ4GlCB0hqQ8xD84XYHmOzxFMkOh%2FfSz6UbvlGTxdAkN0yBK4UOJ0zrHzFK4L5ugTlWGMC0j75QsEYEc51E6wCmdn8Stq59ntszSKSv0ftXPAGzZTlLB71lAp909s%2FI8iFCbeDpHeO%2B0J164emN3j6JzD3EddV0%2F1MxDVgQETZIUsdSfTS%2BEW%2Bc%2BOhHSsHWx%2Bnuj22HoMgwA0KV53GHKFQTHcR2PZT0SQEHgfAggECB0%2F8Uw%2FHeMANzLKMBjVhWX0wO%2BKpskyC6B9wAUBT%2FaT3%2B0WhdzCNTUz07e%2Bk6apThwEh1PwXVYhhloiUmQFVEZbr9sKUU2vu%2Fh3rv3KDb9gbnFEX7FOKX4D729y7RAzj0KHerssHE3gz4sIGa6NZ%2Bpj%2B0fv0ffqUyrcFLkZ8UWvV%2F%2BXmow3cEkyyHAZ%2Fqtwmakf8vhp537Sfw1RzkK8KT8mw6%2Bde%2BXk9NBfdou75YRhJU%2FUXO3XN7ZQjh2p%2FmwFsXHUwK3m0%2FAtfHn5YfRubJ8tuhXCeETSyl2wWg%2BX5aA3K4SL6ZhcjciFlLVCUcCKUOKMRcSuwfiRCI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%2BfAK3y926kji%2FdJn4y0hfLKsHFdn2%2BQj5fCFTxfG8TthfLuxnkTCGblxtAr31YTrJ5UuTXEbwCn%2FF5WNUgE7v3T1l7ZvDgiLCDaT6TDsb7LdEm%2Bi2Uiw3DAYSDLkKxP%2BRzPOMDlXZ73%2BTdZcQ75Ppt%2BlvpR47fRY%2BfXz%2FfJeNueC50CFu2vHTUNaU2ycppOC9EvYfEX5dQ9y%2BgZ9KK8qeX%2FLKIvyvSz5D88eqsS7wBR8xg1hUkQkwE%2F04MdXNU%2FqPwihSsQNW9cnH1ZRN45%2FLsnT7%2FZlw9K8urmw%2FpdQOJDhdcUyjBtlDvcYoZap%2BVXSpjucRyu3MSyH3v4sgE03WOZHi382qqmAO4xZZwLuC5Pczzrk5zHeRTPAMahXExcx6V8yDGMA7sQeKB9mx5OusSy%2BhOon%2BBvQixpnr79bPR6XrMPwy%2F4p84KcG3UJ65%2BRbno9zRoVo1YO1yZwpIxxaL%2BwceHFj6k2Y3Hz8w%2BCfgOuzJwRS%2FfT9fPq8vwP%2F0J');
        $bind = SAML2_Binding::getCurrentBinding();
        $this->assertInstanceOf('SAML2_HTTPRedirect', $bind);

        $_GET = array('SAMLart' => 'AAQAAI4sWYpfoDDYJrHzsMnG%2BjyNM94p5ejn49a%2BnZ0s3ylY7knQ6tkLMDE%3D');
        $bind = SAML2_Binding::getCurrentBinding();
        $this->assertInstanceOf('SAML2_HTTPArtifact', $bind);

        $_GET = array('aap' => 'noot');
        $this->setExpectedException('Exception', 'Unable to find the current binding.');
        $bind = SAML2_Binding::getCurrentBinding();
    }
    /**
     * Test binding guessing functionality.
     */
    public function testBindingGuesserPOST()
    {
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_POST = array('SAMLRequest' => 'lZLBbtswDIZfxdDddpw0TiYkAZK4BQJ0RZGsO+wyCDKTCJMpT6S69u0nOejaXVr0JIDkT/L7xQWpzvZyHfiMe/gdgDh76iySHBJLETxKp8iQRNUBSdbysP56K8fFSPbesdPOijeS9xWKCDwbhyLbNUvxcz6bbLeT8WjeNLPpzea6qjbr2WZyVdXX9WZWVyL7Dp5i/VJEeRQRBdghsUKOoVE1zatxXtXfqlpOvsjp6IfImshgUPGgOjP3JMsS8GQQCo65goI/aofwxAXaUkV0QDZ6UJSm7UsyeLKQkzlhnlbdOiRIA99D05ciqYP38c1N11ujDYvsxnkNg8NLcVSWIHHcRyvMI/yLrF+cScNCB/4A/tFoeNjfvlLw+ZeJa7VFz/nVvAjsLzDWhTaxdGBtZOgd8R6oTxuJ1SJ9ixyc86tPduqAVatYLcq3TRaXo7mLHuyaexcxnxNkpz6wKEVMmx+HUsleIZloVYSPw/5sPSiOhrAPIMrVZeT/p7n6Cw==');
        $bind = SAML2_Binding::getCurrentBinding();
        $this->assertInstanceOf('SAML2_HTTPPost', $bind);

        $_POST = array('SAMLResponse' => ' ');
        $bind = SAML2_Binding::getCurrentBinding();
        $this->assertInstanceOf('SAML2_HTTPPost', $bind);

        $_POST = array();
        $_SERVER['CONTENT_TYPE'] = 'text/xml';
        $bind = SAML2_Binding::getCurrentBinding();
        $this->assertInstanceOf('SAML2_SOAP', $bind);

        $_POST = array('SAMLart' => 'AAQAAI4sWYpfoDDYJrHzsMnG+jyNM94p5ejn49a+nZ0s3ylY7knQ6tkLMDE=');
        $bind = SAML2_Binding::getCurrentBinding();
        $this->assertInstanceOf('SAML2_HTTPArtifact', $bind);

        $_POST=array('AAP' => 'Noot');
        unset($_SERVER['CONTENT_TYPE']);
        $this->setExpectedException('Exception', 'Unable to find the current binding.');
        $bind = SAML2_Binding::getCurrentBinding();
    }

    /**
     * Test binding guessing functionality with unsupported HTTP method.
     */
    public function testBindingGuesserWRONG()
    {
        $_SERVER['REQUEST_METHOD'] = 'PUT';
        $_SERVER['CONTENT_TYPE'] = 'text/xml';
        $this->setExpectedException('Exception', 'Unable to find the current binding.');
        $bind = SAML2_Binding::getCurrentBinding();
    }

    /**
     * test destination getter and setter.
     */
    public function testGetSetDestination()
    {
        $bind = SAML2_Binding::getBinding(SAML2_Const::BINDING_HTTP_POST);
        $dest = $bind->getDestination();
        $this->assertNull($dest);

        $bind->setDestination('http://example.org/example');
        $dest = $bind->getDestination();
        $this->assertEquals($dest, 'http://example.org/example');
    }
}
