/** * Verify that API version is sent in HTTP requests */ public function testVersionHeaderPresence() { $opts = ['key' => 'fake.key:totallyFake', 'httpClass' => 'tests\\HttpMock']; $ably = new AblyRest($opts); $ably->time(); // make a request $curlParams = $ably->http->getCurlLastParams(); $this->assertArrayHasKey('X-Ably-Version', $curlParams[CURLOPT_HTTPHEADER], 'Expected Ably version header in HTTP request'); $this->assertEquals(AblyRest::API_VERSION, $curlParams[CURLOPT_HTTPHEADER]['X-Ably-Version'], 'Expected Ably version in HTTP header to match AblyRest constant'); $this->assertArrayHasKey('X-Ably-Lib', $curlParams[CURLOPT_HTTPHEADER], 'Expected Ably lib header in HTTP request'); $this->assertEquals('php-' . AblyRest::LIB_VERSION, $curlParams[CURLOPT_HTTPHEADER]['X-Ably-Lib'], 'Expected Ably lib in HTTP header to match AblyRest constant'); AblyRest::setLibraryFlavourString('test'); $ably = new AblyRest($opts); $ably->time(); // make a request $curlParams = $ably->http->getCurlLastParams(); $this->assertEquals('php-test-' . AblyRest::LIB_VERSION, $curlParams[CURLOPT_HTTPHEADER]['X-Ably-Lib'], 'Expected X-Ably-Lib to contain library flavour string'); AblyRest::setLibraryFlavourString(); }
/** * Verify that all the parameters are supported and saved as defaults */ public function testAuthorizeParams() { $ably = new AblyRest(array_merge(self::$defaultOptions, ['key' => self::$testApp->getAppKeyDefault()->string, 'authClass' => 'authTest\\AuthMock'])); $ably->auth->fakeRequestToken = true; $tokenParams = ['clientId' => 'tokenParamsClientId', 'ttl' => 2000000, 'capability' => '{"test":"tp"}', 'timestamp' => $ably->time()]; $authOptions = ['clientId' => 'authOptionsClientId', 'key' => 'testKey.Name:testKeySecret', 'token' => 'testToken', 'tokenDetails' => new TokenDetails('testToken'), 'useTokenAuth' => true, 'authCallback' => 'not a callback', 'authUrl' => 'not a url', 'authHeaders' => ['blah' => 'yes'], 'authParams' => ['param' => 'yep'], 'authMethod' => 'TEST', 'queryTime' => true]; // test with empty params first $ably->auth->authorize(); $this->assertTrue($ably->auth->requestTokenCalled, 'Expected authorize() to call requestToken()'); $this->assertEmpty($ably->auth->lastTokenParams, 'Expected authorize() to pass empty tokenParams to requestToken()'); $this->assertEmpty($ably->auth->lastAuthOptions, 'Expected authorize() to pass empty authOptions to requestToken()'); $ably->auth->lastTokenParams = $ably->auth->lastAuthOptions = null; // provide both tokenParams and authOptions and see if they get passed to requestToken $ably->auth->authorize($tokenParams, $authOptions); $this->assertEquals($tokenParams, $ably->auth->lastTokenParams, 'Expected authorize() to pass provided tokenParams to requestToken()'); $this->assertEquals($authOptions, $ably->auth->lastAuthOptions, 'Expected authorize() to pass provided authOptions to requestToken()'); $this->assertFalse(isset($ably->auth->getSavedAuthorizeTokenParams()['timestamp']), 'Expected authorize() to save provided tokenParams without the `timestamp` field'); $this->assertFalse(isset($ably->auth->getSavedAuthorizeAuthOptions()['force']), 'Expected authorize() to save provided authOptions without the `force` field'); $ably->auth->lastTokenParams = $ably->auth->lastAuthOptions = null; // provide no tokenParams or authOptions and see if previously saved params get passed to requestToken unset($tokenParams['timestamp']); // expecting timestamp not to be remembered $ably->auth->authorize(); $this->assertEquals($tokenParams, $ably->auth->lastTokenParams, 'Expected authorize() to pass saved tokenParams to requestToken()'); $this->assertEquals($authOptions, $ably->auth->lastAuthOptions, 'Expected authorize() to pass saved authOptions to requestToken()'); $ably->auth->lastTokenParams = $ably->auth->lastAuthOptions = null; // check if parameter overriding works correctly $ably->auth->authorize(['ttl' => 99999], ['queryTime' => false]); $expectedTokenParams = $tokenParams; // arrays are copied by value in PHP $expectedTokenParams['ttl'] = 99999; $expectedAuthOptions = $authOptions; $expectedAuthOptions['queryTime'] = false; $this->assertEquals($expectedTokenParams, $ably->auth->lastTokenParams, 'Expected authorize() to pass combined tokenParams to requestToken()'); $this->assertEquals($expectedAuthOptions, $ably->auth->lastAuthOptions, 'Expected authorize() to pass combined authOptions to requestToken()'); }
/** * Verify that time fails without valid host */ public function testTimeFailsWithInvalidHost() { $ablyInvalidHost = new AblyRest(['key' => 'fake.key:veryFake', 'restHost' => 'this.host.does.not.exist']); $this->setExpectedException('Ably\\Exceptions\\AblyRequestException'); $reportedTime = $ablyInvalidHost->time(); }