« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Config.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Mixi.pm »
| 1 | package App::Test; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Exporter'; | ||
| 4 | use Data::Dumper; | ||
| 5 | use Encode qw/decode_utf8/; | ||
| 6 | use JSON; | ||
| 7 | use HTTP::Cookies; | ||
| 8 | use HTTP::Request::Common qw/GET HEAD PUT POST DELETE/; | ||
| 9 | use LWP::UserAgent; | ||
| 10 | use Plack::Test; | ||
| 11 | use Test::More; | ||
| 12 | use WWW::Mechanize; | ||
| 13 | use App; | ||
| 14 | |||
| 15 | our @EXPORT = qw/is_success is_failure/; | ||
| 16 | |||
| 17 | sub import { | ||
| 18 | my ($pkg) = @_; | ||
| 19 | Dwarf::Pragma->import(); | ||
| 20 | Test::More->import(); | ||
| 21 | Test::More->export_to_level(1); | ||
| 22 | Plack::Test->import(); | ||
| 23 | Plack::Test->export_to_level(1); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub is_success { | ||
| 27 | my ($res, $path) = @_; | ||
| 28 | my $desc = $res->status_line; | ||
| 29 | $desc .= ', redirected to ' . ($res->header("Location") || "") if ($res->is_redirect); | ||
| 30 | if (!$res->is_redirect) { | ||
| 31 | warn Dumper $res unless $res->is_success; | ||
| 32 | ok $res->is_success, "$path: $desc"; | ||
| 33 | } else { | ||
| 34 | ok $res->is_redirect, "$path: $desc"; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub decode_response { | ||
| 39 | my ($res) = @_; | ||
| 40 | if (($res->code == 200 || $res->code == 400 || $res->code == 500) and $res->header('Content-Type') =~ /json/) { | ||
| 41 | return $res unless $res->content; | ||
| 42 | my $content = eval { decode_json($res->content) }; | ||
| 43 | if ($@) { | ||
| 44 | warn $content; | ||
| 45 | } | ||
| 46 | $res->content($content); | ||
| 47 | return $res; | ||
| 48 | } elsif ($res->code == 302) { | ||
| 49 | return $res; | ||
| 50 | } | ||
| 51 | return $res; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub is_failure { | ||
| 55 | my ($res, $path) = @_; | ||
| 56 | my $desc = $res->status_line; | ||
| 57 | ok !$res->is_success, "$path: $desc"; | ||
| 58 | } | ||
| 59 | |||
| 60 | use Dwarf::Accessor qw/context context_stack cookie_jar ua mech will_decode_content/; | ||
| 61 | |||
| 62 | sub _build_context { App->new } | ||
| 63 | sub _build_context_stack { [] } | ||
| 64 | |||
| 65 | sub _build_cookie_jar { HTTP::Cookies->new } | ||
| 66 | |||
| 67 | sub _build_ua { | ||
| 68 | my ($self) = @_; | ||
| 69 | my $ua = LWP::UserAgent->new; | ||
| 70 | $ua->cookie_jar($self->cookie_jar); | ||
| 71 | return $ua; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _build_mech { | ||
| 75 | my ($self) = @_; | ||
| 76 | my $mech = WWW::Mechanize->new(autocheck => 0); | ||
| 77 | $mech->cookie_jar($self->cookie_jar); | ||
| 78 | return $mech; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub c { $_[0]->context } | ||
| 82 | |||
| 83 | sub new { | ||
| 84 | my $invocant = shift; | ||
| 85 | my $class = ref($invocant) || $invocant; | ||
| 86 | my $self = bless { @_ }, $class; | ||
| 87 | $self->{will_decode_content} //= 0; | ||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub req_ok { | ||
| 92 | my ($self, $method, $url, @params) = @_; | ||
| 93 | my ($req, $res) = $self->req($method, $url, @params); | ||
| 94 | is_success($res, $req->uri); | ||
| 95 | return wantarray ? ($req, $res) : $res; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub req_not_ok { | ||
| 99 | my ($self, $method, $url, @params) = @_; | ||
| 100 | my ($req, $res) = $self->req($method, $url, @params); | ||
| 101 | is_failure($res, $req->uri); | ||
| 102 | return wantarray ? ($req, $res) : $res; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub req { | ||
| 106 | my ($self, $method, $url, @args) = @_; | ||
| 107 | |||
| 108 | my $req = $self->_req($method => $url, @args); | ||
| 109 | |||
| 110 | my $res; | ||
| 111 | test_psgi app => $self->app, client => sub { | ||
| 112 | my ($cb) = @_; | ||
| 113 | |||
| 114 | $self->cookie_jar->add_cookie_header($req); | ||
| 115 | $res = $cb->($req); | ||
| 116 | $self->cookie_jar->extract_cookies($res); | ||
| 117 | }; | ||
| 118 | |||
| 119 | $res = decode_response($res) if $self->will_decode_content; | ||
| 120 | |||
| 121 | return wantarray ? ($req, $res) : $res; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _req { | ||
| 125 | my ($self, $method, $url, @args) = @_; | ||
| 126 | |||
| 127 | if ($self->c->conf('ssl')) { | ||
| 128 | $url =~ s/^http/https/; | ||
| 129 | } | ||
| 130 | |||
| 131 | my $uri = URI->new($url); | ||
| 132 | $uri->query_form($args[0]) if $method =~ /^(get|delete)$/i; | ||
| 133 | |||
| 134 | my @a = ($uri->as_string); | ||
| 135 | push @a, @args if $method !~ /^(get|delete)$/i; | ||
| 136 | $method = uc $method; | ||
| 137 | |||
| 138 | my $req; | ||
| 139 | |||
| 140 | # HTTP::Request::Common が PATCH をサポートしてないのでワークアラウンド | ||
| 141 | if ($method eq 'PATCH') { | ||
| 142 | $method = 'POST'; | ||
| 143 | $method = \&$method; | ||
| 144 | $req = $method->(@a); | ||
| 145 | $req->method('PATCH'); | ||
| 146 | } else { | ||
| 147 | $method = \&$method; | ||
| 148 | $req = $method->(@a); | ||
| 149 | } | ||
| 150 | |||
| 151 | return $req; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub ua_ok { | ||
| 155 | my ($self, $method, $url, @args) = @_; | ||
| 156 | my $req = $self->_req($method, $url, @args); | ||
| 157 | my $res = $self->ua->request($req); | ||
| 158 | $res = decode_response($res) if $self->will_decode_content; | ||
| 159 | is_success($res, $req->uri); | ||
| 160 | return wantarray ? ($req, $res) : $res; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub ua_not_ok { | ||
| 164 | my ($self, $method, $url, @args) = @_; | ||
| 165 | my $req = $self->_req($method, $url, @args); | ||
| 166 | my $res = $self->ua->request($req); | ||
| 167 | $res = decode_response($res) if $self->will_decode_content; | ||
| 168 | is_failure($res, $req->uri); | ||
| 169 | return wantarray ? ($req, $res) : $res; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub mech_fetch { | ||
| 173 | my ($self, $url, $args) = @_; | ||
| 174 | my $uri = URI->new($url); | ||
| 175 | $uri->query_form($args) if ref $args eq 'HASH'; | ||
| 176 | |||
| 177 | my $mech = $self->mech; | ||
| 178 | $mech->get($uri); | ||
| 179 | $mech->update_html(decode_utf8($mech->content)); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub mech_ok { | ||
| 183 | my ($self, $url, $args) = @_; | ||
| 184 | $self->mech_fetch($url, $args); | ||
| 185 | ok($self->mech->success); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub mech_not_ok { | ||
| 189 | my ($self, $url, $args) = @_; | ||
| 190 | $self->mech_fetch($url, $args); | ||
| 191 | ok(!$self->mech->success); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub mech_submit { | ||
| 195 | my ($self, $url, $args, $opt) = @_; | ||
| 196 | my $form_number = $opt->{form_number} // 1; | ||
| 197 | $self->mech_fetch($url); | ||
| 198 | my $mech = $self->mech; | ||
| 199 | $mech->form_number($form_number); | ||
| 200 | $mech->set_fields(%$args); | ||
| 201 | $mech->click; | ||
| 202 | $mech->update_html(decode_utf8($mech->content)); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub mech_submit_ok { | ||
| 206 | my ($self, $url, $args, $opt) = @_; | ||
| 207 | $self->mech_submit($url, $args, $opt); | ||
| 208 | my $mech = $self->mech; | ||
| 209 | ok($mech->success); | ||
| 210 | } | ||
| 211 | |||
| 212 | sub mech_submit_not_ok { | ||
| 213 | my ($self, $url, $args, $opt) = @_; | ||
| 214 | $self->mech_submit($url, $args, $opt); | ||
| 215 | my $mech = $self->mech; | ||
| 216 | ok(!$mech->success); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub app { | ||
| 220 | my $self = shift; | ||
| 221 | return sub { | ||
| 222 | my $env = shift; | ||
| 223 | $env->{HTTPS} = 'on'; | ||
| 224 | $env->{HTTP_HOST} = 'localhost'; | ||
| 225 | #$env->{HTTP_AUTHORIZATION} = "Bearer " . $self->c->conf('/oauth/bearer_token'); | ||
| 226 | push @{ $self->{context_stack} }, $self->context if $self->context; # 古いコンテキストを保存して GC されないようにする | ||
| 227 | $self->{context} = App->new(env => $env); | ||
| 228 | $self->c->runtime(0) if $self->c->can('runtime'); # runtime プラグインの結果を出力しない | ||
| 229 | $self->c->to_psgi; | ||
| 230 | }; | ||
| 231 | } | ||
| 232 | |||
| 233 | 1; |