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

Implement HTTP tests using httpbin.org #63

Merged
merged 7 commits into from
Sep 1, 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
4 changes: 2 additions & 2 deletions classes/http/Response.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function setHeaders($headers)
*/
public function getHeader($name)
{
return isset($this->headers[$name]) ? $this->headers[$name] : null;
return $this->headers[$name] ?? null;
}

/**
Expand All @@ -71,7 +71,7 @@ public function setResponseCode($responseCode)
/**
* Get response code
*/
public function getResponseCode()
public function getResponseCode(): int
{
return $this->responseCode;
}
Expand Down
8 changes: 5 additions & 3 deletions classes/utils/Http.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Nano\Http\Response;

/**
* Class with helper methods for HttpClient
*/
Expand All @@ -14,7 +16,7 @@ class Http
* @return Nano\Http\Response
* @throws Nano\Http\ResponseException
*/
public static function get($url, $query = [])
public static function get(string $url, array $query = []): Response
{
$client = new HttpClient();
$ret = $client->get($url, $query);
Expand All @@ -31,7 +33,7 @@ public static function get($url, $query = [])
* @return Nano\Http\Response
* @throws Nano\Http\ResponseException
*/
public static function post($url, $fields = [])
public static function post(string $url, array $fields = []): Response
{
$client = new HttpClient();
$ret = $client->post($url, $fields);
Expand All @@ -48,7 +50,7 @@ public static function post($url, $fields = [])
* @return Nano\Http\Response
* @throws Nano\Http\ResponseException
*/
public static function head($url, $query = [])
public static function head(string $url, array $query = []): Response
{
$client = new HttpClient();
$ret = $client->head($url, $query);
Expand Down
44 changes: 16 additions & 28 deletions classes/utils/HttpClient.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ public function __construct()
$info = curl_version();
$this->version = $info['version'];

$phpVersion = phpversion();

// set up cURL library
$this->handle = curl_init();

$this->logger = self::getLogger();

// set user agent
$this->setUserAgent('NanoPortal/' . Nano::VERSION . " libcurl/{$this->version}");
$this->setUserAgent('NanoPortal/' . Nano::VERSION . " libcurl/{$this->version}" . " php/{$phpVersion}");

curl_setopt_array($this->handle, [
CURLOPT_ENCODING => 'gzip',
Expand Down Expand Up @@ -84,10 +86,8 @@ public function __construct()

/**
* Get logger instance
*
* @return \Monolog\Logger
*/
protected static function getLogger()
protected static function getLogger(): \Monolog\Logger
{
static $logger;

Expand All @@ -113,7 +113,7 @@ public function close()
* @param string $proxy
* @param int $type
*/
public function setProxy($proxy, $type = CURLPROXY_HTTP)
public function setProxy(string $proxy, int $type = CURLPROXY_HTTP)
{
curl_setopt($this->handle, CURLOPT_PROXY, $proxy);
curl_setopt($this->handle, CURLOPT_PROXYTYPE, $type);
Expand All @@ -123,10 +123,8 @@ public function setProxy($proxy, $type = CURLPROXY_HTTP)

/**
* Set user agent identification used by HTTP client
*
* @param string $userAgent
*/
public function setUserAgent($userAgent)
public function setUserAgent(string $userAgent)
{
$this->userAgent = $userAgent;

Expand All @@ -145,11 +143,8 @@ public function getUserAgent()

/**
* Set request headers
**
* @param string $header
* @param string $value
*/
public function setRequestHeader($header, $value)
public function setRequestHeader(string $header, string $value)
{
$this->reqHeaders[] = "$header: $value";

Expand All @@ -158,10 +153,8 @@ public function setRequestHeader($header, $value)

/**
* Set timeout for a single request
*
* @param int $timeout
*/
public function setTimeout($timeout)
public function setTimeout(int $timeout)
{
$this->timeout = $timeout;

Expand All @@ -170,10 +163,8 @@ public function setTimeout($timeout)

/**
* Use given cookie jar file
*
* @param string $jarFile
*/
public function useCookieJar($jarFile)
public function useCookieJar(string $jarFile)
{
$this->logger->debug(__METHOD__, ['jar' => $jarFile]);

Expand All @@ -185,11 +176,8 @@ public function useCookieJar($jarFile)

/**
* Manually sets request cookie
*
* @param string $name
* @param string $value
*/
public function setCookie($name, $value)
public function setCookie(string $name, string $value)
{
$this->cookies[$name] = $value;

Expand All @@ -199,12 +187,12 @@ public function setCookie($name, $value)
/**
* Send GET HTTP request for a given URL
*
* @param $url
* @param string $url
* @param array $query
* @return Response
* @throws Nano\Http\ResponseException
*/
public function get($url, array $query = [])
public function get(string $url, array $query = []): Response
{
// add request params
if (!empty($query) && is_array($query)) {
Expand All @@ -222,7 +210,7 @@ public function get($url, array $query = [])
* @return Response
* @throws Nano\Http\ResponseException
*/
public function post($url, $fields = false)
public function post(string $url, $fields = false): Response
{
// add request POST fields
if (is_array($fields)) {
Expand All @@ -237,12 +225,12 @@ public function post($url, $fields = false)
/**
* Send HEAD HTTP request for a given URL
*
* @param $url
* @param string $url
* @param array $query
* @return Response
* @throws Nano\Http\ResponseException
*/
public function head($url, array $query = [])
public function head(string $url, array $query = []): Response
{
// add request params
if (!empty($query)) {
Expand All @@ -260,7 +248,7 @@ public function head($url, array $query = [])
* @return Response
* @throws Nano\Http\ResponseException
*/
private function sendRequest($method, $url)
private function sendRequest(string $method, string $url): Response
{
// send requested type of HTTP request
curl_setopt($this->handle, CURLOPT_POST, false);
Expand Down
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"ext-curl": "*",
"ext-mbstring": "*",
"ext-gd": "*",
"ext-json": "*",
"ext-xml": "*",
"predis/predis": "^1.1",
"monolog/monolog": "2.3.2",
"macbre/monolog-utils": "^2.1",
"tedivm/jshrink": "^1.2",
"phpunit/phpunit": "9.x"
"tedivm/jshrink": "^1.2"
},
"autoload": {
"classmap": ["classes/", "classes/utils/", "tests/app/classes/", "tests/CacheTest.php"],
Expand All @@ -33,10 +33,10 @@
},
"scripts": {
"test": [
"phpunit --verbose"
"phpunit --testdox --verbose"
],
"coverage": [
"XDEBUG_MODE=coverage phpunit --coverage-html=.coverage --coverage-clover=.coverage.xml --coverage-text --verbose"
"XDEBUG_MODE=coverage phpunit --coverage-html=.coverage --coverage-clover=.coverage.xml --coverage-text --testdox --verbose"
],
"lint": [
"php-cs-fixer fix --config=.php-cs-fixer.php --dry-run --verbose",
Expand All @@ -47,7 +47,9 @@
]
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"psr/cache": "^1.0.0",
"phpstan/phpstan": "^0.12.82",
"friendsofphp/php-cs-fixer": "^3.0"
"phpunit/phpunit": "9.x"
}
}
Loading