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

chore: add new setters #3

Merged
merged 2 commits into from
Mar 18, 2024
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
64 changes: 64 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Client
private int $connectTimeout = 60;
private int $maxRedirects = 5;
private bool $allowRedirects = true;
private string $userAgent = '';

/**
* @param string $key
Expand Down Expand Up @@ -89,6 +90,18 @@ public function setConnectTimeout(int $connectTimeout): self
return $this;
}

/**
* Set the user agent.
*
* @param string $userAgent
* @return self
*/
public function setUserAgent(string $userAgent): self
{
$this->userAgent = $userAgent;
return $this;
}

/**
* Flatten request body array to PHP multiple format
*
Expand Down Expand Up @@ -169,6 +182,7 @@ public function fetch(
CURLOPT_MAXREDIRS => $this->maxRedirects,
CURLOPT_FOLLOWLOCATION => $this->allowRedirects,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => $this->userAgent
];

// Merge user-defined CURL options with defaults
Expand Down Expand Up @@ -196,4 +210,54 @@ public function fetch(

return $response;
}

/**
* Get the request timeout.
*
* @return int
*/
public function getTimeout(): int
{
return $this->timeout;
}

/**
* Get whether redirects are allowed.
*
* @return bool
*/
public function getAllowRedirects(): bool
{
return $this->allowRedirects;
}

/**
* Get the maximum number of redirects.
*
* @return int
*/
public function getMaxRedirects(): int
{
return $this->maxRedirects;
}

/**
* Get the connection timeout.
*
* @return int
*/
public function getConnectTimeout(): int
{
return $this->connectTimeout;
}

/**
* Get the user agent.
*
* @return string
*/
public function getUserAgent(): string
{
return $this->userAgent;
}
}
99 changes: 85 additions & 14 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testFetch(

try {
$client = new Client();
foreach($headers as $key => $value) {
foreach ($headers as $key => $value) {
$client->addHeader($key, $value);
}

Expand All @@ -46,11 +46,11 @@ public function testFetch(
if ($resp->getStatusCode() === 200) { // If the response is OK
$respData = $resp->json(); // Convert body to array
$this->assertEquals($respData['method'], $method); // Assert that the method is equal to the response's method
if($method != Client::METHOD_GET) {
if(empty($body)) { // if body is empty then response body should be an empty string
if ($method != Client::METHOD_GET) {
if (empty($body)) { // if body is empty then response body should be an empty string
$this->assertEquals($respData['body'], '');
} else {
if($headers['content-type'] != "application/x-www-form-urlencoded") {
if ($headers['content-type'] != "application/x-www-form-urlencoded") {
$this->assertEquals( // Assert that the body is equal to the response's body
$respData['body'],
json_encode($body) // Converting the body to JSON string
Expand All @@ -65,15 +65,15 @@ public function testFetch(
); // Assert that the args are equal to the response's args
$respHeaders = json_decode($respData['headers'], true); // Converting the headers to array
$host = $respHeaders['Host'];
if(array_key_exists('Content-Type', $respHeaders)) {
if (array_key_exists('Content-Type', $respHeaders)) {
$contentType = $respHeaders['Content-Type'];
} else {
$contentType = $respHeaders['content-type'];
}
$contentType = explode(';', $contentType)[0];
$this->assertEquals($host, $url); // Assert that the host is equal to the response's host
if(empty($headers)) {
if(empty($body)) {
if (empty($headers)) {
if (empty($body)) {
$this->assertEquals($contentType, 'application/x-www-form-urlencoded');
} else {
$this->assertEquals($contentType, 'application/json');
Expand Down Expand Up @@ -113,7 +113,7 @@ public function testSendFile(
}
if ($resp->getStatusCode() === 200) { // If the response is OK
$respData = $resp->json(); // Convert body to array
if(isset($respData['method'])) {
if (isset($respData['method'])) {
$this->assertEquals($respData['method'], Client::METHOD_POST);
} // Assert that the method is equal to the response's method
$this->assertEquals($respData['url'], 'localhost:8000'); // Assert that the url is equal to the response's url
Expand Down Expand Up @@ -151,7 +151,7 @@ public function testGetFile(
try {
$client = new Client();
$resp = $client->fetch(
url: 'localhost:8000/'.$type,
url: 'localhost:8000/' . $type,
method: Client::METHOD_GET,
body: [],
query: []
Expand All @@ -163,7 +163,7 @@ public function testGetFile(
if ($resp->getStatusCode() === 200) { // If the response is OK
$data = fopen($path, 'rb');
$size = filesize($path);
if($data && $size) {
if ($data && $size) {
$contents = fread($data, $size);
fclose($data);
$this->assertEquals($resp->getBody(), $contents); // Assert that the body is equal to the expected file contents
Expand Down Expand Up @@ -200,6 +200,77 @@ public function testRedirect(): void
echo "Please configure your PHP inbuilt SERVER";
}
}

/**
* Test setting and getting the timeout.
* @return void
*/
public function testSetGetTimeout(): void
{
$client = new Client();
$timeout = 10;

$client->setTimeout($timeout);

$this->assertEquals($timeout, $client->getTimeout());
}

/**
* Test setting and getting the allowRedirects flag.
* @return void
*/
public function testSetGetAllowRedirects(): void
{
$client = new Client();
$allowRedirects = true;

$client->setAllowRedirects($allowRedirects);

$this->assertEquals($allowRedirects, $client->getAllowRedirects());
}

/**
* Test setting and getting the maxRedirects.
* @return void
*/
public function testSetGetMaxRedirects(): void
{
$client = new Client();
$maxRedirects = 5;

$client->setMaxRedirects($maxRedirects);

$this->assertEquals($maxRedirects, $client->getMaxRedirects());
}

/**
* Test setting and getting the connectTimeout.
* @return void
*/
public function testSetGetConnectTimeout(): void
{
$client = new Client();
$connectTimeout = 5;

$client->setConnectTimeout($connectTimeout);

$this->assertEquals($connectTimeout, $client->getConnectTimeout());
}

/**
* Test setting and getting the userAgent.
* @return void
*/
public function testSetGetUserAgent(): void
{
$client = new Client();
$userAgent = "MyCustomUserAgent/1.0";

$client->setUserAgent($userAgent);

$this->assertEquals($userAgent, $client->getUserAgent());
}

/**
* Data provider for testFetch
* @return array<string, array<mixed>>
Expand Down Expand Up @@ -258,12 +329,12 @@ public function sendFileDataSet(): array
{
return [
'imageFile' => [
__DIR__.'/resources/logo.png',
__DIR__ . '/resources/logo.png',
'image/png',
'logo.png'
],
'textFile' => [
__DIR__.'/resources/test.txt',
__DIR__ . '/resources/test.txt',
'text/plain',
'text.txt'
],
Expand All @@ -277,11 +348,11 @@ public function getFileDataset(): array
{
return [
'imageFile' => [
__DIR__.'/resources/logo.png',
__DIR__ . '/resources/logo.png',
'image'
],
'textFile' => [
__DIR__.'/resources/test.txt',
__DIR__ . '/resources/test.txt',
'text'
],
];
Expand Down
Loading