File Coverage

blib/lib/Email/Stuffer/TestLinks.pm
Criterion Covered Total %
statement 33 33 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod n/a
total 44 44 100.0


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   300683 use warnings;
  1         14  
  1         31  
4 1     1   5  
  1         2  
  1         39  
5             our $VERSION = '0.03';
6              
7             use Test::More;
8 1     1   6 use Mojo::DOM;
  1         3  
  1         15  
9 1     1   903 use Email::Stuffer;
  1         174797  
  1         43  
10 1     1   10 use Class::Method::Modifiers qw/ install_modifier /;
  1         2  
  1         30  
11 1     1   5 use IO::Async::Loop;
  1         5  
  1         67  
12 1     1   755 use Net::Async::HTTP;
  1         23071  
  1         37  
13 1     1   620 use IO::Async::SSL;
  1         81122  
  1         75  
14 1     1   490 use URI;
  1         67034  
  1         52  
15 1     1   11 use Future::Utils qw( fmap_void );
  1         3  
  1         29  
16 1     1   6  
  1         2  
  1         730  
17             =head1 SYNOPSIS
18              
19             use Email::Stuffer::TestLinks;
20              
21             =head1 NAME
22              
23             Email::Stuffer::TestLinks - validates links in HTML emails sent by
24             Email::Stuffer>send_or_die()
25              
26             =head1 DESCRIPTION
27              
28             When this module is included in a test, it parses http links (<a href="xyz">...</a>)
29             and image links (<img src="xyz">) in every email sent through Email::Stuffer->send_or_die().
30             Each URI must be get a successful response code (200 range).
31             Page title must not contain 'error' or 'not found' for text/html content.
32             Image links must return an image content type.
33              
34             =cut
35              
36             install_modifier 'Email::Stuffer', after => send_or_die => sub {
37              
38             my $self = shift;
39              
40             my %urls;
41             $self->email->walk_parts(
42             sub {
43             my ($part) = @_;
44             return unless ($part->content_type && $part->content_type =~ /text\/html/i);
45             my $dom = Mojo::DOM->new($part->body);
46             push @{$urls{http}}, $dom->find('a')->map(attr => 'href')->compact->grep(sub { $_ !~ /^mailto:/ })->uniq->to_array->@*;
47             push @{$urls{image}}, $dom->find('img')->map(attr => 'src')->compact->uniq->to_array->@*;
48             });
49              
50             my @data = map {
51             my $type = $_;
52             map { [$type, $_] } $urls{$type}->@*
53             } keys %urls;
54              
55             my $loop = IO::Async::Loop->new();
56             $loop->add(my $http = Net::Async::HTTP->new(max_connections_per_host => 3));
57              
58             (
59             fmap_void {
60             my ($type, $url) = @$_;
61              
62             my $uri = URI->new($url);
63             unless ($uri->scheme) {
64             fail "$type link $url is an invalid uri";
65             return Future->done;
66             }
67              
68             $http->GET(URI->new($uri))->then(
69             sub {
70             my $response = shift;
71              
72             return Future->fail("Response code was " . $response->code) if ($response->code !~ /^2\d\d/);
73              
74             if ($response->content_type eq 'text/html') {
75             my $dom = Mojo::DOM->new($response->decoded_content);
76             if (my $title = $dom->at('title')) {
77             return Future->fail("Page title contains text '$1'") if $title->text =~ /(error|not found)/i;
78             }
79             }
80              
81             if ($type eq 'image') {
82             return Future->fail("Unexpected content type: " . $response->content_type) unless $response->content_type =~ /^image\//;
83             }
84              
85             return Future->done;
86             }
87             )->transform(
88             done => sub {
89             pass "$type link works ($url)";
90             },
91             fail => sub {
92             my $failure = shift;
93             fail "$type link $url does not work - $failure";
94             }
95             )->else(sub { Future->done })
96             }
97             foreach => \@data,
98             concurrent => 10
99             )->get;
100              
101             };
102              
103             1;