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