Skip to content

Commit 894c422

Browse files
authored
[4.6] Slashes in file path used in Src and Downloads API classes are url-encoded (#83)
* Add regression test for URLs created in Src And Downloads API classes * Fix URLs constructed in Src API class * Fix URLs constructed in Downloads API class
1 parent db0fe6b commit 894c422

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

src/Api/Repositories/Workspaces/Downloads.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function upload(FileResource $file)
6565
*/
6666
public function download(string $filename, array $params = [])
6767
{
68-
$uri = $this->buildDownloadsUri($filename);
68+
$uri = $this->buildDownloadsUri(...\explode('/', $filename));
6969

7070
return $this->getAsResponse($uri, $params, ['Accept' => '*/*'])->getBody();
7171
}
@@ -80,7 +80,7 @@ public function download(string $filename, array $params = [])
8080
*/
8181
public function remove(string $filename, array $params = [])
8282
{
83-
$uri = $this->buildDownloadsUri($filename);
83+
$uri = $this->buildDownloadsUri(...\explode('/', $filename));
8484

8585
return $this->delete($uri, $params);
8686
}

src/Api/Repositories/Workspaces/Src.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function createWithFiles(array $files, array $params = [])
9494
*/
9595
public function show(string $commit, string $filepath, array $params = [])
9696
{
97-
$uri = $this->buildSrcUri($commit, $filepath);
97+
$uri = $this->buildSrcUri($commit, ...\explode('/', $filepath));
9898

9999
if (!isset($params['format'])) {
100100
$params['format'] = 'meta';
@@ -114,7 +114,7 @@ public function show(string $commit, string $filepath, array $params = [])
114114
*/
115115
public function download(string $commit, string $filepath, array $params = [])
116116
{
117-
$uri = $this->buildSrcUri($commit, $filepath);
117+
$uri = $this->buildSrcUri($commit, ...\explode('/', $filepath));
118118

119119
return $this->getAsResponse($uri, $params, ['Accept' => '*/*'])->getBody();
120120
}

tests/ApiUrlTest.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bitbucket\Tests;
6+
7+
use Bitbucket\Client;
8+
use Bitbucket\HttpClient\Builder;
9+
use Http\Mock\Client as MockClient;
10+
use PHPUnit\Framework\TestCase;
11+
12+
/**
13+
* Tests API URLs constructed in Src and Downloads API classes.
14+
*/
15+
class ApiUrlTest extends TestCase
16+
{
17+
protected MockClient $httpClient;
18+
protected Client $client;
19+
20+
public function setUp(): void
21+
{
22+
$this->httpClient = new MockClient();
23+
$this->client = new Client(new Builder($this->httpClient));
24+
}
25+
26+
/**
27+
* @covers \Bitbucket\Api\Repositories\Workspaces\Src::show
28+
* @covers \Bitbucket\Api\Repositories\Workspaces\Src::buildSrcUri
29+
*
30+
* @dataProvider dataProvider
31+
*/
32+
public function testWorkspaceSrcShowUri(string $fileName): void
33+
{
34+
$this->client->repositories()
35+
->workspaces('my-workspace')
36+
->src('my-project')
37+
->show('main', $fileName);
38+
39+
$this->assertSame(
40+
"https://api.bitbucket.org/2.0/repositories/my-workspace/my-project/src/main/$fileName?format=meta",
41+
(string) $this->httpClient->getLastRequest()->getUri()
42+
);
43+
}
44+
45+
/**
46+
* @covers \Bitbucket\Api\Repositories\Workspaces\Src::download
47+
* @covers \Bitbucket\Api\Repositories\Workspaces\Src::buildSrcUri
48+
*
49+
* @dataProvider dataProvider
50+
*/
51+
public function testWorkspaceSrcDownloadUri(string $fileName): void
52+
{
53+
$this->client->repositories()
54+
->workspaces('my-workspace')
55+
->src('my-project')
56+
->download('main', $fileName);
57+
58+
$this->assertSame(
59+
"https://api.bitbucket.org/2.0/repositories/my-workspace/my-project/src/main/$fileName",
60+
(string) $this->httpClient->getLastRequest()->getUri()
61+
);
62+
}
63+
64+
/**
65+
* @covers \Bitbucket\Api\Repositories\Workspaces\Downloads::download
66+
* @covers \Bitbucket\Api\Repositories\Workspaces\Downloads::buildDownloadsUri
67+
*
68+
* @dataProvider dataProvider
69+
*/
70+
public function testWorkspaceDownloadUri(string $fileName): void
71+
{
72+
$this->client->repositories()
73+
->workspaces('my-workspace')
74+
->downloads('my-project')
75+
->download($fileName);
76+
77+
$this->assertSame(
78+
"https://api.bitbucket.org/2.0/repositories/my-workspace/my-project/downloads/$fileName",
79+
(string) $this->httpClient->getLastRequest()->getUri()
80+
);
81+
}
82+
83+
/**
84+
* @covers \Bitbucket\Api\Repositories\Workspaces\Downloads::remove
85+
* @covers \Bitbucket\Api\Repositories\Workspaces\Downloads::buildDownloadsUri
86+
*
87+
* @dataProvider dataProvider
88+
*/
89+
public function testWorkspaceRemoveUri(string $fileName): void
90+
{
91+
$this->client->repositories()
92+
->workspaces('my-workspace')
93+
->downloads('my-project')
94+
->remove($fileName);
95+
96+
$this->assertSame(
97+
"https://api.bitbucket.org/2.0/repositories/my-workspace/my-project/downloads/$fileName",
98+
(string) $this->httpClient->getLastRequest()->getUri()
99+
);
100+
}
101+
102+
public static function dataProvider(): array
103+
{
104+
return [
105+
'File in root' => ['README.md'],
106+
'File in subdirectory' => ['docs/contributing/CODE_OF_CONDUCT.md'],
107+
];
108+
}
109+
}

0 commit comments

Comments
 (0)