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

Fixes problem with detecting HTTP dump request via HTTP user #112

Merged
merged 1 commit into from
Dec 5, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function priority(): int
private function isValidRequest(ServerRequestInterface $request): bool
{
return $request->getHeaderLine('X-Buggregator-Event') === 'http-dump'
|| $request->getAttribute('event-type') === 'http-dump';
|| $request->getAttribute('event-type') === 'http-dump'
|| $request->getUri()->getUserInfo() === 'http-dump';
}

public function handle(ServerRequestInterface $request, \Closure $next): ResponseInterface
Expand Down
2 changes: 1 addition & 1 deletion app/modules/Ray/Interfaces/Http/Handler/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ private function isValidRequest(ServerRequestInterface $request): bool
return $request->getHeaderLine('X-Buggregator-Event') === 'ray'
|| $request->getAttribute('event-type') === 'ray'
|| $request->getUri()->getUserInfo() === 'ray'
|| \str_starts_with($userAgent, 'Ray');
|| \str_starts_with(\strtolower($userAgent), 'ray');
}
}
7 changes: 6 additions & 1 deletion tests/App/Http/HttpFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ public function clearEvents(?string $type = null, ?array $uuids = null): Respons
);
}

public function __call(string $name, array $arguments): ResponseAssertions
public function __call(string $name, array $arguments): ResponseAssertions|self
{
if (!method_exists($this->http, $name)) {
throw new \Exception("Method $name does not exist");
}

if (\str_starts_with($name, 'with')) {
$this->http->$name(...$arguments);
return $this;
}

return $this->makeResponse(
$this->http->$name(...$arguments),
);
Expand Down
18 changes: 18 additions & 0 deletions tests/Feature/Interfaces/Http/HttpDumps/HttpDumpsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@

final class HttpDumpsActionTest extends ControllerTestCase
{
public function testHttpDumpViaHttpUser(): void
{
$this->http
->postJson(
uri: 'http:https://http-dump@localhost/',
data: ['foo' => 'bar'],
)
->assertOk();

$this->broadcastig->assertPushed('events', function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('http-dump', $data['data']['type']);
$this->assertSame('{"foo":"bar"}', $data['data']['payload']['request']['body']);

return true;
});
}

public function testHttpDumpsPost(): void
{
$this->http
Expand Down
39 changes: 34 additions & 5 deletions tests/Feature/Interfaces/Http/Inspector/InspectorActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,52 @@

final class InspectorActionTest extends ControllerTestCase
{
public const PAYLOAD = <<<'BODY'
W3sibW9kZWwiOiJ0cmFuc2FjdGlvbiIsIm5hbWUiOiJcL2ZvbyIsInR5cGUiOiJwcm9jZXNzIiwiaGFzaCI6Ijk3OWZmYmNlY2ZjZDNhNzJjMWM0ZDUzNmFhMWZlODViM2U5OTZkZjFkNzA5Mzc1NWI5YjRhMWRlZDhlMzNiNWMiLCJob3N0Ijp7Imhvc3RuYW1lIjoiQnV0c2Noc3RlckxwcCIsImlwIjoiMTI3LjAuMS4xIiwib3MiOiJMaW51eCJ9LCJ0aW1lc3RhbXAiOjE3MDE0NjQwMzkuNjUwNjIyLCJtZW1vcnlfcGVhayI6MTUuNTMsImR1cmF0aW9uIjowLjIyfSx7Im1vZGVsIjoic2VnbWVudCIsInR5cGUiOiJteS1wcm9jZXNzIiwiaG9zdCI6eyJob3N0bmFtZSI6IkJ1dHNjaHN0ZXJMcHAiLCJpcCI6IjEyNy4wLjEuMSIsIm9zIjoiTGludXgifSwidHJhbnNhY3Rpb24iOnsibmFtZSI6IlwvZm9vIiwiaGFzaCI6Ijk3OWZmYmNlY2ZjZDNhNzJjMWM0ZDUzNmFhMWZlODViM2U5OTZkZjFkNzA5Mzc1NWI5YjRhMWRlZDhlMzNiNWMiLCJ0aW1lc3RhbXAiOjE3MDE0NjQwMzkuNjUwNjIyfSwic3RhcnQiOjAuMiwidGltZXN0YW1wIjoxNzAxNDY0MDM5LjY1MDgyNiwiZHVyYXRpb24iOjAuMDF9XQ==
BODY;

public function testSendData(): void
{
$this->http
->post(
uri: '/',
data: Stream::create(
<<<'BODY'
W3sibW9kZWwiOiJ0cmFuc2FjdGlvbiIsIm5hbWUiOiJcL2ZvbyIsInR5cGUiOiJwcm9jZXNzIiwiaGFzaCI6Ijk3OWZmYmNlY2ZjZDNhNzJjMWM0ZDUzNmFhMWZlODViM2U5OTZkZjFkNzA5Mzc1NWI5YjRhMWRlZDhlMzNiNWMiLCJob3N0Ijp7Imhvc3RuYW1lIjoiQnV0c2Noc3RlckxwcCIsImlwIjoiMTI3LjAuMS4xIiwib3MiOiJMaW51eCJ9LCJ0aW1lc3RhbXAiOjE3MDE0NjQwMzkuNjUwNjIyLCJtZW1vcnlfcGVhayI6MTUuNTMsImR1cmF0aW9uIjowLjIyfSx7Im1vZGVsIjoic2VnbWVudCIsInR5cGUiOiJteS1wcm9jZXNzIiwiaG9zdCI6eyJob3N0bmFtZSI6IkJ1dHNjaHN0ZXJMcHAiLCJpcCI6IjEyNy4wLjEuMSIsIm9zIjoiTGludXgifSwidHJhbnNhY3Rpb24iOnsibmFtZSI6IlwvZm9vIiwiaGFzaCI6Ijk3OWZmYmNlY2ZjZDNhNzJjMWM0ZDUzNmFhMWZlODViM2U5OTZkZjFkNzA5Mzc1NWI5YjRhMWRlZDhlMzNiNWMiLCJ0aW1lc3RhbXAiOjE3MDE0NjQwMzkuNjUwNjIyfSwic3RhcnQiOjAuMiwidGltZXN0YW1wIjoxNzAxNDY0MDM5LjY1MDgyNiwiZHVyYXRpb24iOjAuMDF9XQ==
BODY,
),
data: Stream::create(self::PAYLOAD),
headers: [
'X-Buggregator-Event' => 'inspector',
],
)->assertOk();

$this->assertEvent();
}

public function testSendDataWithHttpUser(): void
{
$this->http
->post(
uri: 'http:https://inspector@localhost/',
data: Stream::create(self::PAYLOAD),
)->assertOk();

$this->assertEvent();
}

public function testSendDataWithClientHeaders(): void
{
$this->http
->post(
uri: '/',
data: Stream::create(self::PAYLOAD),
headers: [
'X-Inspector-Key' => 'test',
'X-Inspector-Version' => '1.0.0',
],
)->assertOk();

$this->assertEvent();
}

public function assertEvent(): void
{
$this->broadcastig->assertPushed('events', function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('inspector', $data['data']['type']);
Expand Down
82 changes: 59 additions & 23 deletions tests/Feature/Interfaces/Http/Ray/RayActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,64 @@

final class RayActionTest extends ControllerTestCase
{
public const PAYLOAD = <<<'JSON'
{"uuid":"11325003-b9cf-4c06-83d0-8a18fe368ac4","payloads":[{"type":"log","content":{"values":["foo"],"meta":[{"clipboard_data":"foo"}]},"origin":{"file":"\/root\/repos\/buggreagtor\/spiral-app\/tests\/Feature\/Interfaces\/Http\/Ray\/RayActionTest.php","line_number":13,"hostname":"ButschsterLpp"}}],"meta":{"php_version":"8.2.5","php_version_id":80205,"project_name":"","ray_package_version":"1.40.1.0"}}
JSON;

public function testSendDump(): void
{
$this->http->postJson(
uri: '/',
data: Stream::create(
<<<'JSON'
{"uuid":"11325003-b9cf-4c06-83d0-8a18fe368ac4","payloads":[{"type":"log","content":{"values":["foo"],"meta":[{"clipboard_data":"foo"}]},"origin":{"file":"\/root\/repos\/buggreagtor\/spiral-app\/tests\/Feature\/Interfaces\/Http\/Ray\/RayActionTest.php","line_number":13,"hostname":"ButschsterLpp"}}],"meta":{"php_version":"8.2.5","php_version_id":80205,"project_name":"","ray_package_version":"1.40.1.0"}}
JSON,
),
data: Stream::create(self::PAYLOAD),
headers: [
'X-Buggregator-Event' => 'ray',
],
)->assertOk();

$this->broadcastig->assertPushed('events', function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('ray', $data['data']['type']);

$this->assertSame('11325003-b9cf-4c06-83d0-8a18fe368ac4', $data['data']['payload']['uuid']);
$this->assertSame('8.2.5', $data['data']['payload']['meta']['php_version']);
$this->assertSame('1.40.1.0', $data['data']['payload']['meta']['ray_package_version']);
$this->assertEventSent();
}

public function testSendDumpViaHttpAuth(): void
{
$this->http->postJson(
uri: 'http:https://ray@localhost',
data: Stream::create(self::PAYLOAD),
)->assertOk();

$this->assertSame('log', $data['data']['payload']['payloads'][0]['type']);
$this->assertSame(['foo'], $data['data']['payload']['payloads'][0]['content']['values']);
$this->assertEventSent();
}

$this->assertNotEmpty($data['data']['uuid']);
$this->assertNotEmpty($data['data']['timestamp']);
public function testSendDumpViaUserAgent(): void
{
$this->http
->withServerVariables([
'HTTP_USER_AGENT' => 'ray 1.0.0',
])
->postJson(
uri: '/',
data: Stream::create(self::PAYLOAD),
)->assertOk();

return true;
});
$this->assertEventSent();
}

public function testSendDumpWithMerge(): void
{
$payload = <<<'JSON'
{"uuid":"11325003-b9cf-4c06-83d0-8a18fe368ac4","payloads":[{"type":"log","content":{"values":["foo"],"meta":[{"clipboard_data":"foo"}]},"origin":{"file":"\/root\/repos\/buggreagtor\/spiral-app\/tests\/Feature\/Interfaces\/Http\/Ray\/RayActionTest.php","line_number":13,"hostname":"ButschsterLpp"}}],"meta":{"php_version":"8.2.5","php_version_id":80205,"project_name":"","ray_package_version":"1.40.1.0"}}
JSON;
$payload = self::PAYLOAD;
$color = <<<'JSON'
{"uuid":"11325003-b9cf-4c06-83d0-8a18fe368ac4","payloads":[{"type":"color","content":{"color":"red"},"origin":{"file":"\/root\/repos\/buggreagtor\/spiral-app\/tests\/Feature\/Interfaces\/Http\/Ray\/RayActionTest.php","line_number":47,"hostname":"ButschsterLpp"}}],"meta":{"php_version":"8.2.5","php_version_id":80205,"project_name":"","ray_package_version":"1.40.1.0"}}
JSON;

$this->http->postJson(uri: '/', data: Stream::create($payload), headers: ['X-Buggregator-Event' => 'ray',],
$this->http->postJson(
uri: '/',
data: Stream::create($payload),
headers: ['X-Buggregator-Event' => 'ray',],
)->assertOk();
$this->broadcastig->reset();
$this->http->postJson(uri: '/', data: Stream::create($color), headers: ['X-Buggregator-Event' => 'ray',],
$this->http->postJson(
uri: '/',
data: Stream::create($color),
headers: ['X-Buggregator-Event' => 'ray',],
)->assertOk();

$this->broadcastig->assertPushed('events', function (array $data) {
Expand All @@ -79,4 +91,28 @@ public function testSendDumpWithMerge(): void
return true;
});
}

/**
* @return void
*/
public function assertEventSent(): void
{
$this->broadcastig->assertPushed('events', function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('ray', $data['data']['type']);

$this->assertSame('11325003-b9cf-4c06-83d0-8a18fe368ac4', $data['data']['payload']['uuid']);
$this->assertSame('8.2.5', $data['data']['payload']['meta']['php_version']);
$this->assertSame('1.40.1.0', $data['data']['payload']['meta']['ray_package_version']);


$this->assertSame('log', $data['data']['payload']['payloads'][0]['type']);
$this->assertSame(['foo'], $data['data']['payload']['payloads'][0]['content']['values']);

$this->assertNotEmpty($data['data']['uuid']);
$this->assertNotEmpty($data['data']['timestamp']);

return true;
});
}
}
Loading
Loading