Skip to content

Commit

Permalink
feat: get port from URL (#143)
Browse files Browse the repository at this point in the history
* feat: get port from URL

* test: add tests passing port in URL

* docs: add passing the port in URL

Co-authored-by: Jodoval Luiz dos Santos Junior <[email protected]>
  • Loading branch information
jodovaljow and Jodoval Luiz dos Santos Junior authored Jul 8, 2021
1 parent ae3725c commit 68feddf
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
55 changes: 55 additions & 0 deletions tests/DownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -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());
}
}
16 changes: 16 additions & 0 deletions tests/SslCertificateFromStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class SslCertificateFromStringTest extends TestCase
/** @var Spatie\SslCertificate\SslCertificate */
protected $certificate;

protected $domainWithDifferentPort;
protected $differentPort;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -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 */
Expand Down Expand Up @@ -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()
{
Expand Down
22 changes: 22 additions & 0 deletions tests/SslCertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class SslCertificateTest extends TestCase
/** @var Spatie\SslCertificate\SslCertificate */
protected $certificate;

protected $domainWithDifferentPort;
protected $differentPort;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -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()
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 68feddf

Please sign in to comment.