Skip to content

Commit 70a8d8f

Browse files
Copilotdavorg
andcommitted
Improve args validation and add test for RSS version compatibility
Co-authored-by: davorg <24642+davorg@users.noreply.github.com>
1 parent 76ec3fe commit 70a8d8f

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

lib/XML/Feed.pm

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,28 @@ sub new {
2626

2727
# Handle optional args hash
2828
my %args;
29-
if (@_ && ref($_[-1]) eq 'HASH' && exists $_[-1]->{useragent}) {
30-
%args = %{pop @_};
31-
# Validate useragent parameter
32-
if (exists $args{useragent}) {
33-
unless (blessed($args{useragent}) && $args{useragent}->isa('LWP::UserAgent')) {
34-
Carp::croak("useragent must be an LWP::UserAgent object");
29+
if (@_ && ref($_[-1]) eq 'HASH') {
30+
my $potential_args = $_[-1];
31+
# Check if this looks like our args hash (has known keys)
32+
my @known_keys = qw(useragent);
33+
my @hash_keys = keys %$potential_args;
34+
35+
# If any key matches our known args, treat it as args hash
36+
if (grep { my $k = $_; grep { $k eq $_ } @known_keys } @hash_keys) {
37+
%args = %{pop @_};
38+
39+
# Validate useragent parameter if provided
40+
if (exists $args{useragent}) {
41+
unless (blessed($args{useragent}) && $args{useragent}->isa('LWP::UserAgent')) {
42+
Carp::croak("useragent must be an LWP::UserAgent object");
43+
}
44+
}
45+
46+
# Only known keys are allowed
47+
my @invalid_keys = grep { my $k = $_; !grep { $k eq $_ } @known_keys } keys %args;
48+
if (@invalid_keys) {
49+
Carp::croak("Invalid argument(s): " . join(', ', @invalid_keys));
3550
}
36-
}
37-
# Only useragent is allowed for now
38-
my @invalid_keys = grep { $_ ne 'useragent' } keys %args;
39-
if (@invalid_keys) {
40-
Carp::croak("Invalid argument(s): " . join(', ', @invalid_keys));
4151
}
4252
}
4353

t/31-useragent.t

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,18 @@ use LWP::UserAgent;
9393
is($feed->useragent->agent, 'RSSTestAgent/1.0', 'UserAgent preserved in RSS feed');
9494
}
9595

96-
# Test 10: LWP::UserAgent subclass should be accepted
96+
# Test 10: RSS feed with both version and useragent
97+
{
98+
my $ua = LWP::UserAgent->new;
99+
$ua->agent('RSSTestAgent/2.0');
100+
101+
my $feed = XML::Feed->new('RSS', version => '0.91', { useragent => $ua });
102+
isa_ok($feed, 'XML::Feed::Format::RSS', 'RSS feed created with version and useragent');
103+
like($feed->format, qr/0\.91/, 'RSS version is correct');
104+
is($feed->useragent->agent, 'RSSTestAgent/2.0', 'UserAgent preserved');
105+
}
106+
107+
# Test 11: LWP::UserAgent subclass should be accepted
97108
SKIP: {
98109
skip "Creating subclass inline for testing", 2;
99110

0 commit comments

Comments
 (0)