File Coverage

blib/lib/GitLab/API/v4/Mock/RESTClient.pm
Criterion Covered Total %
statement 35 36 97.2
branch 9 12 75.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 53 58 91.3


line stmt bran cond sub pod time code
1             package GitLab::API::v4::Mock::RESTClient;
2             our $VERSION = '0.25';
3              
4             =encoding utf8
5              
6             =head1 NAME
7              
8             GitLab::API::v4::Mock::RESTClient - Mocked REST client that doesn't actually make HTTP requests.
9              
10             =head1 DESCRIPTION
11              
12             This module is a subclass of L. It
13             modifies it to divert HTTP requests to
14             L rather than making live requests.
15              
16             This module is used by L.
17              
18             =cut
19              
20 1     1   434 use GitLab::API::v4::Mock::Engine;
  1         3  
  1         32  
21 1     1   656 use JSON;
  1         10251  
  1         5  
22 1     1   712 use URI;
  1         4258  
  1         30  
23              
24 1     1   6 use Moo;
  1         3  
  1         4  
25 1     1   326 use strictures 2;
  1         9  
  1         38  
26 1     1   197 use namespace::clean;
  1         3  
  1         6  
27              
28             extends 'GitLab::API::v4::RESTClient';
29              
30             my @ENDPOINTS;
31              
32             sub has_endpoint {
33 5     5 0 12 my ($method, $path_re, $sub) = @_;
34              
35 5         12 push @ENDPOINTS, [
36             $method, $path_re, $sub,
37             ];
38              
39 5         9 return;
40             }
41              
42             sub _http_tiny_request {
43 11     11   26 my ($self, $req_method, $req) = @_;
44              
45 11 50       26 die "req_method may only be 'request' at this time"
46             if $req_method ne 'request';
47              
48 11         23 my ($http_method, $url, $options) = @$req;
49              
50 11         31 my $path = URI->new( $url )->path();
51 11         837 $path =~ s{^.*api/v4/}{};
52              
53 11         32 foreach my $endpoint (@ENDPOINTS) {
54 24         48 my ($endpoint_method, $path_re, $sub) = @$endpoint;
55              
56 24 100       54 next if $endpoint_method ne $http_method;
57 11 50       57 next if $path !~ $path_re;
58              
59 11         44 my @captures = ($path =~ $path_re);
60              
61 11         38 my ($status, $content) = $sub->(
62             $self,
63             [$http_method, $url, $options],
64             @captures,
65             );
66              
67 11 100       99 $content = encode_json( $content ) if ref $content;
68              
69             return {
70 11 50       92 status => $status,
    100          
71             success => ($status =~ m{^2\d\d$}) ? 1 : 0,
72             defined( $content ) ? (content=>$content) : (),
73             };
74             }
75              
76 0         0 die "No endpoint matched the $http_method '$path' endpoint";
77             }
78              
79             =head1 ATTRIBUTES
80              
81             =head2 engine
82              
83             The L used behind the hood.
84              
85             =cut
86              
87             has engine => (
88             is => 'lazy',
89             init_arg => undef,
90             );
91             sub _build_engine {
92 2     2   45 return GitLab::API::v4::Mock::Engine->new();
93             }
94              
95             =head1 USER ENDPOINTS
96              
97             =head2 GET users
98              
99             Handles L.
100              
101             =cut
102              
103             has_endpoint GET => qr{^users$}, sub{
104             my ($self) = @_;
105             return 200, $self->engine->users();
106             };
107              
108             =head2 GET user/:id
109              
110             Handles L.
111              
112             =cut
113              
114             has_endpoint GET => qr{^users/(\d+)$}, sub{
115             my ($self, $req, $id) = @_;
116              
117             my $user = $self->engine->user( $id );
118             return 404 if !$user;
119              
120             return 200, $user;
121             };
122              
123             =head2 POST users
124              
125             Handles L.
126              
127             =cut
128              
129             has_endpoint POST => qr{^users$}, sub{
130             my ($self, $req) = @_;
131              
132             my $user = decode_json( $req->[2]->{content} );
133             $self->engine->create_user( $user );
134              
135             return 204;
136             };
137              
138             =head2 PUT user/:id
139              
140             Handles L.
141              
142             =cut
143              
144             has_endpoint PUT => qr{^users/(\d+)$}, sub{
145             my ($self, $req, $id) = @_;
146              
147             my $data = decode_json( $req->[2]->{content} );
148              
149             my $user = $self->engine->update_user( $id, $data );
150             return 404 if !$user;
151              
152             return 204;
153             };
154              
155             =head2 DELETE user/:id
156              
157             Handles L.
158              
159             =cut
160              
161             has_endpoint DELETE => qr{^users/(\d+)$}, sub{
162             my ($self, $req, $id) = @_;
163              
164             my $user = $self->engine->delete_user( $id );
165             return 404 if !$user;
166              
167             return 204;
168             };
169              
170             1;
171             __END__