Skip to content

Commit

Permalink
Merge pull request #44 from jdavid/master
Browse files Browse the repository at this point in the history
Fixes issues #40 and #42
  • Loading branch information
mattheworiordan authored May 11, 2017
2 parents 97dbf28 + 955f5fd commit 03191a1
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 12 deletions.
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';

// 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',
] );
}
}
}

0 comments on commit 03191a1

Please sign in to comment.