1515namespace Gitlab \Tests ;
1616
1717use Gitlab \Client ;
18+ use GuzzleHttp \Psr7 \Response ;
1819use Http \Client \Common \HttpMethodsClient ;
20+ use PHPUnit \Framework \Attributes \DataProvider ;
1921use PHPUnit \Framework \TestCase ;
22+ use Psr \Http \Client \ClientInterface ;
23+ use Psr \Http \Message \RequestInterface ;
2024
2125class ClientTest extends TestCase
2226{
@@ -27,4 +31,127 @@ public function testCreateClient(): void
2731 $ this ->assertInstanceOf (Client::class, $ client );
2832 $ this ->assertInstanceOf (HttpMethodsClient::class, $ client ->getHttpClient ());
2933 }
34+
35+ public static function authenticationProvider (): array
36+ {
37+ return [
38+ Client::AUTH_HTTP_TOKEN => [Client::AUTH_HTTP_TOKEN , 'PRIVATE-TOKEN ' , 'my-token ' ],
39+ Client::AUTH_HTTP_JOB_TOKEN => [Client::AUTH_HTTP_JOB_TOKEN , 'JOB-TOKEN ' , 'my-token ' ],
40+ Client::AUTH_OAUTH_TOKEN => [Client::AUTH_OAUTH_TOKEN , 'Authorization ' , 'Bearer my-token ' ],
41+ ];
42+ }
43+
44+ #[DataProvider('authenticationProvider ' )]
45+ public function testAuthenticationIsSentToTheBaseUrl (string $ method , string $ header , string $ value ): void
46+ {
47+ $ requests = [];
48+
49+ $ client = Client ::createWithHttpClient ($ this ->createHttpClient ($ requests ));
50+ $ client ->authenticate ('my-token ' , $ method , 'my-user ' );
51+ $ client ->getHttpClient ()->get ('/api/v4/user ' );
52+ $ client ->getHttpClient ()->get ('https://GitLab.com:443/api/v4/projects?page=2 ' );
53+
54+ $ this ->assertCount (2 , $ requests );
55+
56+ foreach ($ requests as $ request ) {
57+ $ this ->assertSame ($ value , $ request ->getHeaderLine ($ header ));
58+ $ this ->assertSame ('my-user ' , $ request ->getHeaderLine ('SUDO ' ));
59+ }
60+ }
61+
62+ public function testAuthenticationIsNotSentToOtherOrigins (): void
63+ {
64+ $ requests = [];
65+
66+ $ client = Client ::createWithHttpClient ($ this ->createHttpClient ($ requests ));
67+ $ client ->authenticate ('my-token ' , Client::AUTH_HTTP_TOKEN , 'my-user ' );
68+ $ client ->getHttpClient ()->get ('https://about.gitlab.com/api/v4/user ' );
69+ $ client ->getHttpClient ()->get ('http://gitlab.com/api/v4/user ' );
70+ $ client ->getHttpClient ()->get ('https://gitlab.com:8443/api/v4/user ' );
71+ $ client ->getHttpClient ()->get ('https://gitlab.com.example.com/api/v4/user ' );
72+
73+ $ this ->assertCount (4 , $ requests );
74+
75+ foreach ($ requests as $ request ) {
76+ $ this ->assertFalse ($ request ->hasHeader ('PRIVATE-TOKEN ' ), (string ) $ request ->getUri ());
77+ $ this ->assertFalse ($ request ->hasHeader ('SUDO ' ), (string ) $ request ->getUri ());
78+ }
79+ }
80+
81+ #[DataProvider('authenticationProvider ' )]
82+ public function testAuthenticationIsRemovedFromRequestsToOtherOrigins (string $ method , string $ header , string $ value ): void
83+ {
84+ $ requests = [];
85+
86+ $ client = Client ::createWithHttpClient ($ this ->createHttpClient ($ requests ));
87+ $ client ->authenticate ('my-token ' , $ method , 'my-user ' );
88+ $ client ->getHttpClient ()->get ('https://gitlab.example.com/api/v4/user ' , [$ header => $ value , 'SUDO ' => 'my-user ' ]);
89+ $ client ->getHttpClient ()->get ('https://gitlab.example.com/api/v4/user ' , [$ header => 'other-token ' , 'SUDO ' => 'my-user ' ]);
90+
91+ $ this ->assertFalse ($ requests [0 ]->hasHeader ($ header ));
92+ $ this ->assertFalse ($ requests [0 ]->hasHeader ('SUDO ' ));
93+ $ this ->assertSame ('other-token ' , $ requests [1 ]->getHeaderLine ($ header ));
94+ $ this ->assertSame ('my-user ' , $ requests [1 ]->getHeaderLine ('SUDO ' ));
95+ }
96+
97+ public function testAuthenticationFollowsTheBaseUrl (): void
98+ {
99+ $ requests = [];
100+
101+ $ client = Client ::createWithHttpClient ($ this ->createHttpClient ($ requests ));
102+ $ client ->authenticate ('my-token ' , Client::AUTH_HTTP_TOKEN );
103+ $ client ->setUrl ('https://gitlab.example.com ' );
104+ $ client ->getHttpClient ()->get ('/api/v4/user ' );
105+ $ client ->getHttpClient ()->get ('https://gitlab.com/api/v4/user ' );
106+
107+ $ this ->assertSame ('gitlab.example.com ' , $ requests [0 ]->getUri ()->getHost ());
108+ $ this ->assertSame ('my-token ' , $ requests [0 ]->getHeaderLine ('PRIVATE-TOKEN ' ));
109+ $ this ->assertFalse ($ requests [1 ]->hasHeader ('PRIVATE-TOKEN ' ));
110+ }
111+
112+ public function testAuthenticationIsSentToTheBaseUrlOnRedirect (): void
113+ {
114+ $ requests = [];
115+
116+ $ client = Client ::createWithHttpClient ($ this ->createHttpClient ($ requests , new Response (302 , ['Location ' => 'https://gitlab.com/api/v4/projects/2 ' ])));
117+ $ client ->authenticate ('my-token ' , Client::AUTH_HTTP_TOKEN );
118+ $ client ->getHttpClient ()->get ('/api/v4/projects/1 ' );
119+
120+ $ this ->assertSame ('/api/v4/projects/2 ' , $ requests [1 ]->getUri ()->getPath ());
121+ $ this ->assertSame ('my-token ' , $ requests [1 ]->getHeaderLine ('PRIVATE-TOKEN ' ));
122+ }
123+
124+ #[DataProvider('authenticationProvider ' )]
125+ public function testAuthenticationIsNotSentToOtherOriginsOnRedirect (string $ method , string $ header , string $ value ): void
126+ {
127+ $ requests = [];
128+
129+ $ client = Client ::createWithHttpClient ($ this ->createHttpClient ($ requests , new Response (302 , ['Location ' => 'https://storage.example.com/artifacts.zip?Signature=abc ' ])));
130+ $ client ->authenticate ('my-token ' , $ method , 'my-user ' );
131+ $ client ->jobs ()->artifacts (1 , 2 );
132+
133+ $ this ->assertSame ($ value , $ requests [0 ]->getHeaderLine ($ header ));
134+ $ this ->assertSame ('my-user ' , $ requests [0 ]->getHeaderLine ('SUDO ' ));
135+ $ this ->assertSame ('storage.example.com ' , $ requests [1 ]->getUri ()->getHost ());
136+ $ this ->assertFalse ($ requests [1 ]->hasHeader ($ header ));
137+ $ this ->assertFalse ($ requests [1 ]->hasHeader ('SUDO ' ));
138+ }
139+
140+ /**
141+ * @param RequestInterface[] $requests
142+ */
143+ private function createHttpClient (array &$ requests , Response ...$ responses ): ClientInterface
144+ {
145+ $ httpClient = $ this ->createMock (ClientInterface::class);
146+ $ httpClient
147+ ->expects ($ this ->any ())
148+ ->method ('sendRequest ' )
149+ ->willReturnCallback (static function (RequestInterface $ request ) use (&$ requests , &$ responses ): Response {
150+ $ requests [] = $ request ;
151+
152+ return \array_shift ($ responses ) ?? new Response ();
153+ });
154+
155+ return $ httpClient ;
156+ }
30157}
0 commit comments