Skip to content

Commit

Permalink
Create Request.php (#87)
Browse files Browse the repository at this point in the history
* Create Request.php

* style: format code with PHP CS Fixer

This commit fixes the style issues introduced in 5d544c4 according to the output
from PHP CS Fixer.

Details: #87

* Update Request.php

* style: format code with PHP CS Fixer

This commit fixes the style issues introduced in aa4f5f6 according to the output
from PHP CS Fixer.

Details: #87

* Update Request.php

* Add tests

* style: format code with PHP CS Fixer

This commit fixes the style issues introduced in ec1be68 according to the output
from PHP CS Fixer.

Details: #87

* Fix code

---------

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
guibranco and deepsource-autofix[bot] authored Mar 20, 2024
1 parent c8b4804 commit a2aee58
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
137 changes: 137 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace GuiBranco\Pancake;

class Request
{
private function extractHeaders($header)
{
$headers = array();

foreach (explode("\r\n", $header) as $i => $line) {
$result = $this->extractHeader($i, $line);

if ($result === null) {
continue;
}

$headers[$result["key"]] = $result["value"];
}

return $headers;
}

private function extractHeader($i, $line)
{
if ($i === 0) {
return array("key" => "http_code", "value" => $line);
}

$explode = explode(": ", $line);

if (count($explode) != 2) {
return null;
}

list($key, $value) = $explode;
return array("key" => $key, "value" => $value);
}

private function execute($fields)
{
$curl = curl_init();
curl_setopt_array($curl, $fields);
$response = curl_exec($curl);
$result = new \stdCLass();
$result->url = $fields[CURLOPT_URL];

if ($response === false) {
$error = curl_error($curl);
curl_close($curl);

$result->statusCode = -1;
$result->error = $error;
return $result;
}

$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$headers = $this->extractHeaders($header);
$body = substr($response, $headerSize);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

$result->statusCode = $httpCode;
$result->headers = $headers;
$result->body = $body;
return $result;
}

private function getFields($url, $headers)
{
return array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $headers
);
}

public function get($url, $headers = array())
{
$fields = $this->getFields($url, $headers);
return $this->execute($fields);
}

public function post($url, $data, $headers = array())
{
$fields = $this->getFields($url, $headers);
$fields[CURLOPT_CUSTOMREQUEST] = "POST";
$fields[CURLOPT_POSTFIELDS] = $data;
return $this->execute($fields);
}

public function put($url, $data, $headers = array())
{
$fields = $this->getFields($url, $headers);
$fields[CURLOPT_CUSTOMREQUEST] = "PUT";
$fields[CURLOPT_POSTFIELDS] = $data;
return $this->execute($fields);
}

public function delete($url, $headers = array())
{
$fields = $this->getFields($url, $headers);
$fields[CURLOPT_CUSTOMREQUEST] = "DELETE";
return $this->execute($fields);
}

public function patch($url, $data, $headers = array())
{
$fields = $this->getFields($url, $headers);
$fields[CURLOPT_CUSTOMREQUEST] = "PATCH";
$fields[CURLOPT_POSTFIELDS] = $data;
return $this->execute($fields);
}

public function options($url, $headers = array())
{
$fields = $this->getFields($url, $headers);
$fields[CURLOPT_CUSTOMREQUEST] = "OPTIONS";
return $this->execute($fields);
}

public function head($url, $headers = array())
{
$fields = $this->getFields($url, $headers);
$fields[CURLOPT_CUSTOMREQUEST] = "HEAD";
return $this->execute($fields);
}
}
60 changes: 60 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace GuiBranco\Pancake\Tests;

use GuiBranco\Pancake\Request;
use PHPUnit\Framework\TestCase;

final class RequestTest extends TestCase
{
public function testCanGetExampleUrl(): void
{
$request = new Request();
$response = $request->get('https://httpbin.org/get');
$this->assertEquals(200, $response->statusCode);
}

public function testCanPostExampleUrl(): void
{
$request = new Request();
$response = $request->post('https://httpbin.org/post', ['name' => 'GuiBranco']);
$this->assertEquals(200, $response->statusCode);
}

public function testCanPutExampleUrl(): void
{
$request = new Request();
$response = $request->put('https://httpbin.org/put', ['name' => 'GuiBranco']);
$this->assertEquals(200, $response->statusCode);
}

public function testCanPatchExampleUrl(): void
{
$request = new Request();
$response = $request->patch('https://httpbin.org/patch', ['name' => 'GuiBranco']);
$this->assertEquals(200, $response->statusCode);
}

public function testCanDeleteExampleUrl(): void
{
$request = new Request();
$response = $request->delete('https://httpbin.org/delete');
$this->assertEquals(200, $response->statusCode);
}

public function testCanGetWithHeaders(): void
{
$request = new Request();
$response = $request->get('https://httpbin.org/headers', ['X-Test' => 'test']);
$this->assertEquals(200, $response->statusCode);
}

public function testCanPostWithHeaders(): void
{
$request = new Request();
$response = $request->post('https://httpbin.org/post', ['name' => 'GuiBranco'], ['X-Test' => 'test']);
$this->assertEquals(200, $response->statusCode);
}
}

0 comments on commit a2aee58

Please sign in to comment.