summary refs log tree commit diff
path: root/lib/Net/HTTP/Spore/Middleware/Mock.pm
blob: 12568bf2466fef9e0a74d7b5e8360a2eb6b4d3c7 (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
package Net::HTTP::Spore::Middleware::Mock;

# ABSTRACT: Simple Mocker for Spore middlewares

use Moose;
extends 'Net::HTTP::Spore::Middleware';

has tests => ( isa => 'HashRef', is => 'ro', required => 1 );

sub call {
    my ( $self, $req ) = @_;

    foreach my $r ( keys %{ $self->tests } ) {
        next unless $r eq $req->path;
        my $res = $self->tests->{$r}->($req);
        return $res if defined $res;
    }
}

1;

=head1 SYNOPSIS

    my $mock_server = {
        '/path/i/want/to/match' => sub {
            my $req = shift;
            ...
            $req->new_response(200, ['Content-Type' => 'text/plain'], 'ok');
        }
    };

    my $client = Net::HTTP::Spore->new_from_spec('spec.json');
    $client->enable('Mock', tests => $mock_server);
    my $res = $client->my_rest_method();
    is $res->status, 200;
    is $res->body, 'ok';

=head1 DESCRIPTION