warpgate/tests/test_http_common.py

51 lines
1.1 KiB
Python
Raw Normal View History

2022-08-14 18:36:49 +08:00
import pytest
import threading
from .util import alloc_port
2022-11-12 00:00:12 +08:00
@pytest.fixture(scope="session")
2022-08-14 18:36:49 +08:00
def echo_server_port():
from flask import Flask, request, jsonify, redirect
from flask_sock import Sock
2022-11-12 00:00:12 +08:00
2022-08-14 18:36:49 +08:00
app = Flask(__name__)
sock = Sock(app)
2022-11-12 00:00:12 +08:00
@app.route("/set-cookie")
2022-08-14 18:36:49 +08:00
def set_cookie():
response = jsonify({})
2022-11-12 00:00:12 +08:00
response.set_cookie("cookie", "value")
2022-08-14 18:36:49 +08:00
return response
2022-11-12 00:00:12 +08:00
@app.route("/redirect/<path:url>")
2022-08-14 18:36:49 +08:00
def r(url):
return redirect(url)
2022-11-12 00:00:12 +08:00
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
2022-08-14 18:36:49 +08:00
def echo(path):
2022-11-12 00:00:12 +08:00
return jsonify(
{
"method": request.method,
"args": request.args,
"path": request.path,
}
)
@sock.route("/socket")
2022-08-14 18:36:49 +08:00
def ws_echo(ws):
while True:
data = ws.receive()
ws.send(data)
port = alloc_port()
def runner():
app.run(port=port, load_dotenv=False)
thread = threading.Thread(target=runner, daemon=True)
thread.start()
yield port