AnyEvent-FTP

 view release on metacpan or  search on metacpan

t/anyevent_ftp_client.t  view on Meta::CPAN

    prep_client( $client );

    $client->connect($config->{host}, $config->{port})->recv;
    $client->login($config->{user}, $config->{pass})->recv;
    $client->type('I')->recv;

    isa_ok $client->cwd($ENV{AEF_REMOTE})->recv, 'AnyEvent::FTP::Response';

    do {
      my $dir = $client->pwd->recv;
      is $dir, net_pwd($ENV{AEF_REMOTE}), "dir = " .net_pwd($ENV{AEF_REMOTE});
    };

    my $dirname = join '', map { chr(ord('a') + int(rand(23))) } (1..10);

    isa_ok $client->mkd($dirname)->recv, 'AnyEvent::FTP::Response';
    isa_ok $client->cwd($dirname)->recv, 'AnyEvent::FTP::Response';

    SKIP: {
      skip 'wu-ftpd throws an exception on empty directory', 2 if $detect->{wu};
      my $res = $client->nlst->recv;
      is $res, array { etc() };
      is scalar(@$res), 0, 'list empty';
      if(scalar(@$res) > 0)
      {
        diag "~~~ nlst ~~~";
        diag $_ for @$res;
        diag "~~~~~~~~~~~~";
      }
    };

    isa_ok $client->stor('foo.txt', \"here is some data eh\n")->recv, 'AnyEvent::FTP::Response';

    do {
      my $res = $client->nlst->recv;
      is $res, array { etc() };
      is scalar(@$res), 1, 'list not empty';
      is $res->[0], 'foo.txt';
    };

    do {
      my $res = $client->list->recv;
      is $res, array { etc() };
      is scalar(grep /foo.txt$/, @$res), 1, 'has foo.txt in listing';
    };

    do {
      my $data = '';
      isa_ok $client->retr('foo.txt', \$data)->recv, 'AnyEvent::FTP::Response';
      is $data, "here is some data eh\n", 'retr ok';
    };

    isa_ok $client->appe('foo.txt', \"line 2\n")->recv, 'AnyEvent::FTP::Response';

    do {
      my $data = '';
      isa_ok $client->retr('foo.txt', \$data)->recv, 'AnyEvent::FTP::Response';
      is $data, "here is some data eh\nline 2\n", 'retr ok';
    };

    isa_ok $client->rename('foo.txt', 'bar.txt')->recv, 'AnyEvent::FTP::Response';

    do {
      my $res = $client->nlst->recv;
      is $res, array { etc() };
      is scalar(@$res), 1, 'list not empty';
      is $res->[0], 'bar.txt';
    };

    do {
      my $res = $client->list->recv;
      is $res, array { etc() };
      is scalar(grep /bar.txt$/, @$res), 1, 'has bar.txt in listing';
    };

    do {
      my $data = "here is some data";
      isa_ok $client->retr('bar.txt', \$data, restart => do { use bytes; length $data})->recv, 'AnyEvent::FTP::Response';
      is $data, "here is some data eh\nline 2\n", 'rest, retr ok';
    };

    isa_ok $client->dele('bar.txt')->recv, 'AnyEvent::FTP::Response';

    # ...

    isa_ok $client->cdup->recv, 'AnyEvent::FTP::Response';
    isa_ok $client->rmd($dirname)->recv, 'AnyEvent::FTP::Response';
    isa_ok $client->quit->recv, 'AnyEvent::FTP::Response';
  }

};

subtest 'rmd' => sub {
  reset_timeout;
  skip_all 'requires client and server on localhost' if $ENV{AEF_REMOTE};

  our $config;
  $config->{dir} = tempdir( CLEANUP => 1 );

  my $client = AnyEvent::FTP::Client->new;

  prep_client( $client );

  $client->connect($config->{host}, $config->{port})->recv;
  $client->login($config->{user}, $config->{pass})->recv;
  $client->type('I')->recv;
  $client->cwd($config->{dir})->recv;

  do {
    my $dir_name = File::Spec->catdir($config->{dir}, 'foo');
    mkdir $dir_name;
    my $ret = eval { $client->rmd('foo')->recv; };
    diag $@ if $@;
    isa_ok $ret, 'AnyEvent::FTP::Response';
    ok !-d $dir_name, "dir removed: $dir_name";
    rmdir $dir_name if -d $dir_name;
  };

  $client->quit->recv;
};

t/anyevent_ftp_client.t  view on Meta::CPAN

      is $remote, $data, 'local/remote match';

      unlink $fn;

      ok !-e $fn, 'remote deleted';
    };

    $client->quit->recv;
  }
};

subtest 'stat' => sub {
  reset_timeout;
  my $client = AnyEvent::FTP::Client->new;

  prep_client( $client );
  our $config;
  our $detect;

  $client->connect($config->{host}, $config->{port})->recv;
  $client->login($config->{user}, $config->{pass})->recv;

  skip_all 'ncftp return code broken' if $detect->{nc};

  do {
    my $res = eval { $client->stat->recv };
    diag $@ if $@;
    isa_ok $res, 'AnyEvent::FTP::Response';
    my $code = eval { $res->code };
    diag $@ if $@;
    like $code, qr{^21[123]$}, 'code = ' . $code;
  };

  do {
    my $res = eval { $client->stat('/')->recv };
    diag $@ if $@;
    isa_ok $res, 'AnyEvent::FTP::Response';
    my $code = eval { $res->code };
    diag $@ if $@;
    like $code, qr{^21[123]$}, 'code = ' . $code;
  };

  SKIP: {
    skip 'wu-ftpd does not return [45]50 on bogus file', 2 if $detect->{wu};
    skip 'pure-FTPd does not return [45]50 on bogus file', 2 if $detect->{pu};
    skip 'vsftp does not return [45]50 on bogus file', 2 if $detect->{vs};
    skip 'IIS does not return [45]50 on bogus file', 2 if $detect->{ms};
    skip 'bftp does not return [45]50 on bogus file', 2 if $detect->{xb};
    eval { $client->stat('bogus')->recv };
    my $res = $@;
    isa_ok $res, 'AnyEvent::FTP::Response';
    my $code = eval { $res->code };
    diag $@ if $@;
    like $code, qr{^[45]50$}, 'code = ' . $code;
  };

  $client->quit->recv;

};

subtest 'rename' => sub {
  reset_timeout;
  skip_all 'requires client and server on localhost' if $ENV{AEF_REMOTE};

  our $config;
  $config->{dir} = tempdir( CLEANUP => 1 );

  my $client = AnyEvent::FTP::Client->new;

  prep_client( $client );

  $client->connect($config->{host}, $config->{port})->recv;
  $client->login($config->{user}, $config->{pass})->recv;
  $client->type('I')->recv;
  $client->cwd($config->{dir})->recv;

  do {
    my $from = File::Spec->catfile($config->{dir}, 'foo.txt');
    do { open my $fh, '>', $from; close $fh; };
    my $to   = File::Spec->catfile($config->{dir}, 'bar.txt');

    ok  -e $from, "EX: $from";
    ok !-e $to,   "NO: $to";

    my $res1 = eval { $client->rnfr($from)->recv };
    diag $@ if $@;
    isa_ok $res1, 'AnyEvent::FTP::Response';

    my $res2 = eval { $client->rnto($to)->recv };
    diag $@ if $@;
    isa_ok $res2, 'AnyEvent::FTP::Response';

    ok !-e $from, "NO: $from";
    ok  -e $to,   "EX: $to";
  };

  do {
    my $from = File::Spec->catfile($config->{dir}, 'pepper.txt');
    do { open my $fh, '>', $from; close $fh; };
    my $to   = File::Spec->catfile($config->{dir}, 'coke.txt');

    ok  -e $from, "EX: $from";
    ok !-e $to,   "NO: $to";

    my $res = eval { $client->rename($from, $to)->recv };
    diag $@ if $@;
    isa_ok $res, 'AnyEvent::FTP::Response';

    ok !-e $from, "NO: $from";
    ok  -e $to,   "EX: $to";
  };

  $client->quit->recv;
};

subtest 'list' => sub {
  reset_timeout;
  skip_all 'requires client and server on localhost' if $ENV{AEF_REMOTE};

  our $config;
  $config->{dir} = tempdir( CLEANUP => 1 );

  foreach my $name (qw( foo bar baz ))
  {
    my $fn = File::Spec->catfile($config->{dir}, "$name.txt");
    open my $fh, '>', $fn;
    close $fh;
  }

  my $dir2 = File::Spec->catdir($config->{dir}, "dir2");
  mkdir $dir2;

  foreach my $name (qw( dr.pepper coke pepsi ))
  {
    my $fn = File::Spec->catfile($config->{dir}, 'dir2', "$name.txt");
    open my $fh, '>', $fn;
    close $fh;
  }

  foreach my $passive (0,1)
  {

    my $client = AnyEvent::FTP::Client->new( passive => $passive );

    prep_client( $client );

    $client->connect($config->{host}, $config->{port})->recv;
    $client->login($config->{user}, $config->{pass})->recv;
    $client->type('I')->recv;
    $client->cwd($config->{dir})->recv;

    subtest 'listing with directory' => sub {
      my $list = eval { $client->list->recv };
      diag $@ if $@;
      is $list, array { etc() };
      $list //= [];
      # wu-ftpd
      shift @$list if $list->[0] =~ / \d+$/i;
      # Net::FTPServer
      shift @$list if $list->[0] =~ /\s\.$/;
      shift @$list if $list->[0] =~ /\s\.\.$/;
      is scalar(@$list), 4, 'list length 4';
      is scalar(grep /foo.txt$/, @$list), 1, 'has foo.txt';
      is scalar(grep /bar.txt$/, @$list), 1, 'has bar.txt';
      is scalar(grep /baz.txt$/, @$list), 1, 'has baz.txt';



( run in 1.924 second using v1.01-cache-2.11-cpan-5a3173703d6 )