Skip to content

Commit

Permalink
added request_ok method to Test::Mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 13, 2012
1 parent c45aaf4 commit 18d117f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

3.66 2012-12-13
- Added request_ok method to Test::Mojo.
- Improved documentation.
- Improved tests.
- Fixed multipart boundary detection bug in Mojo::Content.
Expand Down
27 changes: 20 additions & 7 deletions lib/Test/Mojo.pm
Expand Up @@ -211,6 +211,12 @@ sub post_json_ok {

sub put_ok { shift->_request_ok(put => @_) }

sub request_ok {
my $self = shift;
my $tx = $self->tx($self->ua->start(shift))->tx;
return $self->_test('ok', $tx->is_finished, shift || 'perform request');
}

sub reset_session {
my $self = shift;
if (my $jar = $self->ua->cookie_jar) { $jar->empty }
Expand Down Expand Up @@ -371,13 +377,6 @@ Current transaction, usually a L<Mojo::Transaction::HTTP> object.
is $t->tx->res->json->{foo}, 'bar', 'right value';
ok $t->tx->res->is_multipart, 'multipart content';
# Test custom transaction
my $tx = $t->ua->build_json_tx('/user/99' => {name => 'sri'});
$tx->req->method('PUT');
$t->tx($t->ua->start($tx))
->status_is(200)
->json_is('/message' => 'User has been replaced.');
=head2 C<ua>
my $ua = $t->ua;
Expand Down Expand Up @@ -691,6 +690,20 @@ the exact same arguments as L<Mojo::UserAgent/"post_json">.
Perform a C<PUT> request and check for transport errors, takes the exact same
arguments as L<Mojo::UserAgent/"put">.
=head2 C<request_ok>
$t = $t->request_ok(Mojo::Transaction::HTTP->new);
$t = $t->request_ok(Mojo::Transaction::HTTP->new, 'request successful');
Perform request and check for transport errors.
# Test custom transaction
my $tx = $t->ua->build_json_tx('/user/99' => {name => 'sri'});
$tx->req->method('PUT');
$t->request_ok($tx)
->status_is(200)
->json_is('/message' => 'User has been replaced.');
=head2 C<reset_session>
$t = $t->reset_session;
Expand Down
19 changes: 14 additions & 5 deletions t/mojolicious/restful_lite_app.t
Expand Up @@ -11,7 +11,7 @@ use Mojolicious::Lite;
use Test::Mojo;

# POST /json/echo
post '/json/echo' => sub {
any [qw(POST PUT)] => '/json/echo' => sub {
my $self = shift;
$self->respond_to(json => {json => $self->req->json});
};
Expand Down Expand Up @@ -52,10 +52,19 @@ $t->post_json_ok(
->status_is(200)->content_type_is('application/json')
->json_content_is({hello => 'world'});

# POST /json/echo (array with json format)
$t->post_json_ok('/json/echo' => [1, 2, 3] => {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is([1, 2, 3]);
# PUT /json/echo (hash with json format)
my $tx = $t->ua->build_json_tx(
'/json/echo' => {hello => 'world'} => {Accept => 'application/json'});
$tx->req->method('PUT');
$t->request_ok($tx)->status_is(200)->content_type_is('application/json')
->json_content_is({hello => 'world'});

# PUT /json/echo (array with json format)
$tx = $t->ua->build_json_tx(
'/json/echo' => [1, 2, 3] => {Accept => 'application/json'});
$tx->req->method('PUT');
$t->request_ok($tx, 'request succesful')->status_is(200)
->content_type_is('application/json')->json_content_is([1, 2, 3]);

# GET /rest
$t->get_ok('/rest')->status_is(200)
Expand Down

0 comments on commit 18d117f

Please sign in to comment.