1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
use strict;
use warnings;
use utf8;
use Test::More;
use Test::Requires 'HTTP::CookieJar', 'Plack::Request', 'Plack::Loader', 'Plack::Builder', 'Plack::Response';
use Test::TCP;
use Furl;
subtest 'Simple case', sub {
test_tcp(
client => sub {
my $port = shift;
my $furl = Furl->new(
cookie_jar => HTTP::CookieJar->new()
);
my $url = "http://127.0.0.1:$port";
subtest 'first time access', sub {
my $res = $furl->get("${url}/");
note "Then, response should be 200 OK";
is $res->status, 200;
note "And, content should be 'OK 1'";
is $res->content, 'OK 1';
};
subtest 'Second time access', sub {
my $res = $furl->get("${url}/");
note "Then, response should be 200 OK";
is $res->status, 200;
note "And, content should be 'OK 2'";
is $res->content, 'OK 2';
};
},
server => \&session_server,
);
};
subtest '->request(host => ...) style simple interface', sub {
test_tcp(
client => sub {
my $port = shift;
my $furl = Furl->new(
cookie_jar => HTTP::CookieJar->new()
);
subtest 'first time access', sub {
my $res = $furl->request(
method => 'GET',
scheme => 'http',
host => '127.0.0.1',
port => $port,
);
note "Then, response should be 200 OK";
is $res->status, 200;
note "And, content should be 'OK 1'";
is $res->content, 'OK 1';
};
subtest 'Second time access', sub {
my $res = $furl->request(
method => 'GET',
scheme => 'http',
host => '127.0.0.1',
port => $port,
);
note "Then, response should be 200 OK";
is $res->status, 200;
note "And, content should be 'OK 2'";
is $res->content, 'OK 2';
};
},
server => \&session_server,
);
};
subtest 'With redirect', sub {
test_tcp(
client => sub {
my $port = shift;
my $furl = Furl->new(
cookie_jar => HTTP::CookieJar->new()
);
my $url = "http://127.0.0.1:$port";
subtest 'first time access', sub {
my $res = $furl->get("${url}/login");
note "Then, response should be 200 OK";
is $res->status, 200;
note "And, content should be 'ok'";
is $res->content, 'ok';
};
subtest 'Second time access', sub {
my $res = $furl->get("${url}/user_name");
note "Then, response should be 200 OK";
is $res->status, 200;
note "And, content should be 'Nick'";
is $res->content, 'Nick';
};
},
server => sub {
my $port = shift;
my %SESSION_STORE;
Plack::Loader->auto( port => $port )->run(builder {
enable 'ContentLength';
enable 'StackTrace';
sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $path_info = $env->{PATH_INFO};
$path_info =~ s!^//!/!;
if ($path_info eq '/login') {
my $res = Plack::Response->new(
302, ['Location' => $req->uri_for('/login_done')], []
);
$res->cookies->{'user_name'} = 'Nick';
return $res->finalize;
} elsif ($path_info eq '/login_done') {
my $res = Plack::Response->new(
200, [], ['ok']
);
return $res->finalize;
} elsif ($path_info eq '/user_name') {
my $res = Plack::Response->new(
200, [], [$req->cookies->{'user_name'}]
);
return $res->finalize;
} else {
my $res = Plack::Response->new(
404, [], ['not found:' . $env->{PATH_INFO}]
);
return $res->finalize;
}
};
});
}
);
};
done_testing;
sub session_server {
my $port = shift;
my %SESSION_STORE;
Plack::Loader->auto( port => $port )->run(builder {
enable 'ContentLength';
sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $session_key = $req->cookies->{session_key} || rand();
my $cnt = ++$SESSION_STORE{$session_key};
note "CNT: $cnt";
my $res = Plack::Response->new(
200, [], ["OK ${cnt}"]
);
$res->cookies->{'session_key'} = $session_key;
return $res->finalize;
};
});
}
sub Plack::Request::uri_for {
my($self, $path, $args) = @_;
my $uri = $self->base;
$uri->path($uri->path . $path);
$uri->query_form(@$args) if $args;
$uri;
}
|