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
|
use strict;
use warnings;
use Test::More;
package test::auth;
use MooseX::Net::API;
net_api_declare fake_auth => (
api_base_url => 'http://localhost',
format => 'json',
authentication => 1,
authentication_method => 'my_auth',
);
net_api_method user => (
method => 'GET',
path => '/user/',
);
sub my_auth {
my ($self, $request, $ua, $h) = @_;
$request->header('Authentication' => 1);
}
package test::auth::simple;
use MooseX::Net::API;
net_api_declare fake_auth => (
api_base_url => 'http://localhost',
format => 'json',
authentication => 1,
);
net_api_method user => (
method => 'GET',
path => '/user/',
);
package main;
ok my $api = test::auth->new, 'object api created';
$api->api_useragent->add_handler(
request_send => sub {
my $request = shift;
is $request->header('Authentication'), 1, 'authentication header is set';
my $res = HTTP::Response->new(200);
$res->content('[{"name":"eris"}]');
$res;
}
);
ok $api->user, 'method user success';
ok $api =
test::auth::simple->new(api_username => 'foo', api_password => 'bar'),
'object api simple created';
$api->api_useragent->add_handler(
request_send => sub {
my $request = shift;
ok $request->header('authorization'), 'authentication header is set';
my $res = HTTP::Response->new(200);
$res->content('[{"name":"eris"}]');
$res;
}
);
ok $api->user, 'method user success';
done_testing;
|