From 587020119bdb634353b10aaf6077ab3e07eb5c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Str=C3=A1dal?= Date: Wed, 17 Feb 2021 13:14:45 +0100 Subject: [PATCH] #62 fix warnings during HTTP::Config->match --- lib/HTTP/Config.pm | 8 ++++++-- t/http-config.t | 12 +++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/HTTP/Config.pm b/lib/HTTP/Config.pm index e2bd649d..0101b528 100644 --- a/lib/HTTP/Config.pm +++ b/lib/HTTP/Config.pm @@ -155,8 +155,12 @@ my %MATCH = ( m_header__ => sub { my($v, $k, $uri, $request, $response) = @_; return unless $request; - return 1 if $request->header($k) eq $v; - return 1 if $response && $response->header($k) eq $v; + my $req_header = $request->header($k); + return 1 if defined($req_header) && $req_header eq $v; + if ($response) { + my $res_header = $response->header($k); + return 1 if defined($res_header) && $res_header eq $v; + } return 0; }, m_response_attr__ => sub { diff --git a/t/http-config.t b/t/http-config.t index c0b8825d..14ca6dc5 100644 --- a/t/http-config.t +++ b/t/http-config.t @@ -2,7 +2,8 @@ use strict; use warnings; use Test::More; -plan tests => 28; + +plan tests => 30; use HTTP::Config; @@ -103,4 +104,13 @@ is(j($conf->matching_items($response)), "HTML|html|text|any"); ok(($conf->empty), 'found and removed the config entry'); is(scalar(@warnings), 0, 'no warnings') or diag('got warnings: ', explain(\@warnings)); + + @warnings = (); + $conf->add_item("bond", m_header__user_agent => 'james/0.0.7'); + my $request2 = HTTP::Request->new(HEAD => "http://www.example.com/foo/bar"); + is(j($conf->matching_items($request2)), ''); + + is(scalar(@warnings), 0, 'no warnings') + or diag('got warnings: ', explain(\@warnings)); + }