--- /dev/null
+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;
--- /dev/null
+#!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';
+