How to check redirect in Mojolicious?

I want to test a page with a form that, when submitted, is redirected to the resulting page for the submitted element.

My Mojolicious controller contains:

sub submit_new { my $self = shift; my $new = $self->db->resultset('Item')->new( { title => $self->param('title'), description => $self->param('description'), } ); $new->insert; # show the newly submitted item my $id = $new->id; $self->redirect_to("/items/$id"); } 

The script test for this controller contains:

 use Test::More; use Test::Mojo; my $t = Test::Mojo->new('MyApp'); my $tx = $t->ua->build_form_tx('/items/new/submit' => $data); $tx->req->method('POST'); $t->tx( $t->ua->start($tx) ) ->status_is(302); 

My problem is that it stops with status 302 . How do I go to redirects to check the resulting page?

+5
source share
2 answers

Set the compliance setting from Mojo :: UserAgent:

 $t->ua->max_redirects(10) 

In addition, you do not need to create a form message manually:

 $t->post_form_ok('/items/new/submit' => $data)->status_is(...); 


Reference:

+8
source

You can also (and probably should) check the contents of the landing page that the successful login is redirected to:

 my $tm = '' ; # the test message for each test my $t = Test::Mojo->new('Qto'); $t->ua->max_redirects(10); my $app = 'foobar'; my $url = '/' . $db_name . '/login' ; $tm = "a successfull result redirects to the home page"; my $tx = $t->ua->post( $url => 'form' => {'email' =>' test.user@gmail.com ', 'pass' => 'secret'}); ok ( $tx->result->dom->all_text =~ "$app home" , $tm ); 

docs on how to get content with Mojo DOM

link to mojo dom selector syntax document

sample test script with test login

0
source

Source: https://habr.com/ru/post/1433103/


All Articles