5
5
namespace Php \PieUnitTest \Downloading ;
6
6
7
7
use Composer \Util \AuthHelper ;
8
+ use Generator ;
8
9
use GuzzleHttp \Psr7 \Request ;
9
10
use Php \Pie \DependencyResolver \Package ;
10
11
use Php \Pie \Downloading \AddAuthenticationHeader ;
11
12
use Php \Pie \ExtensionName ;
12
13
use Php \Pie \ExtensionType ;
13
14
use PHPUnit \Framework \Attributes \CoversClass ;
15
+ use PHPUnit \Framework \Attributes \DataProvider ;
14
16
use PHPUnit \Framework \TestCase ;
15
17
use RuntimeException ;
16
18
@@ -33,24 +35,48 @@ public function testAuthorizationHeaderIsAdded(): void
33
35
34
36
$ requestWithAuthHeader = (new AddAuthenticationHeader ())->withAuthHeaderFromComposer (
35
37
$ request ,
36
- new Package (
37
- ExtensionType::PhpModule,
38
- ExtensionName::normaliseFromString ('foo ' ),
39
- 'foo/bar ' ,
40
- '1.2.3 ' ,
41
- $ downloadUrl ,
42
- [],
43
- null ,
44
- '1.2.3.0 ' ,
45
- true ,
46
- true ,
47
- ),
38
+ $ this ->createDummyPackage ($ downloadUrl ),
48
39
$ authHelper ,
49
40
);
50
41
51
42
self ::assertSame ('whatever ABC123 ' , $ requestWithAuthHeader ->getHeaderLine ('Authorization ' ));
52
43
}
53
44
45
+ #[DataProvider('provideInvalidAuthorizationHeaders ' )]
46
+ public function testEmptyValueInAuthorizationHeaderThrowsException (string $ rawHeader ): void
47
+ {
48
+ $ downloadUrl = 'http://test-uri/ ' . uniqid ('path ' , true );
49
+
50
+ $ request = new Request ('GET ' , $ downloadUrl );
51
+
52
+ $ authHelper = $ this ->createMock (AuthHelper::class);
53
+ $ authHelper ->expects (self ::once ())
54
+ ->method ('addAuthenticationHeader ' )
55
+ ->with ([], 'github.com ' , $ downloadUrl )
56
+ ->willReturn ([$ rawHeader ]);
57
+
58
+ $ addAuthenticationHeader = new AddAuthenticationHeader ();
59
+
60
+ $ this ->expectException (RuntimeException::class);
61
+ $ this ->expectExceptionMessage ('Authorization header is malformed, it should contain a non-empty key and a non-empty value. ' );
62
+ $ addAuthenticationHeader ->withAuthHeaderFromComposer ($ request , $ this ->createDummyPackage ($ downloadUrl ), $ authHelper );
63
+ }
64
+
65
+ /**
66
+ * @return Generator<string[]>
67
+ *
68
+ * @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
69
+ */
70
+ public static function provideInvalidAuthorizationHeaders (): Generator
71
+ {
72
+ yield ['Authorization: ' ];
73
+ yield [': Bearer ' ];
74
+ yield [' : Bearer ' ];
75
+ yield ['Authorization: ' ];
76
+ yield [': ' ];
77
+ yield ['Authorization MyToken ' ];
78
+ }
79
+
54
80
public function testExceptionIsThrownWhenPackageDoesNotHaveDownloadUrl (): void
55
81
{
56
82
$ downloadUrl = 'http://test-uri/ ' . uniqid ('path ' , true );
@@ -60,21 +86,26 @@ public function testExceptionIsThrownWhenPackageDoesNotHaveDownloadUrl(): void
60
86
$ authHelper = $ this ->createMock (AuthHelper::class);
61
87
62
88
$ addAuthenticationHeader = new AddAuthenticationHeader ();
63
- $ package = new Package (
89
+ $ package = $ this ->createDummyPackage ();
90
+
91
+ $ this ->expectException (RuntimeException::class);
92
+ $ this ->expectExceptionMessage ('The package foo/bar does not have a download URL ' );
93
+ $ addAuthenticationHeader ->withAuthHeaderFromComposer ($ request , $ package , $ authHelper );
94
+ }
95
+
96
+ private function createDummyPackage (string |null $ downloadUrl = null ): Package
97
+ {
98
+ return new Package (
64
99
ExtensionType::PhpModule,
65
100
ExtensionName::normaliseFromString ('foo ' ),
66
101
'foo/bar ' ,
67
102
'1.2.3 ' ,
68
- null ,
103
+ $ downloadUrl ,
69
104
[],
70
105
null ,
71
106
'1.2.3.0 ' ,
72
107
true ,
73
108
true ,
74
109
);
75
-
76
- $ this ->expectException (RuntimeException::class);
77
- $ this ->expectExceptionMessage ('The package foo/bar does not have a download URL ' );
78
- $ addAuthenticationHeader ->withAuthHeaderFromComposer ($ request , $ package , $ authHelper );
79
110
}
80
111
}
0 commit comments