about summary refs log tree commit diff
path: root/lib/MooseX/Net/API/Test.pm
blob: e991b7f3c0cb59f5fef1e395c6e274c78db36a5b (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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package MooseX::Net::API::Test;

use lib ('t/lib');
use Try::Tiny;

use Test::More;
use Moose;
use Moose::Exporter;
use MooseX::Net::API::Meta::Class;
use MooseX::Net::API::Meta::Method;

Moose::Exporter->setup_import_methods(
    with_caller => [qw/test_api_method test_api_declare run/] );

my $api_to_test;

sub init_meta {
    my ( $me, %options ) = @_;

    my $for = $options{for_class};
    Moose::Util::MetaRole::apply_metaclass_roles(
        for_class       => $for,
        metaclass_roles => ['MooseX::Net::API::Meta::Class'],
    );
}

my $list_content_type = {
    'json' => 'application/json',
    'yaml' => 'text/x-yaml',
    'xml'  => 'text/xml',
};

my $tests_count = 0;

sub test_api_declare {
    my $caller  = shift;
    my $name    = shift;
    my %options = @_;

    unless ( Class::MOP::is_class_loaded($name) ) {
        Class::MOP::load_class($name);
    }

    $api_to_test = $name;

    if ( $options{catalyst} ) {
        my $app = $options{catalyst_app_name};

        Class::MOP::load_class("HTTP::Request");
        Class::MOP::load_class("Catalyst::Test");

        Catalyst::Test->import($app);

        my $res = __PACKAGE__->meta->remove_method('_request');
        MooseX::Net::API->meta->add_method(
            '_request' => sub {
                my ( $class, $format, $options, $uri, $args ) = @_;
                my $method = $options->{method};

                my $res;
                if (   $method =~ /^(?:GET|DELETE)$/
                    || $options->{params_in_url} )
                {
                    $uri->query_form(%$args);
                    my $req = HTTP::Request->new( $method => $uri );
                    $req->header(
                        'Content-Type' => $list_content_type->{$format} );
                    $res = request($req);
                }
                else {
                    my $req = HTTP::Request->new( $method => $uri );
                    $req->header(
                        'Content-Type' => $list_content_type->{$format} );
                    $req->header( 'Content' => Dump $args);
                    $res = request($req);
                }
                return $res;
            }
        );
    }
}

sub test_api_method {
    my $caller  = shift;
    my $name    = shift;
    my %options = @_;

    my $meta   = $api_to_test->meta;
    my $method = $meta->find_method_by_name($name);

    if ( !$method ) {
        die "method $name does not exists\n";
    }

    my $class = Moose::Meta::Class->initialize($caller);
    foreach my $test_name ( keys %{ $options{tests} } ) {
        foreach my $test ( @{ $options{tests}{$test_name} } ) {
            __PACKAGE__->meta->add_method(
                $test_name => sub {
                    my $res    = $method->execute( $api_to_test->new );
                    if (ref $test eq 'HASH') {
                        my $action = $test->{test};
                        my $result = $test->{expected};
                        # XXX sucky sucky sucky
                        if ( $action eq 'is_deeply' ) {
                            is_deeply( $res, $result );
                        }
                    }else{
                        if ($test eq 'ok') {
                            ok $res;
                        }
                    }
                }
            );
            $class->_add_api_test_method($test_name);
        }
    }
}

sub run {
    my $caller = shift;

    my $class = Moose::Meta::Class->initialize($caller);
    my @test_methods = $class->local_api_test_methods();
    foreach my $m (@test_methods) {
        my $method = __PACKAGE__->meta->find_method_by_name($m);
        $method->execute();
    }
}

1;