Files
librenms-librenms/tests/Unit/ApiTransportTest.php
Tony Murray 0862496e26 Fix api transport mult-line parsing (#13469)
* API transport fix parsing
parse templates after parsing user options, not before

* API transport tests

* fix style and lint

* remove accidental item

* fix more type issues
2021-11-03 13:37:57 -05:00

63 lines
2.0 KiB
PHP

<?php
namespace LibreNMS\Tests\Unit;
use App\Models\AlertTransport;
use GuzzleHttp\Psr7\Response;
use LibreNMS\Config;
use LibreNMS\Tests\TestCase;
use LibreNMS\Tests\Traits\MockGuzzleClient;
class ApiTransportTest extends TestCase
{
use MockGuzzleClient;
public function testGetMultilineVariables(): void
{
/** @var AlertTransport $transport */
$transport = AlertTransport::factory()->api('text={{ $msg }}')->make();
$this->mockGuzzleClient([
new Response(200),
]);
$obj = ['msg' => "This is a multi-line\nalert."];
$opts = Config::get('alert.transports.' . $transport->transport_type);
$result = $transport->instance()->deliverAlert($obj, $opts);
$this->assertTrue($result);
$history = $this->guzzleRequestHistory();
$this->assertCount(1, $history);
$this->assertEquals('GET', $history[0]->getMethod());
$this->assertEquals('text=This%20is%20a%20multi-line%0Aalert.', $history[0]->getUri()->getQuery());
}
public function testPostMultilineVariables(): void
{
/** @var AlertTransport $transport */
$transport = AlertTransport::factory()->api(
'text={{ $msg }}',
'post',
'bodytext={{ $msg }}',
)->make();
$this->mockGuzzleClient([
new Response(200),
]);
$obj = ['msg' => "This is a post multi-line\nalert."];
$opts = Config::get('alert.transports.' . $transport->transport_type);
$result = $transport->instance()->deliverAlert($obj, $opts);
$this->assertTrue($result);
$history = $this->guzzleRequestHistory();
$this->assertCount(1, $history);
$this->assertEquals('POST', $history[0]->getMethod());
// FUBAR
$this->assertEquals('text=This%20is%20a%20post%20multi-line%0Aalert.', $history[0]->getUri()->getQuery());
$this->assertEquals("bodytext=This is a post multi-line\nalert.", (string) $history[0]->getBody());
}
}