Akron | 30e7bc7 | 2021-02-16 17:05:41 +0100 | [diff] [blame] | 1 | use Mojolicious::Lite; |
| 2 | use Test::More tests => 17; |
| 3 | use Mojolicious::Controller; |
| 4 | |
| 5 | use_ok('Kalamar::Request'); |
| 6 | |
| 7 | my $req = Kalamar::Request->new( |
| 8 | url => 'http://www.example.com', |
| 9 | method => 'POST', |
| 10 | controller => Mojolicious::Controller->new |
| 11 | )->param('x', 'y'); |
| 12 | |
| 13 | is($req->method, 'POST'); |
| 14 | is($req->url, 'http://www.example.com'); |
| 15 | ok($req->controller); |
| 16 | is_deeply($req->param, ['x','y'], 'Compare'); |
| 17 | |
| 18 | my $err; |
| 19 | $req->start->catch( |
| 20 | sub { |
| 21 | $err = shift |
| 22 | })->finally( |
| 23 | sub { |
| 24 | is($err,'No useragent defined','Error'); |
| 25 | })->wait; |
| 26 | |
| 27 | my $c = $req->controller; |
| 28 | $c->app(app); |
| 29 | ok($req->controller(undef),'Set controller'); |
| 30 | |
| 31 | $req->start->catch( |
| 32 | sub { |
| 33 | $err = shift |
| 34 | })->finally( |
| 35 | sub { |
| 36 | is($err,'No controller defined','Error'); |
| 37 | })->wait; |
| 38 | |
| 39 | ok($req->url(undef),'Set URL'); |
| 40 | |
| 41 | $req->start->catch( |
| 42 | sub { |
| 43 | $err = shift |
| 44 | })->finally( |
| 45 | sub { |
| 46 | is($err,'No URL defined','Error'); |
| 47 | })->wait; |
| 48 | |
| 49 | put '/test' => sub { |
| 50 | shift->render(text => 'Hallo') |
| 51 | }; |
| 52 | |
| 53 | # Set UA |
| 54 | ok($req->ua(Mojo::UserAgent->new)); |
| 55 | $req->ua->server->app->log->level('fatal'); |
| 56 | ok($req->controller($c)); |
| 57 | ok($req->method('put')); |
| 58 | ok($req->url('/test')); |
| 59 | ok($req->param(undef)); |
| 60 | |
| 61 | $err = undef; |
| 62 | $req->start->then( |
| 63 | sub { |
| 64 | my $tx = shift; |
| 65 | is($tx->res->body,'Hallo'); |
| 66 | })->catch( |
| 67 | sub { |
| 68 | $err = shift; |
| 69 | })->finally( |
| 70 | sub { |
| 71 | ok(!$err); |
| 72 | })->wait; |
| 73 | |
| 74 | done_testing; |
| 75 | __END__ |
| 76 | |
| 77 | 1; |