Skip to content

Commit

Permalink
Implement delete_ok method.
Browse files Browse the repository at this point in the history
  • Loading branch information
moznion committed Aug 29, 2013
1 parent ecbf994 commit c46f996
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
48 changes: 47 additions & 1 deletion Mechanize.pm
Expand Up @@ -268,6 +268,52 @@ sub put_ok {
return $ok;
}

=head2 $mech->delete_ok( $url, [ \%LWP_options ,] $desc )
A wrapper around WWW::Mechanize's delete(), with similar options, except
the second argument needs to be a hash reference, not a hash. Like
well-behaved C<*_ok()> functions, it returns true if the test passed,
or false if not.
A default description of "DELETE to $url" is used if none if provided.
=cut

sub delete_ok {
my $self = shift;

my ($url,$desc,%opts) = $self->_unpack_args( 'DELETE', @_ );

if ($self->can('delete')) {
$self->delete( $url, %opts );
}
else {
# When version of LWP::UserAgent is older than 6.04.
$self->_delete( $url, %opts );
}
my $ok = $self->success;

$ok = $self->_maybe_lint( $ok, $desc );

return $ok;
}

sub _delete {
require URI;
require HTTP::Request::Common;
my $self = shift;
my $uri = shift;

$uri = $uri->url if ref($uri) eq 'WWW::Mechanize::Link';
$uri = $self->base
? URI->new_abs( $uri, $self->base )
: URI->new($uri);

my @parameters = ( $uri->as_string, @_ );
my @suff = $self->_process_colonic_headers( \@parameters, 1 );
return $self->request( HTTP::Request::Common::DELETE(@parameters), @suff );
}

=head2 $mech->submit_form_ok( \%parms [, $desc] )
Makes a C<submit_form()> call and executes tests on the results.
Expand Down Expand Up @@ -1140,7 +1186,7 @@ sub _check_links_status {
return @failures;
}

# This actually performs the content check of each url.
# This actually performs the content check of each url.
sub _check_links_content {
my $self = shift;
my $urls = shift;
Expand Down
51 changes: 51 additions & 0 deletions t/delete_ok.t
@@ -0,0 +1,51 @@
#!perl -T

use strict;
use warnings;

use Test::More tests => 9;
use Test::Builder::Tester;

use Test::WWW::Mechanize ();

use lib 't';
use TestServer;

my $server = TestServer->new;
my $pid = $server->background;
my $server_root = $server->root;

my $mech = Test::WWW::Mechanize->new( autocheck => 0 );
isa_ok($mech,'Test::WWW::Mechanize');

GOOD_DELETE: {
my $scratch = "$server_root/scratch.html";

$mech->delete_ok($scratch);
ok($mech->success, 'sanity check: we can load scratch.html');

test_out('ok 1 - Try to DELETE scratch.html');
my $ok = $mech->delete_ok($scratch, 'Try to DELETE scratch.html');
test_test('DELETEs existing URI and reports success');
is( ref($ok), '', 'delete_ok() should only return a scalar' );
ok( $ok, 'And the result should be true' );

# default desc
test_out("ok 1 - DELETE $scratch");
$mech->delete_ok($scratch);
test_test('DELETEs existing URI and reports success - default desc');

# For old LWP::UA
undef *Test::WWW::Mechanize::can;
*Test::WWW::Mechanize::can = sub {
return undef;
};
$mech->delete_ok($scratch);
ok($mech->success, 'sanity check: we can load scratch.html by old LWP::UA');
undef *Test::WWW::Mechanize::can;
*Test::WWW::Mechanize::can = *UNIVERSAL::can{CODE};
}

$server->stop;

done_testing();

0 comments on commit c46f996

Please sign in to comment.