diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-04-12 13:59:19 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-04-12 16:21:37 +0100 |
commit | c1cfc23c8296e6b94037733371d7aff12585b2bd (patch) | |
tree | 424cc3ab22e670d5bf7a728908b45e212e48e358 | |
parent | 9fde903b6f9d2fa70e666cd5d9f864cb275bffe2 (diff) |
Provide API key to MapIt proxy if present.
-rwxr-xr-x | perllib/FixMyStreet/App/Controller/FakeMapit.pm | 16 | ||||
-rw-r--r-- | t/app/controller/fakemapit.t | 13 |
2 files changed, 27 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/App/Controller/FakeMapit.pm b/perllib/FixMyStreet/App/Controller/FakeMapit.pm index 0ec13ebfa..51975254a 100755 --- a/perllib/FixMyStreet/App/Controller/FakeMapit.pm +++ b/perllib/FixMyStreet/App/Controller/FakeMapit.pm @@ -2,7 +2,7 @@ package FixMyStreet::App::Controller::FakeMapit; use Moose; use namespace::autoclean; use JSON::MaybeXS; -use LWP::Simple; +use LWP::UserAgent; BEGIN { extends 'Catalyst::Controller'; } @@ -22,13 +22,25 @@ world is one area, with ID 161 and name "Everywhere". my $area = { "name" => "Everywhere", "type" => "ZZZ", "id" => 161 }; +has user_agent => ( + is => 'ro', + lazy => 1, + default => sub { + my $ua = LWP::UserAgent->new; + my $api_key = FixMyStreet->config('MAPIT_API_KEY'); + $ua->agent("FakeMapit proxy"); + $ua->default_header( 'X-Api-Key' => $api_key ) if $api_key; + return $ua; + } +); + # The user should have the web server proxying this, # but for development we can also do it on the server. sub proxy : Path('/mapit') { my ($self, $c) = @_; (my $path = $c->req->uri->path_query) =~ s{^/mapit/}{}; my $url = FixMyStreet->config('MAPIT_URL') . $path; - my $kml = LWP::Simple::get($url); + my $kml = $self->user_agent->get($url)->content; $c->response->body($kml); } diff --git a/t/app/controller/fakemapit.t b/t/app/controller/fakemapit.t new file mode 100644 index 000000000..c89aac600 --- /dev/null +++ b/t/app/controller/fakemapit.t @@ -0,0 +1,13 @@ +use JSON::MaybeXS; +use FixMyStreet::TestMech; + +my $mech = FixMyStreet::TestMech->new; + +FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', +}, sub { + $mech->get_ok('/mapit/areas/Birmingham'); + is_deeply decode_json($mech->content), {2514 => {parent_area => undef, id => 2514, name => "Birmingham City Council", type => "MTD"}}; +}; + +done_testing; |