|
9 | 9 | use Illuminate\Routing\Route; |
10 | 10 | use Illuminate\Routing\RouteCollection; |
11 | 11 | use Illuminate\Routing\UrlGenerator; |
| 12 | +use Illuminate\Support\Carbon; |
12 | 13 | use InvalidArgumentException; |
13 | 14 | use PHPUnit\Framework\Attributes\DataProvider; |
14 | 15 | use PHPUnit\Framework\TestCase; |
@@ -938,6 +939,99 @@ public function testSignedUrlWithKeyResolver() |
938 | 939 | $this->assertTrue($url3->hasValidSignature($secondRequest)); |
939 | 940 | } |
940 | 941 |
|
| 942 | + public function testSignedUrlWithRawUrl() |
| 943 | + { |
| 944 | + $url = new UrlGenerator( |
| 945 | + new RouteCollection, |
| 946 | + Request::create('http://www.foo.com/') |
| 947 | + ); |
| 948 | + $url->setKeyResolver(function () { |
| 949 | + return 'secret'; |
| 950 | + }); |
| 951 | + |
| 952 | + $signedUrl = $url->signedUrl('http://www.foo.com/test'); |
| 953 | + $this->assertStringContainsString('signature=', $signedUrl); |
| 954 | + |
| 955 | + $request = Request::create($signedUrl); |
| 956 | + $this->assertTrue($url->hasValidSignature($request)); |
| 957 | + |
| 958 | + $request = Request::create($signedUrl.'&tampered=true'); |
| 959 | + $this->assertFalse($url->hasValidSignature($request)); |
| 960 | + } |
| 961 | + |
| 962 | + public function testSignedUrlWithExistingQueryParameters() |
| 963 | + { |
| 964 | + $url = new UrlGenerator( |
| 965 | + new RouteCollection, |
| 966 | + Request::create('http://www.foo.com/') |
| 967 | + ); |
| 968 | + $url->setKeyResolver(function () { |
| 969 | + return 'secret'; |
| 970 | + }); |
| 971 | + |
| 972 | + $signedUrl = $url->signedUrl('http://www.foo.com/test?param1=value1¶m2=value2'); |
| 973 | + $this->assertStringContainsString('signature=', $signedUrl); |
| 974 | + $this->assertStringContainsString('param1=value1', $signedUrl); |
| 975 | + $this->assertStringContainsString('param2=value2', $signedUrl); |
| 976 | + |
| 977 | + $request = Request::create($signedUrl); |
| 978 | + $this->assertTrue($url->hasValidSignature($request)); |
| 979 | + } |
| 980 | + |
| 981 | + public function testSignedUrlWithExpiration() |
| 982 | + { |
| 983 | + $url = new UrlGenerator( |
| 984 | + new RouteCollection, |
| 985 | + Request::create('http://www.foo.com/') |
| 986 | + ); |
| 987 | + $url->setKeyResolver(function () { |
| 988 | + return 'secret'; |
| 989 | + }); |
| 990 | + |
| 991 | + Carbon::setTestNow(Carbon::create(2025, 1, 1)); |
| 992 | + $signedUrl = $url->signedUrl('http://www.foo.com/test', now()->addMinutes(5)); |
| 993 | + $this->assertStringContainsString('signature=', $signedUrl); |
| 994 | + $this->assertStringContainsString('expires=', $signedUrl); |
| 995 | + |
| 996 | + $request = Request::create($signedUrl); |
| 997 | + $this->assertTrue($url->hasValidSignature($request)); |
| 998 | + |
| 999 | + Carbon::setTestNow(Carbon::create(2025, 1, 1)->addMinutes(10)); |
| 1000 | + $this->assertFalse($url->hasValidSignature($request)); |
| 1001 | + } |
| 1002 | + |
| 1003 | + public function testSignedUrlWithReservedSignatureParameter() |
| 1004 | + { |
| 1005 | + $url = new UrlGenerator( |
| 1006 | + new RouteCollection, |
| 1007 | + Request::create('http://www.foo.com/') |
| 1008 | + ); |
| 1009 | + $url->setKeyResolver(function () { |
| 1010 | + return 'secret'; |
| 1011 | + }); |
| 1012 | + |
| 1013 | + $this->expectException(InvalidArgumentException::class); |
| 1014 | + $this->expectExceptionMessage('reserved'); |
| 1015 | + |
| 1016 | + $url->signedUrl('http://www.foo.com/test?signature=tampered'); |
| 1017 | + } |
| 1018 | + |
| 1019 | + public function testSignedUrlWithReservedExpiresParameter() |
| 1020 | + { |
| 1021 | + $url = new UrlGenerator( |
| 1022 | + new RouteCollection, |
| 1023 | + Request::create('http://www.foo.com/') |
| 1024 | + ); |
| 1025 | + $url->setKeyResolver(function () { |
| 1026 | + return 'secret'; |
| 1027 | + }); |
| 1028 | + |
| 1029 | + $this->expectException(InvalidArgumentException::class); |
| 1030 | + $this->expectExceptionMessage('reserved'); |
| 1031 | + |
| 1032 | + $url->signedUrl('http://www.foo.com/test?expires=123456', now()->addMinutes(5)); |
| 1033 | + } |
| 1034 | + |
941 | 1035 | public function testMissingNamedRouteResolution() |
942 | 1036 | { |
943 | 1037 | $url = new UrlGenerator( |
|
0 commit comments