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

Fix the use of Joomla\Http\Response #20

Open
wants to merge 4 commits into
base: 2.0-dev
Choose a base branch
from
Open
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
52 changes: 23 additions & 29 deletions Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

use Joomla\Application\WebApplicationInterface;
use Joomla\Http\Http;
use Joomla\Http\Response;
use Joomla\Input\Input;
use Joomla\OAuth2\Client;
use Joomla\Registry\Registry;
use Laminas\Diactoros\StreamFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -33,7 +35,7 @@ class ClientTest extends TestCase
*
* @var Http|MockObject
*/
protected $client;
protected $http;

/**
* The input object to use in retrieving GET/POST data.
Expand Down Expand Up @@ -347,13 +349,11 @@ public function testRefreshTokenJson()
*/
public function encodedGrantOauthCallback($url, $data, array $headers = null, $timeout = null)
{
$response = new \stdClass;

$response->code = 200;
$response->headers = ['Content-Type' => 'x-www-form-urlencoded'];
$response->body = 'access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600';

return $response;
return new Response(
(new StreamFactory)->createStream('access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600'),
200,
['Content-Type' => 'x-www-form-urlencoded']
);
}

/**
Expand All @@ -368,13 +368,11 @@ public function encodedGrantOauthCallback($url, $data, array $headers = null, $t
*/
public function jsonGrantOauthCallback($url, $data, array $headers = null, $timeout = null)
{
$response = new \stdClass;

$response->code = 200;
$response->headers = ['Content-Type' => 'application/json'];
$response->body = '{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}';

return $response;
return new Response(
(new StreamFactory)->createStream('{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}'),
200,
['CONTENT-TYPE' => 'application/json']
);
}

/**
Expand All @@ -389,13 +387,11 @@ public function jsonGrantOauthCallback($url, $data, array $headers = null, $time
*/
public function queryOauthCallback($url, $data, array $headers = null, $timeout = null)
{
$response = new \stdClass;

$response->code = 200;
$response->headers = ['Content-Type' => 'text/html'];
$response->body = 'Lorem ipsum dolor sit amet.';

return $response;
return new Response(
(new StreamFactory)->createStream('Lorem ipsum dolor sit amet.'),
200,
['Content-Type' => 'text/html']
);
}

/**
Expand All @@ -409,12 +405,10 @@ public function queryOauthCallback($url, $data, array $headers = null, $timeout
*/
public function getOauthCallback($url, array $headers = null, $timeout = null)
{
$response = new \stdClass;

$response->code = 200;
$response->headers = ['Content-Type' => 'text/html'];
$response->body = 'Lorem ipsum dolor sit amet.';

return $response;
return new Response(
(new StreamFactory)->createStream('Lorem ipsum dolor sit amet.'),
200,
['Content-Type' => ['text/html']]
);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"require-dev": {
"joomla/coding-standards": "^3.0@dev",
"phpunit/phpunit": "^8.5|^9.0"
"phpunit/phpunit": "^8.5|^9.0",
"laminas/laminas-diactoros": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
30 changes: 15 additions & 15 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,25 @@ public function authenticate()

$response = $this->http->post($this->getOption('tokenurl'), $data);

if (!($response->code >= 200 && $response->code < 400))
if (!($response->getStatusCode() >= 200 && $response->getStatusCode() < 400))
{
throw new UnexpectedResponseException(
$response,
sprintf(
'Error code %s received requesting access token: %s.',
$response->code,
$response->body
$response->getStatusCode(),
(string) $response->getBody()
)
);
}

if (strpos($response->headers['Content-Type'], 'application/json') !== false)
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== false)
{
$token = array_merge(json_decode($response->body, true), ['created' => time()]);
$token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]);
}
else
{
parse_str($response->body, $token);
parse_str((string) $response->getBody(), $token);
$token = array_merge($token, ['created' => time()]);
}

Expand Down Expand Up @@ -274,14 +274,14 @@ public function query($url, $data = null, $headers = [], $method = 'get', $timeo
throw new \InvalidArgumentException('Unknown HTTP request method: ' . $method . '.');
}

if ($response->code < 200 || $response->code >= 400)
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 400)
{
throw new UnexpectedResponseException(
$response,
sprintf(
'Error code %s received requesting data: %s.',
$response->code,
$response->body
$response->getStatusCode(),
(string) $response->getBody()
)
);
}
Expand Down Expand Up @@ -394,25 +394,25 @@ public function refreshToken($token = null)

$response = $this->http->post($this->getOption('tokenurl'), $data);

if (!($response->code >= 200 || $response->code < 400))
if (!($response->getStatusCode() >= 200 || $response->getStatusCode() < 400))
{
throw new UnexpectedResponseException(
$response,
sprintf(
'Error code %s received refreshing token: %s.',
$response->code,
$response->body
$response->getStatusCode(),
(string) $response->getBody()
)
);
}

if (strpos($response->headers['Content-Type'], 'application/json') !== false)
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== false)
{
$token = array_merge(json_decode($response->body, true), ['created' => time()]);
$token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]);
}
else
{
parse_str($response->body, $token);
parse_str((string) $response->getBody(), $token);
$token = array_merge($token, ['created' => time()]);
}

Expand Down