Alien-Base-ModuleBuild

 view release on metacpan or  search on metacpan

t/alien_base_modulebuild_repository_http.t  view on Meta::CPAN

use Test2::V0 -no_srand => 1;
use Alien::Base::ModuleBuild::Repository::HTTP;
use Path::Tiny qw( path );
use File::chdir;
use File::Temp;
use URI::file;

subtest 'network fetch' => sub {

  is(
    Alien::Base::ModuleBuild::Repository::HTTP->is_network_fetch,
    1
  );

};

subtest 'secure fetch' => sub {

  is(
    Alien::Base::ModuleBuild::Repository::HTTP->new( protocol => 'http' )->is_secure_fetch,
    F(),
  );

  is(
    Alien::Base::ModuleBuild::Repository::HTTP->new( protocol => 'https' )->is_secure_fetch,
    T(),
  );

  is(
    Alien::Base::ModuleBuild::Repository::HTTP->new( protocol => 'http', exact_filename => 'https://foo' )->is_secure_fetch,
    T(),
  );

  is(
    Alien::Base::ModuleBuild::Repository::HTTP->new( protocol => 'http', exact_filename => 'http://foo' )->is_secure_fetch,
    F(),
  );

};

subtest 'verify tls' => sub {

  local $ENV{ALIEN_DOWNLOAD_RULE};
  delete $ENV{ALIEN_DOWNLOAD_RULE};

  subtest 'HTTP::Tiny' => sub {

    my $new_args;

    my $mock = mock 'HTTP::Tiny' => (
      around => [
        new => sub {
          my($orig, $self, @args) = @_;
          $new_args = { @args };
          $self->$orig(@args);
        },
      ],
    );

    $ENV{ALIEN_DOWNLOAD_RULE} = 'warn';

    Alien::Base::ModuleBuild::Repository::HTTP->new->connection;

    is(
      $new_args,
      hash {
        field agent => match qr/^Alien-Base-ModuleBuild\/HTTP::Tiny\//;
        end;
      },
      'warn',
    );

    $ENV{ALIEN_DOWNLOAD_RULE} = 'digest_or_encrypt';
    $new_args = {};

    Alien::Base::ModuleBuild::Repository::HTTP->new->connection;

    is(
      $new_args,
      hash {
        field agent      => match qr/Alien-Base-ModuleBuild\/HTTP::Tiny\//;
        field verify_SSL => 1;
        end;
      },
      'digest_or_encrypt',
    );

  };

  subtest 'LWP' => sub {

    skip_all 'subtest requires LWP::UserAgent' unless eval { require LWP::UserAgent; 1 };

    my $new_args;

    my $mock = mock 'LWP::UserAgent' => (
      around => [
        new => sub {
          my($orig, $self, @args) = @_;
          $new_args = { @args };
          $self->$orig(@args);
        },
      ],
    );

    $ENV{ALIEN_DOWNLOAD_RULE} = 'warn';

    Alien::Base::ModuleBuild::Repository::HTTP->new( protocol_class => 'LWP::UserAgent' )->connection;

    is(
      $new_args,
      hash {
        field agent => match qr/^Alien-Base-ModuleBuild\/LWP::UserAgent\//;
        end;
      },
      'warn',
    );

    $ENV{ALIEN_DOWNLOAD_RULE} = 'digest_or_encrypt';
    $new_args = {};

    Alien::Base::ModuleBuild::Repository::HTTP->new( protocol_class => 'LWP::UserAgent' )->connection;

    is(
      $new_args,
      hash {
        field agent    => match qr/Alien-Base-ModuleBuild\/LWP::UserAgent\//;
        field ssl_opts => { verify_hostname => 1 };
        end;
      },
      'digest_or_encrypt',
    );

  };

};

my $index_path = path('corpus/alien_base_modulebuild_repository_http/index.html')->absolute->stringify;

my $mock = mock 'HTTP::Tiny' => (
  override => [
    get => sub {
      local $/ = undef;
      open my $fh, '<', $index_path or die "Could not open $index_path: $!";
      return {
        success => 1,
        content => <$fh>,
      };
    },
    mirror => sub {
      return {
        success => 1,
      };
    },
  ]
);

my $repo = Alien::Base::ModuleBuild::Repository::HTTP->new;

# replicated in utils.t
my $html = q#Some <a href=link>link text</a> stuff. And a little <A HREF="link2">different link text</a>. AN ALL CAPS TAG <A HREF="link3">ALL CAPS</A> <A HREF=link4>ALL CAPS NO QUOTES</A>. <!--  <a href="dont_follow.html">you can't see me!</a> -->#;
my $correct = [qw/link link2 link3 link4/];

subtest 'find linsk with xtor' => sub {
  no warnings 'once';
  skip_all "HTML::LinkExtor not detected"
    unless $Alien::Base::ModuleBuild::Repository::HTTP::Has_HTML_Parser;

  my @targets = $repo->find_links_preferred($html);
  is( \@targets, $correct, "parse HTML for anchor targets (HTML::LinkExtor)");

  my @disp_targets = $repo->find_links($html);
  is( \@disp_targets, $correct, "parse HTML for anchor targets (HTML::LinkExtor, dispatched)");
};

subtest 'find links with Text::Balanced' => sub {
  my @targets = $repo->find_links_textbalanced($html);
  is( \@targets, $correct, "parse HTML for anchor targets (Text::Balanced)");

  # force Text::Balanced in dispatcher
  $Alien::Base::ModuleBuild::Repository::HTTP::Has_HTML_Parser = 0;
  my @disp_targets = $repo->find_links($html);
  is( \@disp_targets, $correct, "parse HTML for anchor targets (Text::Balanced, dispatched)");
};

subtest 'connection() and protocol_class' => sub {
  subtest 'HTTP::Tiny' => sub {
    my $repo = Alien::Base::ModuleBuild::Repository::HTTP->new(



( run in 1.438 second using v1.01-cache-2.11-cpan-e1769b4cff6 )