tests : redirect & content_like

24 views
Skip to first unread message

amka...@gmail.com

unread,
Jul 7, 2017, 12:57:44 PM7/7/17
to Mojolicious
Hi,

Can please someone say me how to check a content_like, in a mojolicious test script, if a redirects occurs ?

$t = $t->post_ok('/engre' => {Accept => '*/*'} => form => {a => '0.25', z => '10'})->status_is(302)->content_like(qr/principale/);

does not work.

Thanks & best regards,

Amka

Stefan Adams

unread,
Jul 7, 2017, 1:28:08 PM7/7/17
to mojolicious
See this example in Test::Mojo.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscribe@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

amka...@gmail.com

unread,
Jul 8, 2017, 7:35:00 AM7/8/17
to Mojolicious
Thanks,

and I don't understand. I can know what is the new url, ok, but how please to check the content ?

Joel Berger

unread,
Jul 8, 2017, 9:09:38 AM7/8/17
to Mojolicious
Hello Amka,

Your question is slightly ambiguous, are you trying to test the content/body of the 302 message or the content/body of the message it redirects to? Most people don't include a body on their 302 so I'm going to assume you mean the latter. Assuming that's true, this is how you might test both.

```perl
# set the internal useragent to follow redirects
# in this case I'm only going to allow one since I only expect one
$t->ua->max_redirects(1);

# then test the request and the final response
$t->post_ok('/engre' => {Accept => '*/*'} => form => {a => '0.25', z => '10'})->status_is(200)->content_like(qr/principale/);

# then come back and test that there was a redirect and it was the appropriate status
my $redirect = $t->tx->previous;
ok $redirect, 'got a previous transaction';
is $redirect->res->code, 302, 'got the expected redirect code';

# if you don't care about the exact value you could have tested
ok $redirect->res->is_redirect, 'previous transaction was a redirect';
# however the fact that there was a previous transaction kinda implies that same thing.
```

Of course you could wrap some of that into its own test method, but that's only useful if you are going to do it a lot.

Cheers,
Joel

amka...@gmail.com

unread,
Jul 9, 2017, 11:18:57 AM7/9/17
to Mojolicious
Hi,

It seems that only

$t->ua->max_redirects(1);

was missing to me.

Thanks a lot, have a nice day  :)
Reply all
Reply to author
Forward
0 new messages