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

feat(app sec): Modify Request class to pass specific headers #12

Merged
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
6 changes: 1 addition & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ As far as possible, we try to adhere to [Symfony guidelines](https://symfony.com

### Changed

- Modify `Client\AbstractClient` class to handle App Sec request

### Added

- Add `setHeaders` public method in `Client\HttpMessage\Request` class
- Modify `Client\AbstractClient` and `Client\HttpMessage\Request` classes to handle App Sec requests


---
Expand Down
6 changes: 3 additions & 3 deletions src/Client/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getRequestHandler(): RequestHandlerInterface
return $this->requestHandler;
}

public function getUrl(string $type = Constants::TYPE_API): string
public function getUrl(string $type = Constants::TYPE_REST): string
{
$url = Constants::TYPE_APPSEC === $type ? $this->appSecUrl : $this->url;

Expand All @@ -105,7 +105,7 @@ protected function request(
string $endpoint,
array $parameters = [],
array $headers = [],
string $type = Constants::TYPE_API
string $type = Constants::TYPE_REST
): array {
$method = strtoupper($method);
if (!in_array($method, $this->allowedMethods)) {
Expand Down Expand Up @@ -160,7 +160,7 @@ private function formatResponseBody(Response $response): array
return $decoded;
}

private function getFullUrl(string $endpoint, string $type = Constants::TYPE_API): string
private function getFullUrl(string $endpoint, string $type = Constants::TYPE_REST): string
{
return $this->getUrl($type) . ltrim($endpoint, '/');
}
Expand Down
14 changes: 5 additions & 9 deletions src/Client/HttpMessage/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace CrowdSec\Common\Client\HttpMessage;

use CrowdSec\Common\Constants;

/**
* Request that will be sent to CrowdSec.
*
Expand Down Expand Up @@ -40,11 +42,12 @@ public function __construct(
string $uri,
string $method,
array $headers = [],
array $parameters = []
array $parameters = [],
string $type = Constants::TYPE_REST
) {
$this->uri = $uri;
$this->method = $method;
$this->headers = array_merge($this->headers, $headers);
$this->headers = Constants::TYPE_APPSEC === $type ? $headers : array_merge($this->headers, $headers);
$this->parameters = $parameters;
}

Expand All @@ -62,11 +65,4 @@ public function getUri(): string
{
return $this->uri;
}

public function setHeaders(array $headers): Request
{
$this->headers = $headers;

return $this;
}
}
6 changes: 3 additions & 3 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ class Constants
*/
public const SCOPE_RANGE = 'range';
/**
* @var string The API type
* @var string The REST API type
*/
public const TYPE_API = 'api';
public const TYPE_REST = 'rest';
/**
* @var string The APPSEC type
* @var string The APPSEC API type
*/
public const TYPE_APPSEC = 'app_sec';
/**
Expand Down
35 changes: 26 additions & 9 deletions tests/Unit/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* @covers \CrowdSec\Common\Client\HttpMessage\Request::getUri
* @covers \CrowdSec\Common\Client\HttpMessage\Request::__construct
* @covers \CrowdSec\Common\Client\HttpMessage\AbstractMessage::getHeaders
* @covers \CrowdSec\Common\Client\HttpMessage\Request::setHeaders
*/
final class RequestTest extends TestCase
{
Expand Down Expand Up @@ -71,27 +70,45 @@ public function testConstructor()
$headers,
'Request headers should be set'
);
}

public function testSetHeaders()
{
$request = new Request(
'test-uri',
'POST',
['test' => 'test', 'User-Agent' => TestConstants::USER_AGENT_SUFFIX],
['foo' => 'bar']
['foo' => 'bar'],
'app_sec'
);

$request->setHeaders(['test' => 'test2']);

$headers = $request->getHeaders();
$params = $request->getParams();
$method = $request->getMethod();
$uri = $request->getUri();

$this->assertEquals(
'POST',
$method,
'Request method should be set'
);

$this->assertEquals(
'test-uri',
$uri,
'Request URI should be set'
);

$this->assertEquals(
['foo' => 'bar'],
$params,
'Request params should be set'
);

$this->assertEquals(
[
'test' => 'test2',
'User-Agent' => TestConstants::USER_AGENT_SUFFIX,
'test' => 'test',
],
$headers,
'Request headers should be set'
'Request headers should be set without Content-Type and Accept'
);
}
}
Loading