|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Highlight\Tests\Languages\Nginx; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Tempest\Highlight\Highlighter; |
| 10 | + |
| 11 | +class NginxLanguageTest extends TestCase |
| 12 | +{ |
| 13 | + #[DataProvider('provide_highlight_cases')] |
| 14 | + public function test_highlight(string $content, string $expected): void |
| 15 | + { |
| 16 | + $highlighter = new Highlighter(); |
| 17 | + |
| 18 | + $this->assertSame( |
| 19 | + $expected, |
| 20 | + $highlighter->parse($content, 'nginx'), |
| 21 | + ); |
| 22 | + |
| 23 | + $this->assertSame( |
| 24 | + $expected, |
| 25 | + $highlighter->parse($content, 'nginxconf'), |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + public static function provide_highlight_cases(): iterable |
| 30 | + { |
| 31 | + return [ |
| 32 | + [<<<'TXT' |
| 33 | +# Reverse proxy configuration |
| 34 | +server { |
| 35 | + listen 80; |
| 36 | + server_name example.com; |
| 37 | +
|
| 38 | + location / { |
| 39 | + proxy_pass http://backend; |
| 40 | + proxy_set_header Host $host; |
| 41 | + } |
| 42 | +
|
| 43 | + location ~* \.(jpg|css|js)$ { |
| 44 | + expires 30d; |
| 45 | + gzip on; |
| 46 | + } |
| 47 | +} |
| 48 | +TXT, |
| 49 | + <<<'TXT' |
| 50 | +<span class="hl-comment"># Reverse proxy configuration</span> |
| 51 | +<span class="hl-keyword">server</span> <span class="hl-operator">{</span> |
| 52 | + <span class="hl-keyword">listen</span> <span class="hl-number">80</span><span class="hl-operator">;</span> |
| 53 | + <span class="hl-keyword">server_name</span> example.com<span class="hl-operator">;</span> |
| 54 | +
|
| 55 | + <span class="hl-keyword">location</span> / <span class="hl-operator">{</span> |
| 56 | + <span class="hl-keyword">proxy_pass</span> <span class="hl-keyword">http</span>://backend<span class="hl-operator">;</span> |
| 57 | + <span class="hl-keyword">proxy_set_header</span> Host <span class="hl-variable">$host</span><span class="hl-operator">;</span> |
| 58 | + <span class="hl-operator">}</span> |
| 59 | +
|
| 60 | + <span class="hl-keyword">location</span> <span class="hl-operator">~*</span> \.(jpg|css|js)$ <span class="hl-operator">{</span> |
| 61 | + <span class="hl-keyword">expires</span> 30d<span class="hl-operator">;</span> |
| 62 | + <span class="hl-keyword">gzip</span> <span class="hl-value">on</span><span class="hl-operator">;</span> |
| 63 | + <span class="hl-operator">}</span> |
| 64 | +<span class="hl-operator">}</span> |
| 65 | +TXT], |
| 66 | + [<<<'TXT' |
| 67 | +upstream backend { |
| 68 | + server 127.0.0.1:8080; |
| 69 | + server 127.0.0.1:8081; |
| 70 | + keepalive_timeout 65; |
| 71 | +} |
| 72 | +TXT, |
| 73 | + <<<'TXT' |
| 74 | +<span class="hl-keyword">upstream</span> backend <span class="hl-operator">{</span> |
| 75 | + <span class="hl-keyword">server</span> <span class="hl-number">127</span>.<span class="hl-number">0</span>.<span class="hl-number">0</span>.<span class="hl-number">1</span>:<span class="hl-number">8080</span><span class="hl-operator">;</span> |
| 76 | + <span class="hl-keyword">server</span> <span class="hl-number">127</span>.<span class="hl-number">0</span>.<span class="hl-number">0</span>.<span class="hl-number">1</span>:<span class="hl-number">8081</span><span class="hl-operator">;</span> |
| 77 | + <span class="hl-keyword">keepalive_timeout</span> <span class="hl-number">65</span><span class="hl-operator">;</span> |
| 78 | +<span class="hl-operator">}</span> |
| 79 | +TXT], |
| 80 | + [<<<'TXT' |
| 81 | +server { |
| 82 | + listen 443 ssl; |
| 83 | + ssl_certificate /etc/ssl/cert.pem; |
| 84 | + ssl_certificate_key /etc/ssl/key.pem; |
| 85 | +
|
| 86 | + add_header X-Frame-Options "SAMEORIGIN"; |
| 87 | +
|
| 88 | + if ($request_uri ~* "^/old") { |
| 89 | + return 301 $scheme://example.com/new; |
| 90 | + } |
| 91 | +} |
| 92 | +TXT, |
| 93 | + <<<'TXT' |
| 94 | +<span class="hl-keyword">server</span> <span class="hl-operator">{</span> |
| 95 | + <span class="hl-keyword">listen</span> <span class="hl-number">443</span> <span class="hl-keyword">ssl</span><span class="hl-operator">;</span> |
| 96 | + <span class="hl-keyword">ssl_certificate</span> /etc/<span class="hl-keyword">ssl</span>/cert.pem<span class="hl-operator">;</span> |
| 97 | + <span class="hl-keyword">ssl_certificate_key</span> /etc/<span class="hl-keyword">ssl</span>/key.pem<span class="hl-operator">;</span> |
| 98 | +
|
| 99 | + <span class="hl-keyword">add_header</span> X-Frame-Options <span class="hl-value">"SAMEORIGIN"</span><span class="hl-operator">;</span> |
| 100 | +
|
| 101 | + <span class="hl-keyword">if</span> (<span class="hl-variable">$request_uri</span> <span class="hl-operator">~*</span> <span class="hl-value">"^/old"</span>) <span class="hl-operator">{</span> |
| 102 | + <span class="hl-keyword">return</span> <span class="hl-number">301</span> <span class="hl-variable">$scheme</span>://example.com/new<span class="hl-operator">;</span> |
| 103 | + <span class="hl-operator">}</span> |
| 104 | +<span class="hl-operator">}</span> |
| 105 | +TXT], |
| 106 | + ]; |
| 107 | + } |
| 108 | +} |
0 commit comments