Adds WSGI validator test

This commit is contained in:
James Roberts
2021-10-26 23:44:55 +02:00
parent 8d9b34fff8
commit 3743f3e1c1
4 changed files with 31 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
import pytest
import fastwsgi
import time
from multiprocessing import Process
@@ -10,10 +11,12 @@ PORT = 8080
class ServerProcess:
def __init__(self, application, host=HOST, port=PORT) -> None:
self.process = Process(target=fastwsgi.run, args=(application, host, port))
self.endpoint = f"http://{host}:{port}"
def __enter__(self):
self.process.start()
return self.process
time.sleep(1) # Allow server to start
return self
def __exit__(self, exc_type, exc_value, exc_tb):
if self.process.is_alive():

View File

@@ -6,11 +6,11 @@ app = Flask(__name__)
@app.get("/")
def hello_world():
return "Hello, World!", 200
return "Hello, Flask!", 200
def test_uwsgi_hello_world(server_process):
with server_process(app):
result = requests.get("http://127.0.0.1:8080/")
with server_process(app) as server:
result = requests.get(server.endpoint)
assert result.status_code == 200
assert result.text == "Hello, World!"
assert result.text == "Hello, Flask!"

View File

@@ -3,11 +3,11 @@ import requests
def wsgi_app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"Hello, World!"]
return [b"Hello, WSGI!"]
def test_uwsgi_hello_world(server_process):
with server_process(wsgi_app):
result = requests.get("http://127.0.0.1:8080/")
with server_process(wsgi_app) as server:
result = requests.get(server.endpoint)
assert result.status_code == 200
assert result.text == "Hello, World!"
assert result.text == "Hello, WSGI!"

View File

@@ -0,0 +1,19 @@
import requests
from wsgiref.validate import validator
def simple_app(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
return [b"Valid"]
validator_app = validator(simple_app)
def test_get_valid_wsgi_server(server_process):
with server_process(validator_app) as server:
result = requests.get(server.endpoint)
assert result.text == "Valid"