Skip to content

Commit

Permalink
Merge pull request #37 from remotelyliving/bug-fix
Browse files Browse the repository at this point in the history
Fix Dig response parsing when extra tabs are present
  • Loading branch information
Christian Thomas authored Oct 27, 2020
2 parents a99f2ee + acda41f commit b8647e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Resolvers/Dig.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static function parseDigResponseToRows(string $digResponse): array
private static function normalizeColumns(string $response): string
{
$keysRemoved = preg_replace('/;(.*)/m', ' ', trim($response));
$tabsRemoved = preg_replace('/(\t)/m', ' ', (string) $keysRemoved);
$tabsRemoved = preg_replace('/(\t+)/m', ' ', (string) $keysRemoved);
$breaksRemoved = preg_replace('/\s\s/m', '', (string) $tabsRemoved);
return (string) preg_replace('/(\(\s|(\s\)))/m', '', (string) $breaksRemoved);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/DigResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public static function ARecords(Hostname $hostname): string
EOT;
}

public static function ARecordsWithTabs(Hostname $hostname): string
{
return <<<EOT
{$hostname} 300 IN A 77.88.55.55
{$hostname} 300 IN A 77.88.55.60
{$hostname} 300 IN A 5.255.255.55
{$hostname} 300 IN A 5.255.255.60
EOT;
}

public static function anyRecords(Hostname $hostname): string
{
return <<<EOT
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Resolvers/DigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ public function testDoesQueryForSpecificRecords(): void
}
}

public function testParsesRecordsWithExtraTabsInFormatting(): void
{
$this->spatieDNS->method('getRecords')
->with(Entities\DNSRecordType::TYPE_A)
->willReturn(Tests\Fixtures\DigResponses::ARecordsWithTabs($this->hostname));

$records = $this->dig->getARecords((string) $this->hostname);
$this->assertCount(4, $records);
foreach ($records as $record) {
$this->assertTrue($record->getType()->equals(Entities\DNSRecordType::createA()));
}
}

public function testReturnsEmptyCollectionForUnsupportedQueryType(): void
{
$this->assertFalse(in_array(Entities\DNSRecordType::TYPE_PTR, Resolvers\Dig::SUPPORTED_QUERY_TYPES));
Expand Down

0 comments on commit b8647e6

Please sign in to comment.