Skip to content

Commit

Permalink
Improve HttpRequestTrait for tests. (#3602)
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Apr 19, 2024
1 parent 5b495cc commit 9b24a50
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 16 deletions.
83 changes: 76 additions & 7 deletions module/VuFind/src/VuFindTest/Feature/HttpRequestTrait.php
Expand Up @@ -29,6 +29,8 @@

namespace VuFindTest\Feature;

use VuFindHttp\HttpService;

/**
* HTTP request helper methods for integration tests.
*
Expand All @@ -43,16 +45,20 @@ trait HttpRequestTrait
use RemoteCoverageTrait;

/**
* Perform an HTTP get operation with coverage awareness.
* HTTP service
*
* @param string $url URL to retrieve
* @var ?HttpService
*/
protected $httpService = null;

/**
* Get extra HTTP headers to support testing.
*
* @return \Laminas\Http\Response
* @return array
*/
protected function httpGet(string $url): \Laminas\Http\Response
protected function getExtraVuFindHttpRequestHeaders(): array
{
$http = new \VuFindHttp\HttpService();
$headers = ($coverageDir = $this->getRemoteCoverageDirectory())
return ($coverageDir = $this->getRemoteCoverageDirectory())
? [
'X-VuFind-Remote-Coverage' => json_encode(
[
Expand All @@ -62,6 +68,69 @@ protected function httpGet(string $url): \Laminas\Http\Response
]
),
] : [];
return $http->get($url, headers: $headers);
}

/**
* Get HTTP service.
*
* @return HttpService
*/
protected function getHttpService(): HttpService
{
if (!$this->httpService) {
$this->httpService = new HttpService();
}
return $this->httpService;
}

/**
* Perform an HTTP GET operation with coverage awareness.
*
* @param string $url Request URL
* @param array $params Request parameters
* @param float $timeout Request timeout in seconds
* @param array $headers Request headers
*
* @return \Laminas\Http\Response
*/
protected function httpGet(
$url,
array $params = [],
$timeout = null,
array $headers = []
): \Laminas\Http\Response {
return $this->getHttpService()->get(
$url,
$params,
$timeout,
array_merge($headers, $this->getExtraVuFindHttpRequestHeaders())
);
}

/**
* Perform an HTTP POST operation with coverage awareness.
*
* @param string $url Request URL
* @param mixed $body Request body document
* @param string $type Request body content type
* @param float $timeout Request timeout in seconds
* @param array $headers Request http-headers
*
* @return \Laminas\Http\Response
*/
protected function httpPost(
$url,
$body = null,
$type = 'application/octet-stream',
$timeout = null,
array $headers = []
): \Laminas\Http\Response {
return $this->getHttpService()->post(
$url,
$body,
$type,
$timeout,
array_merge($headers, $this->getExtraVuFindHttpRequestHeaders())
);
}
}
Expand Up @@ -45,6 +45,7 @@
final class OAuth2Test extends \VuFindTest\Integration\MinkTestCase
{
use \VuFindTest\Feature\DemoDriverTestTrait;
use \VuFindTest\Feature\HttpRequestTrait;
use \VuFindTest\Feature\LiveDatabaseTrait;
use \VuFindTest\Feature\UserCreationTrait;

Expand Down Expand Up @@ -215,8 +216,7 @@ public function testOAuth2Authorization(): void
'client_id' => 'test',
'client_secret' => 'mysecret',
];
$http = new \VuFindHttp\HttpService();
$response = $http->post(
$response = $this->httpPost(
$this->getVuFindUrl() . '/OAuth2/token',
http_build_query($tokenParams),
'application/x-www-form-urlencoded'
Expand All @@ -228,7 +228,7 @@ public function testOAuth2Authorization(): void
$this->assertArrayHasKey('token_type', $tokenResult);

// Fetch public key to verify idToken:
$response = $http->get($this->getVuFindUrl() . '/OAuth2/jwks');
$response = $this->httpGet($this->getVuFindUrl() . '/OAuth2/jwks');
$this->assertEquals(
200,
$response->getStatusCode(),
Expand Down Expand Up @@ -260,7 +260,7 @@ public function testOAuth2Authorization(): void
);

// Test the userinfo endpoint:
$response = $http->get(
$response = $this->httpGet(
$this->getVuFindUrl() . '/OAuth2/userinfo',
[],
'',
Expand Down Expand Up @@ -288,7 +288,7 @@ public function testOAuth2Authorization(): void

// Test token request with bad credentials:
$tokenParams['client_secret'] = 'badsecret';
$response = $http->post(
$response = $this->httpPost(
$this->getVuFindUrl() . '/OAuth2/token',
http_build_query($tokenParams),
'application/x-www-form-urlencoded'
Expand Down Expand Up @@ -460,8 +460,7 @@ public function testOIDCDiscovery(): void
'service_documentation' => 'https://vufind.org/wiki/configuration:oauth2_oidc',
];

$http = new \VuFindHttp\HttpService();
$response = $http->get($this->getVuFindUrl() . '/.well-known/openid-configuration');
$response = $this->httpGet($this->getVuFindUrl() . '/.well-known/openid-configuration');
$this->assertEquals(
'application/json',
$response->getHeaders()->get('Content-Type')->getFieldValue()
Expand Down
Expand Up @@ -45,6 +45,7 @@
final class ShibbolethLogoutNotificationTest extends \VuFindTest\Integration\MinkTestCase
{
use \VuFindTest\Feature\FixtureTrait;
use \VuFindTest\Feature\HttpRequestTrait;
use \VuFindTest\Feature\LiveDatabaseTrait;
use \VuFindTest\Feature\LiveDetectionTrait;

Expand Down Expand Up @@ -82,8 +83,7 @@ public function testLogoutNotification()
$table->addSessionMapping($sessionId, 'EXTERNAL_SESSION_ID');

// Call the notification endpoint:
$http = new \VuFindHttp\HttpService();
$result = $http->post(
$result = $this->httpPost(
$this->getVuFindUrl() . '/soap/shiblogout',
$this->getFixture('shibboleth/logout_notification.xml'),
'application/xml'
Expand Down

0 comments on commit 9b24a50

Please sign in to comment.