# coding=utf8
from __future__ import unicode_literals
import threading
import time
import json
import requests
from werkzeug.serving import run_simple
from werkzeug.wrappers import Request, Response
from tests.mock import pickup_port
from zerotest.utils.generator_helper import get_name_from_request
from zerotest.utils.url_helper import urljoin
class Server(object):
def __init__(self):
self.count = 0
self.host = None
self.port = None
self.thread = None
self.running = False
def dispatch_request(self, request):
handler = getattr(self, get_name_from_request(request), None)
if handler:
return handler(request)
else:
return Response("non route", status=404)
def get_count(self, request):
return Response(json.dumps(dict(count=self.count)),
content_type='application/json; charset=utf-8')
def post_echo(self, request):
return Response(request.data,
content_type=request.headers['content_type'])
def post_raw_to_json(self, request):
data = request.data
return Response(data,
content_type='application/json')
def get_chinese_hello_world(self, request):
return Response(json.dumps(dict(count=self.count, content="你好,世界")),
content_type='application/json; charset=utf-8')
def delete_shutdown_server(self, request):
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
return Response("shutdown")
def __call__(self, environ, start_response):
request = Request(environ)
self.count += 1
environ['HTTP_COUNT'] = self.count
return self.dispatch_request(request)(environ, start_response)
def start(self, host, port):
self.host = host
self.port = port
self.running = True
run_simple(host, port, self)
@property
def url(self):
return 'http://{}:{}'.format(self.host, self.port)
def start_daemon(self, host, port):
self.thread = threading.Thread(target=lambda: self.start(host, port))
self.thread.start()
def shutdown(self):
if self.running:
requests.delete(urljoin(self.url, 'shutdown_server'))
self.running = False
self.thread.join(30)
def start_mock_server(self):
port = pickup_port()
self.start_daemon('127.0.0.1', port)
test_count = 10
while test_count > 0:
try:
if self.running:
r = requests.get(urljoin(self.url, '/count'))
if r.status_code == 200:
return
print("wait server start, response", r.status_code, r.text)
except requests.exceptions.ConnectionError:
pass
time.sleep(1)
test_count -= 1
else:
self.shutdown()
raise RuntimeError("start mock server timeout")
Mocked API server
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- docker api Docker is running on client / server model, ea...
- 场景 使用webpack-dev-server驱动前端项目时,开发工程中由于访问的webpack-dev-serv...
- 以往因為javascript的許多殘缺設計,使得我轉身投靠iced-coffeescript(進階版coffees...
- configuration has an unknown property 'colors'. These pro...