-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,5 +56,10 @@ | |
"files": [ | ||
"lib/functions.php" | ||
] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Amp\\Dns\\Test\\": "test" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Amp\Dns\Test; | ||
|
||
use Amp\Dns; | ||
use Amp\Loop; | ||
use Amp\PHPUnit\TestCase; | ||
use Amp\Promise; | ||
use LibDNS\Messages\Message; | ||
use LibDNS\Messages\MessageTypes; | ||
use LibDNS\Records\QuestionFactory; | ||
|
||
abstract class SocketTest extends TestCase { | ||
abstract protected function connect(): Promise; | ||
|
||
public function testAsk() { | ||
Loop::run(function () { | ||
$question = (new QuestionFactory)->create(Dns\Record::A); | ||
$question->setName("google.com"); | ||
|
||
/** @var Dns\Internal\Socket $socket */ | ||
$socket = yield $this->connect(); | ||
|
||
/** @var Message $result */ | ||
$result = yield $socket->ask($question, 5000); | ||
|
||
$this->assertInstanceOf(Message::class, $result); | ||
$this->assertSame(MessageTypes::RESPONSE, $result->getType()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Amp\Dns\Test; | ||
|
||
use Amp\Delayed; | ||
use Amp\Dns; | ||
use Amp\Loop; | ||
use Amp\Promise; | ||
use LibDNS\Messages\Message; | ||
use LibDNS\Messages\MessageTypes; | ||
use LibDNS\Records\QuestionFactory; | ||
use function Amp\Promise\wait; | ||
|
||
class TcpSocketTest extends SocketTest { | ||
protected function connect(): Promise { | ||
return Dns\Internal\TcpSocket::connect("tcp://8.8.8.8:53"); | ||
} | ||
|
||
public function testTimeout() { | ||
$this->expectException(Dns\TimeoutException::class); | ||
wait(Dns\Internal\TcpSocket::connect("tcp://8.8.8.8:53", 0)); | ||
} | ||
|
||
public function testInvalidUri() { | ||
$this->expectException(Dns\ResolutionException::class); | ||
wait(Dns\Internal\TcpSocket::connect("tcp://8.8.8.8")); | ||
} | ||
|
||
public function testAfterConnectionTimedOut() { | ||
Loop::run(function () { | ||
$question = (new QuestionFactory)->create(Dns\Record::A); | ||
$question->setName("google.com"); | ||
|
||
/** @var Dns\Internal\Socket $socket */ | ||
$socket = yield $this->connect(); | ||
|
||
/** @var Message $result */ | ||
$result = yield $socket->ask($question, 3000); | ||
|
||
$this->assertInstanceOf(Message::class, $result); | ||
$this->assertSame(MessageTypes::RESPONSE, $result->getType()); | ||
|
||
// Google's DNS times out really fast | ||
yield new Delayed(3000); | ||
|
||
$this->expectException(Dns\ResolutionException::class); | ||
$this->expectExceptionMessage("Reading from the server failed"); | ||
|
||
yield $socket->ask($question, 3000); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Amp\Dns\Test; | ||
|
||
use Amp\Dns; | ||
use Amp\Promise; | ||
use function Amp\Promise\wait; | ||
|
||
class UdpSocketTest extends SocketTest { | ||
protected function connect(): Promise { | ||
return Dns\Internal\UdpSocket::connect("udp://8.8.8.8:53"); | ||
} | ||
|
||
public function testInvalidUri() { | ||
$this->expectException(Dns\ResolutionException::class); | ||
wait(Dns\Internal\UdpSocket::connect("udp://8.8.8.8")); | ||
} | ||
} |