|
| 1 | +<?php |
| 2 | + |
| 3 | +use CoyoteCert\Challenge\Http01Handler; |
| 4 | +use CoyoteCert\Enums\AuthorizationChallengeEnum; |
| 5 | +use CoyoteCert\Exceptions\LetsEncryptClientException; |
| 6 | + |
| 7 | +beforeEach(function () { |
| 8 | + $this->webroot = sys_get_temp_dir() . '/coyote-http01-' . uniqid(); |
| 9 | + $this->handler = new Http01Handler($this->webroot); |
| 10 | +}); |
| 11 | + |
| 12 | +afterEach(function () { |
| 13 | + $dir = $this->webroot . '/.well-known/acme-challenge'; |
| 14 | + if (is_dir($dir)) { |
| 15 | + foreach (glob($dir . '/*') ?: [] as $f) { |
| 16 | + @unlink($f); |
| 17 | + } |
| 18 | + @rmdir($dir); |
| 19 | + @rmdir($this->webroot . '/.well-known'); |
| 20 | + @rmdir($this->webroot); |
| 21 | + } |
| 22 | +}); |
| 23 | + |
| 24 | +it('supports the http-01 challenge type', function () { |
| 25 | + expect($this->handler->supports(AuthorizationChallengeEnum::HTTP))->toBeTrue(); |
| 26 | +}); |
| 27 | + |
| 28 | +it('does not support dns-01', function () { |
| 29 | + expect($this->handler->supports(AuthorizationChallengeEnum::DNS))->toBeFalse(); |
| 30 | +}); |
| 31 | + |
| 32 | +it('does not support dns-persist-01', function () { |
| 33 | + expect($this->handler->supports(AuthorizationChallengeEnum::DNS_PERSIST))->toBeFalse(); |
| 34 | +}); |
| 35 | + |
| 36 | +it('deploy creates the challenge file', function () { |
| 37 | + $this->handler->deploy('example.com', 'tokenABC', 'tokenABC.thumbprint'); |
| 38 | + |
| 39 | + $path = $this->webroot . '/.well-known/acme-challenge/tokenABC'; |
| 40 | + expect(file_exists($path))->toBeTrue(); |
| 41 | + expect(file_get_contents($path))->toBe('tokenABC.thumbprint'); |
| 42 | +}); |
| 43 | + |
| 44 | +it('deploy creates the challenge directory when it does not exist', function () { |
| 45 | + expect(is_dir($this->webroot))->toBeFalse(); |
| 46 | + |
| 47 | + $this->handler->deploy('example.com', 'tok', 'content'); |
| 48 | + |
| 49 | + expect(is_dir($this->webroot . '/.well-known/acme-challenge'))->toBeTrue(); |
| 50 | +}); |
| 51 | + |
| 52 | +it('deploy works with a trailing slash in webroot', function () { |
| 53 | + $handler = new Http01Handler($this->webroot . '/'); |
| 54 | + $handler->deploy('example.com', 'tok2', 'c2'); |
| 55 | + |
| 56 | + $path = $this->webroot . '/.well-known/acme-challenge/tok2'; |
| 57 | + expect(file_exists($path))->toBeTrue(); |
| 58 | +}); |
| 59 | + |
| 60 | +it('cleanup removes the challenge file', function () { |
| 61 | + $this->handler->deploy('example.com', 'tokenXYZ', 'content'); |
| 62 | + |
| 63 | + $path = $this->webroot . '/.well-known/acme-challenge/tokenXYZ'; |
| 64 | + expect(file_exists($path))->toBeTrue(); |
| 65 | + |
| 66 | + $this->handler->cleanup('example.com', 'tokenXYZ'); |
| 67 | + expect(file_exists($path))->toBeFalse(); |
| 68 | +}); |
| 69 | + |
| 70 | +it('cleanup is a no-op when the file does not exist', function () { |
| 71 | + expect(fn () => $this->handler->cleanup('example.com', 'nonexistent'))->not->toThrow(\Throwable::class); |
| 72 | +}); |
0 commit comments