Subject: | API Conflict between LWP::UserAgent::credentials() and WWW::Mechanize::credentials |
WWW::Mechanize 1.20+ implements a horrible monkeypatch globally
overwriting LWP::UserAgent::credentials with a curried version that can
only ever return one set of login/password. This breaks programs that
use the LWP authentication mechanism.
There is no way of retrieving the credentials once they have been set
from WWW::Mechanize.
Please don't step on other people's namespaces.
The attached test file tests that the ->credentials() method is
independent per WWW::Mechanize instance and doesn't affect
LWP::UserAgent methods.
-max
Subject: | www-mechanize-monkeypatch-bug.t |
#!/usr/bin/perl -w
use strict;
use Test::More tests => 4;
use LWP::UserAgent;
use WWW::Mechanize;
=head1 NAME
www-mechanize-monkeypatch-bug.t - Prevent ugly LWP::UserAgent monkeypatching
=head1 REASON
The monkeypatch introduced since at least
WWW::Mechanize 1.34 only ever allows
one instance of every LWP::UserAgent descendant to have credentials.
This test checks that this buggy behaviour is gone.
Also, the WWW::Mechanize API breaks LWP::UserAgent which always expects
->credentials($hostloc,$realm) to return a username and password.
The solution would be to rename ->credentials() to something different
that doesn't clash with LWP::UserAgent.
=cut
my $ua = LWP::UserAgent->new();
my $m_1 = WWW::Mechanize->new();
my $m_2 = WWW::Mechanize->new();
my $m_3 = WWW::Mechanize->new();
$m_1->credentials('m_1','m_1');
$m_2->credentials('m_2','m_2');
my @ua = $ua->credentials;
isn't "@ua", "m_2 m_2", 'LWP::UserAgent instance retains its old credentials';
is_deeply [$m_1->get_basic_credentials], ['m_1','m_1'], 'First instance retains its credentials';
is_deeply [$m_2->get_basic_credentials], ['m_2','m_2'], 'Second instance retains its credentials';
is_deeply [$m_3->get_basic_credentials], [undef,undef], 'Untouched instance retains its credentials';