|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | +from click.testing import CliRunner |
| 5 | + |
| 6 | +from chattool.client.main import cli |
| 7 | + |
| 8 | + |
| 9 | +pytestmark = [pytest.mark.e2e] |
| 10 | + |
| 11 | + |
| 12 | +def test_chattool_nginx_basic(tmp_path: Path): |
| 13 | + runner = CliRunner() |
| 14 | + |
| 15 | + result = runner.invoke( |
| 16 | + cli, |
| 17 | + [ |
| 18 | + "nginx", |
| 19 | + "proxy-pass", |
| 20 | + "--set", |
| 21 | + "SERVER_NAME=app.example.com", |
| 22 | + "--set", |
| 23 | + "PROXY_PASS=http://127.0.0.1:8080", |
| 24 | + ], |
| 25 | + ) |
| 26 | + assert result.exit_code == 0 |
| 27 | + assert "server_name app.example.com;" in result.output |
| 28 | + assert "proxy_pass http://127.0.0.1:8080;" in result.output |
| 29 | + |
| 30 | + ws_file = tmp_path / "websocket.conf" |
| 31 | + result = runner.invoke( |
| 32 | + cli, |
| 33 | + [ |
| 34 | + "nginx", |
| 35 | + "websocket-proxy", |
| 36 | + str(ws_file), |
| 37 | + "--set", |
| 38 | + "SERVER_NAME=ws.example.com", |
| 39 | + "--set", |
| 40 | + "PROXY_PASS=http://127.0.0.1:3000", |
| 41 | + "--force", |
| 42 | + ], |
| 43 | + ) |
| 44 | + assert result.exit_code == 0 |
| 45 | + ws_content = ws_file.read_text(encoding="utf-8") |
| 46 | + assert 'proxy_set_header Connection "upgrade";' in ws_content |
| 47 | + assert "proxy_http_version 1.1;" in ws_content |
| 48 | + assert "proxy_set_header Upgrade $http_upgrade;" in ws_content |
| 49 | + |
| 50 | + nas_file = tmp_path / "nas.conf" |
| 51 | + result = runner.invoke( |
| 52 | + cli, |
| 53 | + [ |
| 54 | + "nginx", |
| 55 | + "static-root", |
| 56 | + str(nas_file), |
| 57 | + "--set", |
| 58 | + "SERVER_NAME=share.example.com", |
| 59 | + "--set", |
| 60 | + "ROOT_DIR=/storage/nas", |
| 61 | + "--force", |
| 62 | + ], |
| 63 | + ) |
| 64 | + assert result.exit_code == 0 |
| 65 | + nas_content = nas_file.read_text(encoding="utf-8") |
| 66 | + assert "root /storage/nas;" in nas_content |
| 67 | + assert "autoindex on;" in nas_content |
| 68 | + assert "charset utf-8;" in nas_content |
| 69 | + |
| 70 | + result = runner.invoke( |
| 71 | + cli, |
| 72 | + [ |
| 73 | + "nginx", |
| 74 | + "redirect", |
| 75 | + "--set", |
| 76 | + "SERVER_NAME=example.com www.example.com", |
| 77 | + ], |
| 78 | + ) |
| 79 | + assert result.exit_code == 0 |
| 80 | + assert "return 301 https://$host$request_uri;" in result.output |
0 commit comments