Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issues #40 and #42 #44

Merged
merged 3 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
tmp/
config.php
vendor/
composer.lock
composer.lock

.*.swp
38 changes: 37 additions & 1 deletion src/Models/BaseMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,42 @@ public function fromJSON( $json, $keepOriginal = false ) {
$this->decode();
}

/**
* Creates and returns a new message from the given encoded message like object
* @param stdClass $obj Message-like object
* @param CipherParams|null $cipherParams
*/
public static function fromEncoded( $obj, CipherParams $cipherParams = null ) {
$class = get_called_class();

$msg = new $class();
if ($cipherParams != null) {
$msg->setCipherParams( $cipherParams );
}

foreach ($obj as $key => $value) {
if (property_exists( $class, $key )) {
$msg->$key = $value;
}
}

$msg->decode();

return $msg;
}

/**
* Creates and returns a new message from the given encoded message like object
* @param array $objs Array of Message-Like objects
* @param CipherParams|null $cipherParams
*/
public static function fromEncodedArray( $objs, CipherParams $cipherParams = null ) {
return array_map(
function( $obj ) use ($cipherParams) { return static::fromEncoded($obj, $cipherParams); },
$objs
);
}

/**
* Returns an encoded message as a stdClass ready for stringifying
*/
Expand Down Expand Up @@ -224,4 +260,4 @@ protected function clearFields() {
public function setCipherParams( CipherParams $cipherParams ) {
$this->cipherParams = $cipherParams;
}
}
}
9 changes: 7 additions & 2 deletions src/Models/ClientOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@ class ClientOptions extends AuthOptions {
/**
* @var integer connection timeout after which a next fallback host is used
*/
public $httpRequestTimeout = 15000;
public $httpRequestTimeout = 10000;

/**
* @var integer Max number of fallback host retries for HTTP requests that fail due to network issues or server problems
*/
public $httpMaxRetryCount = 3;

/**
* @var integer Max elapsed time in which fallback host retries for HTTP requests will be attempted
*/
public $httpMaxRetryDuration = 15000;

/**
* @var string a class that should be used for making HTTP connections
* To allow mocking in tests.
Expand Down Expand Up @@ -126,4 +131,4 @@ public function __construct( $options = [] ) {
$this->restHost = $this->environment . '-' . $this->restHost;
}
}
}
}
1 change: 1 addition & 0 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ protected function encode() {

return $msg;
}

}
2 changes: 1 addition & 1 deletion tests/ChannelMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function testPublishConnectionKey() {
$msg = new Message();
$msg->name = 'delegatedMsg';
$msg->data = 'test payload';
$msg->connectionKey = 'fake!realtime_key';
$msg->connectionKey = 'fake.realtime_key';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What error were you getting with the !. This is expected to fail anyway no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout error

you can see the error in the build for the master branch
https://travis-ci.org/ably/ably-php/jobs/218008466#L426

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we had the same issue with ably-python
it got fixed in this commit
ably/ably-python@e4078f2

but I understand there is still an issue server side, it should not timeout imho

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK


// publishing the message with an invalid key must fail
$this->setExpectedException( 'Ably\Exceptions\AblyException', '', 40006 );
Expand Down
76 changes: 71 additions & 5 deletions tests/CryptoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,44 @@ public function testMessageEncryptionAgainstFixture( $filename ) {
$decryptedExample->setCipherParams( $cipherParams );
$decryptedExample->fromJSON( $example->encrypted );

$this->assertEquals( $decodedExample->data, $decryptedExample->data, 'Expected unencrypted and decrypted message\'s contents to match' );
$this->assertEquals( $decodedExample->data, $decryptedExample->data,
'Expected unencrypted and decrypted message\'s contents to match' );

$decodedExample->setCipherParams( $cipherParams );
$encryptedJSON = json_decode( $decodedExample->toJSON() );
$this->assertEquals( $example->encrypted->data, $encryptedJSON->data, 'Expected encrypted and example encrypted message\'s contents to match' );
$this->assertEquals( $example->encrypted->data, $encryptedJSON->data,
'Expected encrypted and example encrypted message\'s contents to match' );
}
}

/**
* Tests Message:fromEncodedArray
*
* @dataProvider filenameProvider
*/
public function testMessageFromEncodedArray( $filename ) {
$fixture = json_decode( file_get_contents( $filename ) );

$cipherParams = Crypto::getDefaultParams([
'key' => $fixture->key,
'algorithm' => $fixture->algorithm,
'keyLength' => $fixture->keylength,
'mode' => $fixture->mode,
'iv' => $fixture->iv,
'base64Key' => true,
'base64Iv' => true,
]);

$encrypted = array_map( function ( $x ) { return $x->encrypted ; }, $fixture->items );
$encoded = array_map( function ( $x ) { return $x->encoded ; }, $fixture->items );

$encrypted = Message::fromEncodedArray( $encrypted, $cipherParams );
$encoded = Message::fromEncodedArray( $encoded );

foreach ($encrypted as $i => $decryptedExample) {
$decodedExample = $encoded[$i];
$this->assertEquals( $decodedExample->data, $decryptedExample->data,
'Expected unencrypted and decrypted message\'s contents to match' );
}
}

Expand Down Expand Up @@ -166,11 +199,44 @@ public function testPresenceMessageEncryptionAgainstFixture( $filename ) {
$decryptedExample->setCipherParams( $cipherParams );
$decryptedExample->fromJSON( $example->encrypted );

$this->assertEquals( $decodedExample->data, $decryptedExample->data, 'Expected unencrypted and decrypted message\'s contents to match' );
$this->assertEquals( $decodedExample->data, $decryptedExample->data,
'Expected unencrypted and decrypted message\'s contents to match' );

$decodedExample->setCipherParams( $cipherParams );
$encryptedJSON = json_decode( $decodedExample->toJSON() );
$this->assertEquals( $example->encrypted->data, $encryptedJSON->data, 'Expected encrypted and example encrypted message\'s contents to match' );
$this->assertEquals( $example->encrypted->data, $encryptedJSON->data,
'Expected encrypted and example encrypted message\'s contents to match' );
}
}

/**
* Tests PresenceMessage:fromEncodedArray
*
* @dataProvider filenameProvider
*/
public function testPresenceMessageFromEncodedArray( $filename ) {
$fixture = json_decode( file_get_contents( $filename ) );

$cipherParams = Crypto::getDefaultParams([
'key' => $fixture->key,
'algorithm' => $fixture->algorithm,
'keyLength' => $fixture->keylength,
'mode' => $fixture->mode,
'iv' => $fixture->iv,
'base64Key' => true,
'base64Iv' => true,
]);

$encrypted = array_map( function ( $x ) { return $x->encrypted ; }, $fixture->items );
$encoded = array_map( function ( $x ) { return $x->encoded ; }, $fixture->items );

$encrypted = PresenceMessage::fromEncodedArray( $encrypted, $cipherParams );
$encoded = PresenceMessage::fromEncodedArray( $encoded );

foreach ($encrypted as $i => $decryptedExample) {
$decodedExample = $encoded[$i];
$this->assertEquals( $decodedExample->data, $decryptedExample->data,
'Expected unencrypted and decrypted message\'s contents to match' );
}
}

Expand All @@ -180,4 +246,4 @@ public function filenameProvider() {
[ __DIR__ . '/../ably-common/test-resources/crypto-data-256.json'],
];
}
}
}
5 changes: 3 additions & 2 deletions tests/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ public function testClientOptionsType() {

$co = new \Ably\Models\ClientOptions();
$this->assertEquals( 4000, $co->httpOpenTimeout );
$this->assertEquals( 15000, $co->httpRequestTimeout );
$this->assertEquals( 10000, $co->httpRequestTimeout );
$this->assertEquals( 3, $co->httpMaxRetryCount );
$this->assertEquals( 15000, $co->httpMaxRetryDuration );
}

public function testAuthOptionsType() {
Expand Down Expand Up @@ -261,4 +262,4 @@ public function testHttpPaginatedResponseType() {
'headers',
] );
}
}
}