aboutsummaryrefslogtreecommitdiffstats
path: root/t/Mock/Bing.pm
blob: cc4dab845557bff451aacd401c3351c10bad6473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package t::Mock::Bing;

use JSON::MaybeXS;
use Web::Simple;
use LWP::Protocol::PSGI;

has json => (
    is => 'lazy',
    default => sub {
        JSON->new->pretty->allow_blessed->convert_blessed;
    },
);

sub dispatch_request {
    my $self = shift;

    sub (GET + /REST/v1/Locations + ?*) {
        my ($self, $query) = @_;
        my $results = [ {
            point => { coordinates => [ 51, -1 ] },
            name => 'Constitution Hill, London, SW1A',
            confidence => 'High',
            address => {
                addressLine => 'Constitution Hill',
                locality => 'London',
                countryRegion => 'United Kingdom',
            }
        } ];
        if ($query->{q} =~ /two results/) {
            push @$results, {
                point => { coordinates => [ 51, -1 ] },
                name => 'Constitution Hill again, United Kingdom',
                confidence => 'High',
                address => {
                    addressLine => 'Constitution Hill again',
                    locality => 'London',
                    countryRegion => 'United Kingdom',
                }
            };
        }
        if ($query->{q} =~ /low/) {
            push @$results, {
                point => { coordinates => [ 52, -2 ] },
                name => 'Constitution Hill elsewhere, United Kingdom',
                confidence => 'Low',
                address => {
                    addressLine => 'Constitution Hill elsewhere',
                    locality => 'London',
                    countryRegion => 'United Kingdom',
                }
            };
        }
        if ($query->{q} =~ /onlylow/) {
            @$results = map { $_->{confidence} = 'Low'; $_ } @$results;
        }
        my $data = {
            statusCode => 200,
            resourceSets => [ { resources => $results } ],
        };
        my $json = $self->json->encode($data);
        return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ];
    },

    sub (GET + /REST/v1/Locations/* + ?*) {
        my ($self, $location, $query) = @_;
        my $data = {
            resourceSets => [ {
                resources => [ {
                    name => 'Constitution Hill, London, SW1A',
                    address => {
                        addressLine => 'Constitution Hill',
                        locality => 'London',
                    }
                } ],
            } ],
        };
        my $json = $self->json->encode($data);
        return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ];
    },
}

LWP::Protocol::PSGI->register(t::Mock::Bing->to_psgi_app, host => 'dev.virtualearth.net');

__PACKAGE__->run_if_script;