1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More; 7 8use IO::Async::Test; 9use IO::Async::Loop; 10 11BEGIN { 12 # Install a fake resolver 13 *IO::Async::Resolver::DNS::res_search = 14 *IO::Async::Resolver::DNS::res_query = sub { 15 my ( $dname, $class, $type ) = @_; 16 my $packet = Net::DNS::Packet->new( $dname, $type, $class ); 17 $packet->push( answer => Net::DNS::RR->new( "example.com. 86400 MX 10 mail.example.com." ) ); 18 $packet->push( answer => Net::DNS::RR->new( "example.com. 86400 MX 20 mail.backuphost.net." ) ); 19 $packet->push( additional => Net::DNS::RR->new( "mail.example.com. 86400 A 10.0.0.1" ) ); 20 $packet->push( additional => Net::DNS::RR->new( "mail.example.com. 86400 A 10.0.0.2" ) ); 21 $packet->push( additional => Net::DNS::RR->new( "mail.example.com. 86400 AAAA fd00::1" ) ); 22 return $packet->data; 23 }; 24} 25 26use IO::Async::Resolver::DNS; 27 28my $loop = IO::Async::Loop->new; 29 30testing_loop( $loop ); 31 32my $resolver = $loop->resolver; 33 34my ( $pkt, @mx ) = $resolver->res_query( 35 dname => "example.com", 36 type => "MX", 37)->get; 38 39isa_ok( $pkt, "Net::DNS::Packet", '$pkt from ->res_query isa Net::DNS::Packet' ); 40 41is_deeply( \@mx, 42 [ { exchange => "mail.example.com", 43 preference => 10, 44 address => [ "10.0.0.1", "10.0.0.2", "fd00:0:0:0:0:0:0:1" ] }, 45 { exchange => "mail.backuphost.net", 46 preference => 20 } ], 47 'Sorted and processed MX records' ); 48 49done_testing; 50