summaryrefslogtreecommitdiffstats
path: root/vespaclient/src/perl/test/Yahoo/Vespa/Mocks/HttpClientMock.pm
blob: d2eb5c221805bc67f4011bdf382d069027e44cdb (plain) (blame)
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
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#
# Switched the backend implementation of the Vespa::Http library, such that
# requests are sent here rather than onto the network. Register handlers here
# to respond to requests.
#
# Handlers are called in sequence until one of them returns a defined result.
# If none do, return a generic failure.
#

package Yahoo::Vespa::Mocks::HttpClientMock;

use strict;
use warnings;
use Yahoo::Vespa::ConsoleOutput;
use Yahoo::Vespa::Http;

BEGIN { # - Define default exports for module
    use base 'Exporter';
    our @EXPORT = qw(
        registerHttpClientHandler
    );
}

my @HANDLERS;

&initialize();

return 1;

#################### Default exported functions #############################

sub registerHttpClientHandler { # (Handler)
    push @HANDLERS, $_[0];
}

##################### Internal utility functions ##########################

sub initialize { # ()
    Yahoo::Vespa::Http::setHttpExecutor(\&clientMock);
}
sub clientMock { # (HttpRequest to forward) -> Response
    foreach my $handler (@HANDLERS) {
        my %result = &$handler(@_);
        if (exists $result{'code'}) {
            return %result;
        }
    }
    return (
        'code' => 500,
        'status' => 'No client handler for given request',
        'content' => '',
        'all' => ''
    );
}