summary refs log tree commit diff
path: root/t/spore/01_new_from_string.t
blob: 6a21994c591e7424dbc3a0e0d2e18fc278fbded1 (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
use strict;
use warnings;
use Test::More;
use Test::Exception;

plan tests => 27;

use JSON;
use IO::All;
use Net::HTTP::Spore;

my $api_spec = 't/specs/api.json';
my $api2_spec = 't/specs/api2.json';

my %args = ( base_url => 'http://localhost/', );

my $github_spec =
  "http://github.com/franckcuny/spore/raw/master/services/github.json";

my $api_ok = {
    base_url => "http://services.org/restapi",
    methods  => { get_info => { method => 'GET', path => '/show' } },
};

my $second_api = {
    base_url => "http://services.org/restapi",
    methods  => { list_users => { method => 'GET', path => '/users' } },
};

my $api_without_path = {
    base_url => "http://services.org/restapi",
    methods  => { get_info => { method => 'GET' } },
};

my $api_without_method = {
    base_url => "http://services.org/restapi",
    methods  => { get_info => { method => 'PET', path => '/show' } },
};

dies_ok { Net::HTTP::Spore->new_from_spec };
like $@, qr/specification file is missing/;

dies_ok { Net::HTTP::Spore->new_from_spec( "/foo/bar/baz", ) };
like $@, qr/does not exists/;

dies_ok { Net::HTTP::Spore->new_from_spec( $api_spec ) };
like $@, qr/base_url is missing/;

ok my $client = Net::HTTP::Spore->new_from_spec( $api_spec, %args );
ok $client =
  Net::HTTP::Spore->new_from_string( JSON::encode_json($api_ok), %args );
ok $client->meta->_find_spore_method_by_name(sub{/^get_info$/});

SKIP: {
    skip "require RUN_HTTP_TEST", 2 unless $ENV{RUN_HTTP_TEST};
    ok $client = Net::HTTP::Spore->new_from_spec( $github_spec, %args );
    ok $client->meta->_find_spore_method_by_name(sub{/^user_search$/});
}

dies_ok {
    Net::HTTP::Spore->new_from_string( JSON::encode_json($api_without_path) );
};
like $@, qr/Attribute \(path\) is required/;

dies_ok {
    Net::HTTP::Spore->new_from_string(JSON::encode_json($api_without_method));
};
like $@, qr/Attribute \(method\) does not pass the type constraint/;

ok $client = Net::HTTP::Spore->new_from_string(JSON::encode_json($api_ok));
ok $client->meta->_find_spore_method_by_name(sub{/^get_info$/});

dies_ok {
    Net::HTTP::Spore->new_from_strings('/a/b/c', '/a/b/c');
};

for ( {}, { base_url => 'http://localhost/api' } ) {
    ok $client = Net::HTTP::Spore->new_from_strings( JSON::encode_json($api_ok),
        JSON::encode_json($second_api), $_ );
    ok $client->meta->_find_spore_method_by_name( sub { /^get_info$/ } );
    ok $client->meta->_find_spore_method_by_name( sub { /^list_users$/ } );
}

dies_ok {
    $client = Net::HTTP::Spore->new_from_specs($api_spec, $api2_spec);
};
like $@, qr/base_url is missing/;

ok $client =
  Net::HTTP::Spore->new_from_specs( $api_spec, $api2_spec,
    { base_url => 'http://localhost' } );