Cookies-Roundtrip
view release on metacpan or search on metacpan
t/350-file-from-httpcookies.t view on Meta::CPAN
#!/usr/bin/env perl
###################################################################
#### NOTE env-var PERL_TEST_TEMPDIR_TINY_NOCLEANUP=1 will stop erasing tmp files
###################################################################
use strict;
use warnings;
our $VERSION = '0.01';
use Test::More;
use Test::More::UTF8;
use Test::Deep '!blessed';
use FindBin;
use Test::TempDir::Tiny;
use HTTP::CookieJar;
use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;
use lib ($FindBin::Bin, 'blib/lib');
use Cookies::Roundtrip qw/:count :as_string :equal :file
httpcookiejar2httpcookies
/;
my $VERBOSITY = 4; # we need verbosity of 10 (max), so this is not used
my $curdir = $FindBin::Bin;
require(File::Spec->catfile($curdir, 'MY', 'CookieMaker.pm'));
#ok(@MY::CookieMaker::HTTPCookieJar_cases, "Library 'CookieMaker.pm' loaed OK/1.") or BAIL_OUT;
ok(@MY::CookieMaker::HTTPCookies_cases, "Library 'CookieMaker.pm' loaed OK/2.") or BAIL_OUT;
# if for debug you change this make sure that it has path in it e.g. ./xyz
my $tmpdir = tempdir(); # will be erased unless a BAIL_OUT or env var set
ok(-d $tmpdir, "tmpdir exists $tmpdir") or BAIL_OUT;
my $skip_discard = 0;
for my $c (@MY::CookieMaker::HTTPCookies_cases) {
diag "\n==================\ntest " . $c->{label} . "\n==================\n";
my $httpcookies = $c->{getcookie}->();
ok(defined $httpcookies, "(label : ".$c->{'label'}.") : got the HTTP::Cookies object by running the test sub.") or BAIL_OUT;
# save to file
my $cooksfile = File::Spec->catfile($tmpdir, 'cookies.txt');
my $ret = cookies2file($httpcookies, $cooksfile, $skip_discard, $VERBOSITY);
ok(defined($ret), 'cookies2file()'." : (label '".$c->{label}."') : called and got good result.") or BAIL_OUT;
ok(-f $cooksfile, 'cookies2file()'." : (label '".$c->{label}."') : output cookies file '$cooksfile' exists on disk.") or BAIL_OUT;
ok(!-z $cooksfile, 'cookies2file()'." : (label '".$c->{label}."') : output cookies file '$cooksfile' has some content.") or BAIL_OUT;
# load from file
my $httpcookies2 = file2httpcookies($cooksfile, undef, $skip_discard, $VERBOSITY);
ok(defined($httpcookies2), 'file2httpcookies()'." : (label '".$c->{label}."') : called and got good result.") or BAIL_OUT;
ok(-f $cooksfile, 'file2httpcookies()'." : (label '".$c->{label}."') : output cookies file '$cooksfile' exists on disk.") or BAIL_OUT;
ok(!-z $cooksfile, 'file2httpcookies()'." : (label '".$c->{label}."') : output cookies file '$cooksfile' has some content.") or BAIL_OUT;
# compare number of cookies
my $c1 = count_cookies($httpcookies, $skip_discard, $VERBOSITY);
ok(defined($c1), 'count_cookies()'." : (label '".$c->{label}."') : called and got good result.") or BAIL_OUT;
my $c2 = count_cookies($httpcookies2, $skip_discard, $VERBOSITY);
ok(defined($c2), 'count_cookies()'." : (label '".$c->{label}."') : called and got good result.") or BAIL_OUT;
is($c1, $c2, "there is the same number of cookies in 1 ($c1) and 2 ($c2).") or BAIL_OUT;
# compare
$ret = cookies_are_equal_httpcookies(
$httpcookies,
$httpcookies2,
$skip_discard,
$VERBOSITY
);
ok(defined($ret), 'cookies_are_equal_httpcookiejar()'." : (label '".$c->{label}."') : called and got good result.") or BAIL_OUT;
is($ret, 1, "(label '".$c->{label}."') : cookies are equal.") or BAIL_OUT("--begin cookies:\n".as_string_cookies($httpcookies)."\n--end cookies\n--begin cookies:\n".as_string_cookies($httpcookies2)."\n--end cookies.\nno, see cookies above.");
# load from file but the format of the produced cookie jar must be HTTP::Cookies
my $httpcookiejar = file2httpcookiejar($cooksfile, undef, $skip_discard, $VERBOSITY);
ok(defined($httpcookiejar), 'file2httpcookiejar()'." : (label '".$c->{label}."') : called and got good result.") or BAIL_OUT;
( run in 1.827 second using v1.01-cache-2.11-cpan-b9db842bd85 )