diff options
-rw-r--r-- | perllib/FixMyStreet/App.pm | 26 | ||||
-rw-r--r-- | t/app/uri_for.t | 89 |
2 files changed, 114 insertions, 1 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index 4e992d4f9..a5ba6bc5d 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -182,7 +182,7 @@ sub setup_request { FixMyStreet::Map::set_map_class( $c->request->param('map') ); - return $cobrand; + return $c; } =head2 setup_dev_overrides @@ -300,6 +300,30 @@ sub send_email { return $email; } +=head2 uri_for + + $uri = $c->uri_for( ... ); + +Like C<uri_for> except that it passes the uri to the cobrand to be altered if +needed. + +=cut + +sub uri_for { + my $c = shift; + my @args = @_; + + my $uri = $c->next::method(@args); + + # Currently the cobrand expect and return the url as a string. + my $cobranded_uri = $c->cobrand->url( $uri->as_string ); + + # check to see if the returned string looks like a url (cities does not) + return $cobranded_uri =~ m{^https?://} + ? URI->new($cobranded_uri) + : $cobranded_uri; +} + =head2 uri_for_email $uri = $c->uri_for_email( ... ); diff --git a/t/app/uri_for.t b/t/app/uri_for.t new file mode 100644 index 000000000..9a93fedea --- /dev/null +++ b/t/app/uri_for.t @@ -0,0 +1,89 @@ +use strict; +use warnings; + +use Test::More; + +# structure of these tests borrowed from '/t/aggregate/unit_core_uri_for.t' + +use strict; +use warnings; +use URI; + +use_ok('FixMyStreet::App'); + +my $fms_c = FixMyStreet::App->new( + { + request => Catalyst::Request->new( + { + base => URI->new('http://www.fixmystreet.com/'), + uri => URI->new('http://www.fixmystreet.com/test_namespace') + } + ), + namespace => 'test_namespace', + } +); + +my $fgm_c = FixMyStreet::App->new( + { + request => Catalyst::Request->new( + { + base => URI->new('http://www.fiksgatami.no/'), + uri => URI->new('http://www.fiksgatami.no/test_namespace') + } + ), + namespace => 'test_namespace', + } +); + +is( + $fms_c->uri_for('/bar/baz') . "", + 'http://www.fixmystreet.com/bar/baz', + 'URI for absolute path' +); + +is( + $fms_c->uri_for('') . "", + 'http://www.fixmystreet.com/test_namespace', + 'URI for namespace' +); + +is( + $fms_c->uri_for( '/bar/baz', 'boing', { foo => 'bar', } ) . "", + 'http://www.fixmystreet.com/bar/baz/boing?foo=bar', + 'URI with query' +); + +# fiksgatami +is( + $fgm_c->uri_for( '/foo', { lat => 1.23, } ) . "", + 'http://www.fiksgatami.no/foo?lat=1.23;zoom=2', + 'FiksGataMi url with lat not zoom' +); + +## Should really test the cities but we'd need to fake up too much of the +# request. Following code starts to do this but is not complete. Instead better +# to test that the cities produces the correct urls by looking at the html +# produced. +# +# # cities +# my $cities_c = FixMyStreet::App->new( +# { +# request => Catalyst::Request->new( +# { +# base => URI->new('http://cities.fixmystreet.com/'), +# uri => URI->new( +# 'http://cities.fixmystreet.com/test_namespace?city=cardiff' +# ), +# params => { city => 'cardiff', }, +# } +# ), +# namespace => 'test_namespace', +# } +# )->setup_request; +# is( +# $cities_c->uri_for( '/foo', { bar => 'baz' } ) . "", +# '{microapp-href:http://cities.fixmystreet.com/foo?bar=baz&city=cardiff}', +# 'Cities url' +# ); + +done_testing(); |