Skip to content
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
41 changes: 36 additions & 5 deletions src/Libraries/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class HttpClient
*/
private $data = null;

/**
* @var array
*/
private array $requestHeaders = [];

/**
* @var array
*/
Expand Down Expand Up @@ -206,13 +211,11 @@ public function getRequestHeaders(string $header = null)
{
$this->ensureSingleRequest();

$requestHeaders = $this->formatHeaders($this->client->getRequestHeaders());

if ($header) {
return $requestHeaders[$header] ?? null;
if ($header !== null) {
return $this->requestHeaders[$header] ?? null;
}

return $requestHeaders;
return $this->requestHeaders;
}

/**
Expand Down Expand Up @@ -325,6 +328,8 @@ public function __call(string $method, array $arguments): HttpClient
throw HttpClientException::methodNotSupported($method, get_class($this->client));
}

$this->interceptCall($method, $arguments);

$this->client->$method(...$arguments);

return $this;
Expand Down Expand Up @@ -366,6 +371,10 @@ private function handleResponse(Curl $instance)
];
}

/**
* @param CaseInsensitiveArray $headers
* @return array
*/
private function formatHeaders(CaseInsensitiveArray $headers): array
{
$formatted = [];
Expand All @@ -387,4 +396,26 @@ private function ensureSingleRequest(): void
throw HttpClientException::methodNotSupported(__METHOD__, MultiCurl::class);
}
}

/**
* @param string $method
* @param array $arguments
* @return void
*/
private function interceptCall(string $method, array $arguments): void
{
switch ($method) {
case 'setHeaders':
if (isset($arguments[0]) && is_array($arguments[0])) {
$this->requestHeaders = array_change_key_case($arguments[0], CASE_LOWER);
}
break;

case 'setHeader':
if (isset($arguments[0], $arguments[1])) {
$this->requestHeaders[strtolower($arguments[0])] = $arguments[1];
}
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,33 @@ public function get(string $filename)
public function put(string $filename, string $content, ?string $parentId = null)
{
try {
if($this->isFile($filename)){
return $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_MEDIA_URL . '/' . $filename . '?uploadType=media', $content, 'PATCH', 'application/octet-stream');
}else{
if ($this->isFile($filename)) {
$fileId = $filename;
} else {
$data = [
'name' => $filename,
'parents' => $parentId ? [$parentId] : ['root']
'name' => $filename,
'parents' => $parentId ? [$parentId] : ['root'],
];

$newFile = $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL, $data);
$newFile = $this->googleDriveApp->rpcRequest(
GoogleDriveApp::FILE_METADATA_URL,
$data
);

return $this->put($newFile->id, $content);
if (!isset($newFile->id)) {
throw new Exception('Google Drive file creation failed');
}

$fileId = $newFile->id;
}

return $this->googleDriveApp->rpcRequest(
GoogleDriveApp::FILE_MEDIA_URL . '/' . $fileId . '?uploadType=media',
$content,
'PUT',
'application/octet-stream'
);

} catch (Exception $e) {
return false;
}
Expand Down
22 changes: 18 additions & 4 deletions tests/Unit/Libraries/HttpClient/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ function ($instance) {
$this->assertEquals(405, $instance->getErrorCode());
}
)
->setOpt(CURLOPT_SSL_VERIFYPEER, false)
->setOpt(CURLOPT_SSL_VERIFYHOST, false)
->setHeader('x-api-key', $this->restServerApiKey)
->addGet($this->restServer . '/breeds')
->addPost($this->restServer . '/breeds')
Expand Down Expand Up @@ -147,6 +149,8 @@ public function testHttpClientGetResponseHeaders()
$this->httpClient
->createRequest($this->restServer . '/breeds/?limit=10&page=0')
->setHeader('x-api-key', $this->restServerApiKey)
->setOpt(CURLOPT_SSL_VERIFYPEER, false)
->setOpt(CURLOPT_SSL_VERIFYHOST, false)
->setOpt(CURLOPT_FOLLOWLOCATION, true)
->start();

Expand All @@ -161,6 +165,8 @@ public function testHttpClientGetResponseCookies()
{
$this->httpClient
->createRequest('https://www.youtube.com/')
->setOpt(CURLOPT_SSL_VERIFYPEER, false)
->setOpt(CURLOPT_SSL_VERIFYHOST, false)
->start();

$responseCookies = $this->httpClient->getResponseCookies();
Expand Down Expand Up @@ -189,6 +195,8 @@ public function testHttpClientGetObjectResponseBody()
$this->httpClient
->createRequest($this->restServer . '/breeds/?limit=10&page=0')
->setHeader('x-api-key', $this->restServerApiKey)
->setOpt(CURLOPT_SSL_VERIFYPEER, false)
->setOpt(CURLOPT_SSL_VERIFYHOST, false)
->setOpt(CURLOPT_FOLLOWLOCATION, true)
->start();

Expand All @@ -204,6 +212,8 @@ public function testHttpClientSendPostRequestAndGetResponseBody()
$this->httpClient
->createRequest($this->restServer . '/images/search?format=json&limit=10')
->setHeader('x-api-key', $this->restServerApiKey)
->setOpt(CURLOPT_SSL_VERIFYPEER, false)
->setOpt(CURLOPT_SSL_VERIFYHOST, false)
->setOpt(CURLOPT_FOLLOWLOCATION, true)
->start();

Expand All @@ -213,6 +223,8 @@ public function testHttpClientSendPostRequestAndGetResponseBody()

$this->httpClient
->createRequest($this->restServer . '/votes')
->setOpt(CURLOPT_SSL_VERIFYPEER, false)
->setOpt(CURLOPT_SSL_VERIFYHOST, false)
->setHeader('x-api-key', $this->restServerApiKey)
->setHeader('Content-Type', 'application/json')
->setMethod('POST')
Expand All @@ -237,21 +249,23 @@ public function testHttpClientGetError()
{
$this->httpClient
->createRequest('https://test.comx')
->setOpt(CURLOPT_SSL_VERIFYPEER, false)
->setOpt(CURLOPT_SSL_VERIFYHOST, false)
->start();

$errors = $this->httpClient->getErrors();

$this->assertIsArray($errors);

$this->assertEquals(6, $errors['code']);

$this->assertStringContainsString('CURLE_COULDNT_RESOLVE_HOST', $errors['message']);
}

public function testHttpClientMultiRequestGetError()
{
$this->httpClient
->createMultiRequest()
->setOpt(CURLOPT_SSL_VERIFYPEER, false)
->setOpt(CURLOPT_SSL_VERIFYHOST, false)
->addPut('https://test.comx/put')
->addPatch('https://test.comx/patch')
->start();
Expand All @@ -263,14 +277,14 @@ public function testHttpClientMultiRequestGetError()
$this->assertCount(2, $errors);

$this->assertEquals(6, $errors[0]['code']);

$this->assertStringContainsString('CURLE_COULDNT_RESOLVE_HOST', $errors[0]['message']);
}

public function testHttpClientCurlInfo()
{
$this->httpClient
->createRequest($this->restServer . '/breeds')
->setOpt(CURLOPT_SSL_VERIFYPEER, false)
->setOpt(CURLOPT_SSL_VERIFYHOST, false)
->setHeader('x-api-key', $this->restServerApiKey)
->start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function testDropboxListDirectory()

self::$response = [];

$this->assertFalse($this->fs->listDirectory('test'));
$this->assertNull($this->fs->listDirectory('test'));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GoogleDriveFileSystemAdapterTest extends AppTestCase
/**
* @var string
*/
private $content = 'This file was created via dropbox api';
private $content = 'This file was created via gdrive api';

/**
* @var array
Expand All @@ -50,7 +50,9 @@ public function setUp(): void
if(str_contains($endpoint, '?alt=media')){
return self::$response[array_key_last(self::$response)];
}

self::$response = array_merge(self::$response, (array)$params);

return (object)self::$response;
});

Expand Down Expand Up @@ -161,7 +163,7 @@ public function testGoogleDriveFileSize()

self::$response['size'] = strlen($text);

$this->assertEquals(10, $this->fs->size($this->filename));
$this->assertEquals(strlen($text), $this->fs->size($this->filename));
}

public function testGoogleDriveFileLastModified()
Expand All @@ -172,9 +174,10 @@ public function testGoogleDriveFileLastModified()

self::$response['modifiedTime'] = $modified;

$this->assertIsInt($this->fs->lastModified($this->filename));
$result = $this->fs->lastModified($this->filename);

$this->assertEquals(strtotime($modified), $this->fs->lastModified($this->filename));
$this->assertIsInt($result);
$this->assertEquals(strtotime($modified), $result);
}

public function testGoogleDriveListDirectory()
Expand Down Expand Up @@ -212,7 +215,7 @@ public function testGoogleDriveListDirectory()

self::$response = [];

$this->assertFalse($this->fs->listDirectory('test'));
$this->assertNull($this->fs->listDirectory('test'));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function testLocalListDirectory()

$this->assertIsString(current($entries));

$this->assertFalse($this->fs->listDirectory('nowhere'));
$this->assertEmpty($this->fs->listDirectory('nowhere'));
}

public function testLocalGlob()
Expand Down