]> Dogcows Code - chaz/p5-HTTP-AnyUA/commitdiff
add RequestHeaders middleware
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sat, 2 Mar 2019 20:58:28 +0000 (13:58 -0700)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sat, 2 Mar 2019 20:58:28 +0000 (13:58 -0700)
lib/HTTP/AnyUA/Middleware/RequestHeaders.pm [new file with mode: 0644]
t/40-middleware-request-headers.t [new file with mode: 0644]

diff --git a/lib/HTTP/AnyUA/Middleware/RequestHeaders.pm b/lib/HTTP/AnyUA/Middleware/RequestHeaders.pm
new file mode 100644 (file)
index 0000000..557271a
--- /dev/null
@@ -0,0 +1,76 @@
+package HTTP::AnyUA::Middleware::RequestHeaders;
+# ABSTRACT: Middleware to add custom request headers
+
+=head1 SYNOPSIS
+
+    $any_ua->apply_middleware('RequestHeaders',
+        headers  => {connection => 'close'},
+        override => 0,
+    );
+
+=head1 DESCRIPTION
+
+This middleware adds custom headers to each request.
+
+=head1 SEE ALSO
+
+=for :list
+* L<HTTP::AnyUA::Middleware>
+
+=cut
+
+use warnings;
+use strict;
+
+our $VERSION = '9999.999'; # VERSION
+
+use parent 'HTTP::AnyUA::Middleware';
+
+use HTTP::AnyUA::Util;
+
+
+sub init {
+    my $self = shift;
+    my %args = @_;
+    $self->{override} = !!$args{override};
+    $self->{headers}  = HTTP::AnyUA::Util::normalize_headers($args{headers});
+}
+
+sub request {
+    my $self = shift;
+    my ($method, $url, $args) = @_;
+
+    if ($self->override) {
+        $args->{headers} = {
+            %{HTTP::AnyUA::Util::normalize_headers($args->{headers})},
+            %{$self->headers},
+        };
+    }
+    else {
+        $args->{headers} = {
+            %{$self->headers},
+            %{HTTP::AnyUA::Util::normalize_headers($args->{headers})},
+        };
+    }
+
+    return $self->backend->request($method, $url, $args);
+}
+
+=attr headers
+
+Get the custom headers.
+
+=cut
+
+sub headers { shift->{headers} }
+
+=attr override
+
+When true, custom headers overwrite headers in the request. The default is false (the request
+headers take precedence when defined).
+
+=cut
+
+sub override { shift->{override} }
+
+1;
diff --git a/t/40-middleware-request-headers.t b/t/40-middleware-request-headers.t
new file mode 100644 (file)
index 0000000..c15736a
--- /dev/null
@@ -0,0 +1,48 @@
+#!perl
+
+use warnings;
+use strict;
+
+use lib 't/lib';
+
+use HTTP::AnyUA;
+use Test::More tests => 5;
+
+HTTP::AnyUA->register_backend(Mock => '+MockBackend');
+
+my $any_ua  = HTTP::AnyUA->new(ua => 'Mock');
+my $backend = $any_ua->backend;
+
+$any_ua->apply_middleware('RequestHeaders',
+    headers => {
+        whatever => 'meh',
+        Foo      => 'bar',
+    },
+);
+
+my $url     = 'http://acme.tld/';
+
+$any_ua->get($url, {headers => {baz => 'qux'}});
+my $headers = ($backend->requests)[-1][2]{headers};
+is $headers->{whatever}, 'meh', 'custom header with GET';
+is $headers->{foo}, 'bar', 'normalized header';
+is $headers->{baz}, 'qux', 'request header left intact';
+
+$any_ua->get($url, {headers => {baz => 'qux', foo => 'moof'}});
+$headers = ($backend->requests)[-1][2]{headers};
+is $headers->{foo}, 'moof', 'request header takes precedence';
+
+$any_ua  = HTTP::AnyUA->new(ua => 'Mock');
+$backend = $any_ua->backend;
+
+$any_ua->apply_middleware('RequestHeaders',
+    headers => {
+        Foo => 'bar',
+    },
+    override => 1,
+);
+
+$any_ua->get($url, {headers => {foo => 'moof'}});
+$headers = ($backend->requests)[-1][2]{headers};
+is $headers->{foo}, 'bar', 'custom header takes precedence if override on';
+
This page took 0.033027 seconds and 4 git commands to generate.