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

Fix test case in local environment #95

Merged
merged 2 commits into from
Mar 4, 2021
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
3 changes: 2 additions & 1 deletion src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public function request( $method, $url, $headers = [], $params = [] ) {

if ( $info['http_code'] < 200 || $info['http_code'] >= 300 ) {
$ablyCode = empty( $decodedBody->error->code ) ? $info['http_code'] * 100 : $decodedBody->error->code * 1;
$errorMessage = empty( $decodedBody->error->message ) ? 'cURL request failed' : $decodedBody->error->message;
$errorMessage = empty( $decodedBody->error->message ) ? 'cURL request failed'
: $decodedBody->error->message;

throw new AblyRequestException( $errorMessage, $ablyCode, $info['http_code'], $response );
}
Expand Down
12 changes: 10 additions & 2 deletions src/Models/HttpPaginatedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class HttpPaginatedResponse extends PaginatedResult {
* @param array $headers Headers to be sent with the request
* @throws AblyRequestException Thrown when the server and all the fallbacks are unreachable
*/
public function __construct( \Ably\AblyRest $ably, $model, $cipherParams, $method, $path, $params = [], $headers = [] ) {
public function __construct( \Ably\AblyRest $ably, $model, $cipherParams,
$method, $path, $params = [], $headers = [] ) {
try {
parent::__construct( $ably, $model, $cipherParams, $method, $path, $params, $headers );
} catch (AblyRequestException $ex) {
Expand Down Expand Up @@ -83,7 +84,14 @@ private function parseHeaders( $headers ) {
foreach($headers as $header) {
if(!trim($header)) continue;
list($key, $value) = explode(':', $header, 2);
$this->headers[trim($key)] = trim($value);
$key = trim($key);

// Title-Case
$key = preg_replace_callback('/\w+/', function ($match) {
return ucfirst(strtolower($match[0]));
}, $key);

$this->headers[$key] = trim($value);
}
}
}
16 changes: 10 additions & 6 deletions src/Utils/CurlWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ public function init( $url = null ) {
}

public function setOpt( $handle, $option, $value ) {
if ( $option == CURLOPT_URL ) $this->commands[(int) $handle]['url'] = $value;
else if ( $option == CURLOPT_POST && $value ) $this->commands[(int) $handle]['command'] .= '-X POST ';
else if ( $option == CURLOPT_CUSTOMREQUEST ) $this->commands[(int) $handle]['command'] .= '-X ' . $value . ' ';
else if ( $option == CURLOPT_POSTFIELDS ) $this->commands[(int) $handle]['command'] .= '--data "'. str_replace( '"', '\"', $value ) .'" ';
else if ( $option == CURLOPT_HTTPHEADER ) {
if ( $option == CURLOPT_URL ) {
$this->commands[(int) $handle]['url'] = $value;
} else if ( $option == CURLOPT_POST && $value ) {
$this->commands[(int) $handle]['command'] .= '-X POST ';
} else if ( $option == CURLOPT_CUSTOMREQUEST ) {
$this->commands[(int) $handle]['command'] .= '-X ' . $value . ' ';
} else if ( $option == CURLOPT_POSTFIELDS ) {
$this->commands[(int) $handle]['command'] .= '--data "'. str_replace( '"', '\"', $value ) .'" ';
} else if ( $option == CURLOPT_HTTPHEADER ) {
foreach($value as $header) {
$this->commands[(int) $handle]['command'] .= '-H "' . str_replace( '"', '\"', $header ).'" ';
}
Expand Down Expand Up @@ -61,4 +65,4 @@ public function getError( $handle ) {
public function getCommand( $handle ) {
return $this->commands[(int) $handle]['prefix'] . $this->commands[(int) $handle]['command'] . $this->commands[(int) $handle]['url'];
}
}
}
33 changes: 22 additions & 11 deletions tests/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,23 @@ public function testVersionHeaderPresence() {

$expectedVersion = '1.1';

$this->assertArrayHasKey( 'X-Ably-Version', $curlParams[CURLOPT_HTTPHEADER], 'Expected Ably version header in HTTP request' );
$this->assertEquals( $expectedVersion, $curlParams[CURLOPT_HTTPHEADER]['X-Ably-Version'], 'Expected Ably version in HTTP header to match AblyRest constant' );
$this->assertArrayHasKey( 'X-Ably-Version', $curlParams[CURLOPT_HTTPHEADER],
'Expected Ably version header in HTTP request' );
$this->assertEquals( $expectedVersion, $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->assertContains( 'php-' . $expectedVersion, $curlParams[CURLOPT_HTTPHEADER]['X-Ably-Lib'], 'Expected Ably lib in HTTP header to match AblyRest constant' );
$this->assertArrayHasKey( 'X-Ably-Lib', $curlParams[CURLOPT_HTTPHEADER],
'Expected Ably lib header in HTTP request' );
$this->assertContains( 'php-' . $expectedVersion, $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->assertContains( 'php-test-' . $expectedVersion, $curlParams[CURLOPT_HTTPHEADER]['X-Ably-Lib'], 'Expected X-Ably-Lib to contain library flavour string' );
$this->assertContains( 'php-test-' . $expectedVersion, $curlParams[CURLOPT_HTTPHEADER]['X-Ably-Lib'],
'Expected X-Ably-Lib to contain library flavour string' );

AblyRest::setLibraryFlavourString();
}
Expand Down Expand Up @@ -114,8 +119,10 @@ public function testPOST() {

$curlParams = $ably->http->getCurlLastParams();

$this->assertEquals( 'http://test.test/tokenRequest', $curlParams[CURLOPT_URL], 'Expected URL to match authUrl' );
$this->assertEquals( http_build_query($expectedParams), $curlParams[CURLOPT_POSTFIELDS], 'Expected POST params to contain encoded params' );
$this->assertEquals( 'http://test.test/tokenRequest', $curlParams[CURLOPT_URL],
'Expected URL to match authUrl' );
$this->assertEquals( http_build_query($expectedParams), $curlParams[CURLOPT_POSTFIELDS],
'Expected POST params to contain encoded params' );
}

/**
Expand All @@ -140,17 +147,21 @@ public function testRequestBasic() {

$this->assertTrue($res2->success, 'Expected retrieving the message via custom request to succeed');
$this->assertLessThan(300, $res2->statusCode, 'Expected statusCode < 300');
$this->assertArrayHasKey('Content-Type', $res2->headers, 'Expected headers to be an array containing key `Content-Type`');
$this->assertArrayHasKey('Content-Type', $res2->headers,
'Expected headers to be an array containing key `Content-Type`');
$this->assertEquals(1, count($res2->items), 'Expected to receive 1 message');
$this->assertEquals($msg->name, $res2->items[0]->name, 'Expected to receive matching message contents');
$this->assertEquals($msg->name, $res2->items[0]->name,
'Expected to receive matching message contents');

$res3 = $ably->request('GET', '/this-does-not-exist');

$this->assertEquals(404, $res3->statusCode, 'Expected statusCode 404');
$this->assertEquals(40400, $res3->errorCode, 'Expected errorCode 40400');
$this->assertNotEmpty($res3->errorMessage, 'Expected errorMessage to be set');
$this->assertArrayHasKey('X-Ably-Errorcode', $res3->headers, 'Expected X-Ably-Errorcode header to be present');
$this->assertArrayHasKey('X-Ably-Errormessage', $res3->headers, 'Expected X-Ably-Errormessage header to be present');
$this->assertArrayHasKey('X-Ably-Errorcode', $res3->headers,
'Expected X-Ably-Errorcode header to be present');
$this->assertArrayHasKey('X-Ably-Errormessage', $res3->headers,
'Expected X-Ably-Errormessage header to be present');
}

/**
Expand Down