Skip to content

Commit 1423c0e

Browse files
authored
Merge pull request #3 from utopia-php/add-new-setters
chore: add new setters
2 parents 21fb745 + 0557f52 commit 1423c0e

File tree

2 files changed

+149
-14
lines changed

2 files changed

+149
-14
lines changed

src/Client.php

+64
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Client
2929
private int $connectTimeout = 60;
3030
private int $maxRedirects = 5;
3131
private bool $allowRedirects = true;
32+
private string $userAgent = '';
3233

3334
/**
3435
* @param string $key
@@ -89,6 +90,18 @@ public function setConnectTimeout(int $connectTimeout): self
8990
return $this;
9091
}
9192

93+
/**
94+
* Set the user agent.
95+
*
96+
* @param string $userAgent
97+
* @return self
98+
*/
99+
public function setUserAgent(string $userAgent): self
100+
{
101+
$this->userAgent = $userAgent;
102+
return $this;
103+
}
104+
92105
/**
93106
* Flatten request body array to PHP multiple format
94107
*
@@ -169,6 +182,7 @@ public function fetch(
169182
CURLOPT_MAXREDIRS => $this->maxRedirects,
170183
CURLOPT_FOLLOWLOCATION => $this->allowRedirects,
171184
CURLOPT_RETURNTRANSFER => true,
185+
CURLOPT_USERAGENT => $this->userAgent
172186
];
173187

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

197211
return $response;
198212
}
213+
214+
/**
215+
* Get the request timeout.
216+
*
217+
* @return int
218+
*/
219+
public function getTimeout(): int
220+
{
221+
return $this->timeout;
222+
}
223+
224+
/**
225+
* Get whether redirects are allowed.
226+
*
227+
* @return bool
228+
*/
229+
public function getAllowRedirects(): bool
230+
{
231+
return $this->allowRedirects;
232+
}
233+
234+
/**
235+
* Get the maximum number of redirects.
236+
*
237+
* @return int
238+
*/
239+
public function getMaxRedirects(): int
240+
{
241+
return $this->maxRedirects;
242+
}
243+
244+
/**
245+
* Get the connection timeout.
246+
*
247+
* @return int
248+
*/
249+
public function getConnectTimeout(): int
250+
{
251+
return $this->connectTimeout;
252+
}
253+
254+
/**
255+
* Get the user agent.
256+
*
257+
* @return string
258+
*/
259+
public function getUserAgent(): string
260+
{
261+
return $this->userAgent;
262+
}
199263
}

tests/ClientTest.php

+85-14
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testFetch(
2929

3030
try {
3131
$client = new Client();
32-
foreach($headers as $key => $value) {
32+
foreach ($headers as $key => $value) {
3333
$client->addHeader($key, $value);
3434
}
3535

@@ -46,11 +46,11 @@ public function testFetch(
4646
if ($resp->getStatusCode() === 200) { // If the response is OK
4747
$respData = $resp->json(); // Convert body to array
4848
$this->assertEquals($respData['method'], $method); // Assert that the method is equal to the response's method
49-
if($method != Client::METHOD_GET) {
50-
if(empty($body)) { // if body is empty then response body should be an empty string
49+
if ($method != Client::METHOD_GET) {
50+
if (empty($body)) { // if body is empty then response body should be an empty string
5151
$this->assertEquals($respData['body'], '');
5252
} else {
53-
if($headers['content-type'] != "application/x-www-form-urlencoded") {
53+
if ($headers['content-type'] != "application/x-www-form-urlencoded") {
5454
$this->assertEquals( // Assert that the body is equal to the response's body
5555
$respData['body'],
5656
json_encode($body) // Converting the body to JSON string
@@ -65,15 +65,15 @@ public function testFetch(
6565
); // Assert that the args are equal to the response's args
6666
$respHeaders = json_decode($respData['headers'], true); // Converting the headers to array
6767
$host = $respHeaders['Host'];
68-
if(array_key_exists('Content-Type', $respHeaders)) {
68+
if (array_key_exists('Content-Type', $respHeaders)) {
6969
$contentType = $respHeaders['Content-Type'];
7070
} else {
7171
$contentType = $respHeaders['content-type'];
7272
}
7373
$contentType = explode(';', $contentType)[0];
7474
$this->assertEquals($host, $url); // Assert that the host is equal to the response's host
75-
if(empty($headers)) {
76-
if(empty($body)) {
75+
if (empty($headers)) {
76+
if (empty($body)) {
7777
$this->assertEquals($contentType, 'application/x-www-form-urlencoded');
7878
} else {
7979
$this->assertEquals($contentType, 'application/json');
@@ -113,7 +113,7 @@ public function testSendFile(
113113
}
114114
if ($resp->getStatusCode() === 200) { // If the response is OK
115115
$respData = $resp->json(); // Convert body to array
116-
if(isset($respData['method'])) {
116+
if (isset($respData['method'])) {
117117
$this->assertEquals($respData['method'], Client::METHOD_POST);
118118
} // Assert that the method is equal to the response's method
119119
$this->assertEquals($respData['url'], 'localhost:8000'); // Assert that the url is equal to the response's url
@@ -151,7 +151,7 @@ public function testGetFile(
151151
try {
152152
$client = new Client();
153153
$resp = $client->fetch(
154-
url: 'localhost:8000/'.$type,
154+
url: 'localhost:8000/' . $type,
155155
method: Client::METHOD_GET,
156156
body: [],
157157
query: []
@@ -163,7 +163,7 @@ public function testGetFile(
163163
if ($resp->getStatusCode() === 200) { // If the response is OK
164164
$data = fopen($path, 'rb');
165165
$size = filesize($path);
166-
if($data && $size) {
166+
if ($data && $size) {
167167
$contents = fread($data, $size);
168168
fclose($data);
169169
$this->assertEquals($resp->getBody(), $contents); // Assert that the body is equal to the expected file contents
@@ -200,6 +200,77 @@ public function testRedirect(): void
200200
echo "Please configure your PHP inbuilt SERVER";
201201
}
202202
}
203+
204+
/**
205+
* Test setting and getting the timeout.
206+
* @return void
207+
*/
208+
public function testSetGetTimeout(): void
209+
{
210+
$client = new Client();
211+
$timeout = 10;
212+
213+
$client->setTimeout($timeout);
214+
215+
$this->assertEquals($timeout, $client->getTimeout());
216+
}
217+
218+
/**
219+
* Test setting and getting the allowRedirects flag.
220+
* @return void
221+
*/
222+
public function testSetGetAllowRedirects(): void
223+
{
224+
$client = new Client();
225+
$allowRedirects = true;
226+
227+
$client->setAllowRedirects($allowRedirects);
228+
229+
$this->assertEquals($allowRedirects, $client->getAllowRedirects());
230+
}
231+
232+
/**
233+
* Test setting and getting the maxRedirects.
234+
* @return void
235+
*/
236+
public function testSetGetMaxRedirects(): void
237+
{
238+
$client = new Client();
239+
$maxRedirects = 5;
240+
241+
$client->setMaxRedirects($maxRedirects);
242+
243+
$this->assertEquals($maxRedirects, $client->getMaxRedirects());
244+
}
245+
246+
/**
247+
* Test setting and getting the connectTimeout.
248+
* @return void
249+
*/
250+
public function testSetGetConnectTimeout(): void
251+
{
252+
$client = new Client();
253+
$connectTimeout = 5;
254+
255+
$client->setConnectTimeout($connectTimeout);
256+
257+
$this->assertEquals($connectTimeout, $client->getConnectTimeout());
258+
}
259+
260+
/**
261+
* Test setting and getting the userAgent.
262+
* @return void
263+
*/
264+
public function testSetGetUserAgent(): void
265+
{
266+
$client = new Client();
267+
$userAgent = "MyCustomUserAgent/1.0";
268+
269+
$client->setUserAgent($userAgent);
270+
271+
$this->assertEquals($userAgent, $client->getUserAgent());
272+
}
273+
203274
/**
204275
* Data provider for testFetch
205276
* @return array<string, array<mixed>>
@@ -258,12 +329,12 @@ public function sendFileDataSet(): array
258329
{
259330
return [
260331
'imageFile' => [
261-
__DIR__.'/resources/logo.png',
332+
__DIR__ . '/resources/logo.png',
262333
'image/png',
263334
'logo.png'
264335
],
265336
'textFile' => [
266-
__DIR__.'/resources/test.txt',
337+
__DIR__ . '/resources/test.txt',
267338
'text/plain',
268339
'text.txt'
269340
],
@@ -277,11 +348,11 @@ public function getFileDataset(): array
277348
{
278349
return [
279350
'imageFile' => [
280-
__DIR__.'/resources/logo.png',
351+
__DIR__ . '/resources/logo.png',
281352
'image'
282353
],
283354
'textFile' => [
284-
__DIR__.'/resources/test.txt',
355+
__DIR__ . '/resources/test.txt',
285356
'text'
286357
],
287358
];

0 commit comments

Comments
 (0)