diff --git a/README.md b/README.md index d0519a8..60a2cc6 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,12 @@ You can create an instance of `Spatie\SslCertificate\SslCertificate` with this n $certificate = SslCertificate::createForHostName('spatie.be'); ``` +You can create an instance of `Spatie\SslCertificate\SslCertificate` passing the port with this named constructor: + +```php +$certificate = SslCertificate::createForHostName('spatie.be:443'); +``` + You can use this fluent style to specify a specific port to connect to. ```php diff --git a/src/Downloader.php b/src/Downloader.php index 3565aa6..d3a6f6a 100644 --- a/src/Downloader.php +++ b/src/Downloader.php @@ -125,7 +125,11 @@ public function getCertificates(string $hostName): array public function forHost(string $hostName): SslCertificate | bool { - $hostName = (new Url($hostName))->getHostName(); + $url = new Url($hostName); + + $this->port = $url->getPort(); + + $hostName = $url->getHostName(); $certificates = $this->getCertificates($hostName); diff --git a/tests/DownloaderTest.php b/tests/DownloaderTest.php index b2f72bb..dba35db 100644 --- a/tests/DownloaderTest.php +++ b/tests/DownloaderTest.php @@ -10,6 +10,19 @@ class DownloaderTest extends TestCase { + protected $domainWithDifferentPort; + protected $ipDomainWithDifferentPort; + protected $differentPort; + + protected function setUp(): void + { + parent::setUp(); + + $this->domainWithDifferentPort = 'ben.hotweb.de'; + $this->ipDomainWithDifferentPort = '178.254.8.25'; + $this->differentPort = 8443; + } + /** @test */ public function it_can_download_a_certificate_from_a_host_name() { @@ -18,6 +31,14 @@ public function it_can_download_a_certificate_from_a_host_name() $this->assertInstanceOf(SslCertificate::class, $sslCertificate); } + /** @test */ + public function it_can_download_a_certificate_from_a_host_name_with_hostport() + { + $sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort); + + $this->assertInstanceOf(SslCertificate::class, $sslCertificate); + } + /** @test */ public function it_can_download_a_certificate_from_a_host_name_with_strange_characters() { @@ -26,6 +47,14 @@ public function it_can_download_a_certificate_from_a_host_name_with_strange_char $this->assertInstanceOf(SslCertificate::class, $sslCertificate); } + /** @test */ + public function it_can_download_a_certificate_from_a_host_name_with_strange_characters_with_hostport() + { + $sslCertificate = Downloader::downloadCertificateFromUrl('https://' . $this->domainWithDifferentPort . ':' . $this->differentPort); + + $this->assertInstanceOf(SslCertificate::class, $sslCertificate); + } + /** @test */ public function it_can_download_a_certificate_for_a_host_name_from_an_ip_address() { @@ -36,6 +65,16 @@ public function it_can_download_a_certificate_for_a_host_name_from_an_ip_address $this->assertInstanceOf(SslCertificate::class, $sslCertificate); } + /** @test */ + public function it_can_download_a_certificate_for_a_host_name_from_an_ip_address_with_hostport() + { + $sslCertificate = SslCertificate::download() + ->fromIpAddress($this->ipDomainWithDifferentPort) + ->forHost($this->domainWithDifferentPort . ':' . $this->differentPort); + + $this->assertInstanceOf(SslCertificate::class, $sslCertificate); + } + /** @test */ public function it_sets_a_fingerprint_on_the_downloaded_certificate() { @@ -44,6 +83,14 @@ public function it_sets_a_fingerprint_on_the_downloaded_certificate() $this->assertNotEmpty($sslCertificate->getFingerprint()); } + /** @test */ + public function it_sets_a_fingerprint_on_the_downloaded_certificate_with_hostport() + { + $sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort); + + $this->assertNotEmpty($sslCertificate->getFingerprint()); + } + /** @test */ public function it_can_download_all_certificates_from_a_host_name() { @@ -97,4 +144,12 @@ public function it_can_retrieve_the_ip_address_of_the_server_that_served_the_cer $this->assertEquals('138.197.187.74:443', $sslCertificate->getRemoteAddress()); } + + /** @test */ + public function it_can_retrieve_the_ip_address_of_the_server_that_served_the_certificates_with_hostport() + { + $sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort); + + $this->assertEquals($this->ipDomainWithDifferentPort . ':' . $this->differentPort, $sslCertificate->getRemoteAddress()); + } } diff --git a/tests/SslCertificateFromStringTest.php b/tests/SslCertificateFromStringTest.php index 6ff22bb..49ff78c 100644 --- a/tests/SslCertificateFromStringTest.php +++ b/tests/SslCertificateFromStringTest.php @@ -14,6 +14,9 @@ class SslCertificateFromStringTest extends TestCase /** @var Spatie\SslCertificate\SslCertificate */ protected $certificate; + protected $domainWithDifferentPort; + protected $differentPort; + protected function setUp(): void { parent::setUp(); @@ -23,6 +26,9 @@ protected function setUp(): void $certificate = file_get_contents(__DIR__.'/stubs/spatieCertificate.pem'); $this->certificate = SslCertificate::createFromString($certificate); + + $this->domainWithDifferentPort = 'ben.hotweb.de'; + $this->differentPort = 8443; } /** @test */ @@ -116,6 +122,16 @@ public function it_provides_a_fluent_interface_to_set_all_options() $this->assertSame('spatie.be', $downloadedCertificate->getDomain()); } + /** @test */ + public function it_provides_a_fluent_interface_to_set_all_options_with_hostport() + { + $downloadedCertificate = SslCertificate::download() + ->setTimeout(30) + ->forHost($this->domainWithDifferentPort . ':' . $this->differentPort); + + $this->assertSame($this->domainWithDifferentPort, $downloadedCertificate->getDomain()); + } + /** @test */ public function it_can_convert_the_certificate_to_json() { diff --git a/tests/SslCertificateTest.php b/tests/SslCertificateTest.php index 1121fa0..b27cce0 100644 --- a/tests/SslCertificateTest.php +++ b/tests/SslCertificateTest.php @@ -15,6 +15,9 @@ class SslCertificateTest extends TestCase /** @var Spatie\SslCertificate\SslCertificate */ protected $certificate; + protected $domainWithDifferentPort; + protected $differentPort; + protected function setUp(): void { parent::setUp(); @@ -24,6 +27,9 @@ protected function setUp(): void $rawCertificateFields = json_decode(file_get_contents(__DIR__.'/stubs/spatieCertificateFields.json'), true); $this->certificate = new SslCertificate($rawCertificateFields); + + $this->domainWithDifferentPort = 'ben.hotweb.de'; + $this->differentPort = 8443; } public function it_can_get_the_raw_certificate_fields() @@ -177,6 +183,16 @@ public function it_provides_a_fluent_interface_to_set_all_options() $this->assertSame('spatie.be', $downloadedCertificate->getDomain()); } + /** @test */ + public function it_provides_a_fluent_interface_to_set_all_options_with_hostport() + { + $downloadedCertificate = SslCertificate::download() + ->setTimeout(30) + ->forHost($this->domainWithDifferentPort . ':' . $this->differentPort); + + $this->assertSame($this->domainWithDifferentPort, $downloadedCertificate->getDomain()); + } + /** @test */ public function it_can_convert_the_certificate_to_json() { @@ -251,6 +267,12 @@ public function it_can_be_encoded_as_json() $serializable = serialize($sslCertificate); $this->assertGreaterThan(1000, strlen($serializable)); + + $sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort); + + $serializable = serialize($sslCertificate); + + $this->assertGreaterThan(1000, strlen($serializable)); } /** @test */