Skip to content

Commit 58ca7b9

Browse files
authored
Merge pull request #36 from LadySolveig/3.x/contect-header-response
Fix: case insesitive Content-Type header check for json response
2 parents 4e718f1 + d2b2ea4 commit 58ca7b9

File tree

2 files changed

+8
-23
lines changed

2 files changed

+8
-23
lines changed

Tests/ClientTest.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Joomla\Application\WebApplicationInterface;
1111
use Joomla\Http\Http;
12+
use Joomla\Http\Response;
1213
use Joomla\Input\Input;
1314
use Joomla\OAuth2\Client;
1415
use Joomla\Registry\Registry;
@@ -34,7 +35,7 @@ class ClientTest extends TestCase
3435
*
3536
* @var Http|MockObject
3637
*/
37-
protected $client;
38+
protected $http;
3839

3940
/**
4041
* The input object to use in retrieving GET/POST data.
@@ -348,11 +349,7 @@ public function testRefreshTokenJson()
348349
*/
349350
public function encodedGrantOauthCallback($url, $data, ?array $headers = null, $timeout = null)
350351
{
351-
$response = new \stdClass();
352-
353-
$response->code = 200;
354-
$response->headers = ['Content-Type' => 'x-www-form-urlencoded'];
355-
$response->body = 'access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600';
352+
$response = new Response('data://text/plain,access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600', 200, ['Content-Type' => 'x-www-form-urlencoded']);
356353

357354
return $response;
358355
}
@@ -369,11 +366,7 @@ public function encodedGrantOauthCallback($url, $data, ?array $headers = null, $
369366
*/
370367
public function jsonGrantOauthCallback($url, $data, ?array $headers = null, $timeout = null)
371368
{
372-
$response = new \stdClass();
373-
374-
$response->code = 200;
375-
$response->headers = ['Content-Type' => 'application/json'];
376-
$response->body = '{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}';
369+
$response = new Response('data://text/plain,{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}', 200, ['Content-Type' => 'application/json']);
377370

378371
return $response;
379372
}
@@ -390,11 +383,7 @@ public function jsonGrantOauthCallback($url, $data, ?array $headers = null, $tim
390383
*/
391384
public function queryOauthCallback($url, $data, ?array $headers = null, $timeout = null)
392385
{
393-
$response = new \stdClass();
394-
395-
$response->code = 200;
396-
$response->headers = ['Content-Type' => 'text/html'];
397-
$response->body = 'Lorem ipsum dolor sit amet.';
386+
$response = new Response('data://text/plain,Lorem ipsum dolor sit amet.', 200, ['Content-Type' => 'text/html']);
398387

399388
return $response;
400389
}
@@ -410,11 +399,7 @@ public function queryOauthCallback($url, $data, ?array $headers = null, $timeout
410399
*/
411400
public function getOauthCallback($url, ?array $headers = null, $timeout = null)
412401
{
413-
$response = new \stdClass();
414-
415-
$response->code = 200;
416-
$response->headers = ['Content-Type' => 'text/html'];
417-
$response->body = 'Lorem ipsum dolor sit amet.';
402+
$response = new Response('data://text/plain,Lorem ipsum dolor sit amet.', 200, ['Content-Type' => 'text/html']);
418403

419404
return $response;
420405
}

src/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function authenticate()
112112
);
113113
}
114114

115-
if (strpos($response->headers['Content-Type'], 'application/json') !== false) {
115+
if (in_array('application/json', $response->getHeader('Content-Type'))) {
116116
$token = array_merge(json_decode($response->body, true), ['created' => time()]);
117117
} else {
118118
parse_str($response->body, $token);
@@ -380,7 +380,7 @@ public function refreshToken($token = null)
380380
);
381381
}
382382

383-
if (strpos($response->headers['Content-Type'], 'application/json') !== false) {
383+
if (in_array('application/json', $response->getHeader('Content-Type'))) {
384384
$token = array_merge(json_decode($response->body, true), ['created' => time()]);
385385
} else {
386386
parse_str($response->body, $token);

0 commit comments

Comments
 (0)