forked from logandk/serverless-wsgi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_test.py
103 lines (79 loc) · 2.77 KB
/
serve_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import importlib
import pytest
import serve
import sys
from werkzeug import serving
class ObjectStub:
def __init__(self, **kwds):
self.__dict__.update(kwds)
@pytest.fixture
def mock_werkzeug(monkeypatch):
stub = ObjectStub(lastcall=None)
def mock_serving(host, port, app, **kwargs):
stub.lastcall = ObjectStub(host=host, port=port, app=app, kwargs=kwargs)
monkeypatch.setattr(serving, "run_simple", mock_serving)
return stub
@pytest.fixture
def mock_importlib(monkeypatch):
app = ObjectStub()
app.app = ObjectStub()
def mock_import_module(module):
if app.app:
app.app.module = module
return app
monkeypatch.setattr(importlib, "import_module", mock_import_module)
return app
@pytest.fixture
def mock_path(monkeypatch):
path = []
monkeypatch.setattr(sys, "path", path)
return path
def test_serve(mock_path, mock_importlib, mock_werkzeug):
serve.serve("/tmp1", "app.app", "5000")
assert len(mock_path) == 1
assert mock_path[0] == "/tmp1"
assert mock_werkzeug.lastcall.host == "localhost"
assert mock_werkzeug.lastcall.port == 5000
assert mock_werkzeug.lastcall.app.module == "app"
assert mock_werkzeug.lastcall.app.debug
assert mock_werkzeug.lastcall.kwargs == {
"use_reloader": True,
"use_debugger": True,
"use_evalex": True,
"threaded": True,
}
def test_serve_alternative_hostname(mock_path, mock_importlib, mock_werkzeug):
serve.serve("/tmp1", "app.app", "5000", "0.0.0.0")
assert len(mock_path) == 1
assert mock_path[0] == "/tmp1"
assert mock_werkzeug.lastcall.host == "0.0.0.0"
assert mock_werkzeug.lastcall.port == 5000
assert mock_werkzeug.lastcall.app.module == "app"
assert mock_werkzeug.lastcall.app.debug
assert mock_werkzeug.lastcall.kwargs == {
"use_reloader": True,
"use_debugger": True,
"use_evalex": True,
"threaded": True,
}
def test_serve_from_subdir(mock_path, mock_importlib, mock_werkzeug):
serve.serve("/tmp2", "subdir/app.app", "5000")
assert len(mock_path) == 2
assert mock_path[0] == "/tmp2/subdir"
assert mock_path[1] == "/tmp2"
assert mock_werkzeug.lastcall.host == "localhost"
assert mock_werkzeug.lastcall.port == 5000
assert mock_werkzeug.lastcall.app.module == "app"
assert mock_werkzeug.lastcall.app.debug
assert mock_werkzeug.lastcall.kwargs == {
"use_reloader": True,
"use_debugger": True,
"use_evalex": True,
"threaded": True,
}
def test_serve_non_debuggable_app(mock_path, mock_importlib, mock_werkzeug):
mock_importlib.app = None
serve.serve("/tmp1", "app.app", "5000")
assert mock_werkzeug.lastcall.app is None