MooKoo是一个基于Python的Mock http server
pip install mookoo
mookoo mock1 && cd mock1 && python mock.py
# 在浏览器中访问 http://localhost:7928/hellomkdir mock1; cd mock1
git clone https://github.com/gaorx/mookoo.git && rm -rf mookoo/.git
vim mock.py在mock1目录中,编辑mock.py
from mookoo import *
GET('/hello').json({"message": "Hello MooKoo!"})
run()然后在shell中执行
python mock.py
# 也可以设定端口
# python mock.py -p 9928然后在浏览器中就访问http://localhost:7928/hello,就可以看到
{"message": "Hello Mookoo"}也可以访问http://localhost:7928/+mookoo查看此帮助文件
在mock1目录中,创建hello.json,然后编辑它,然后使用下面的代码动态加载
GET('/hello').load_json('hello.json')Status和Header
# 定制Status
GET('/404').html('<h1>Not found</h1>', status=404)
# 加入Header
GET('/custom_header').text("Press F12", header={"My-Header": "HeaderContent"})
# 修改content_type
GET('/custom_content_type').text("<h1>Press F12</h1>", content_type='text/html')@GET('/dynamic/<sub>')
def _dynamic(sub):
response.content_type = 'text/plain'
return "Sub path is %s, query_string is '%s'" % (sub, request.query_string)将一张在mock1目录中复制一张图片hello.jpg
GET('/image').static_file('hello.jpg')@mookoo.GET('/static/<filename:path>')
def _static_dir(filename):
return mookoo.static_file(os.path.join('static_dir', filename))在mock1目录中,创建hello.json.py,内容为:
JSON = {
"message": "Python json",
"query_string": request.query_string,
}然后使用下面的代码加载:
GET('/dynamic_json').load_json('hello.json.py')可以使用mookoo.helper注册共享函数,然后在动态JSON中可以使用helpers来使用这些函数。例如在mock.py中:
@helper
def helper1():
return "Helper1"
@helper()
def helper2():
return "Helper2"
@helper('helper3')
def the_name_is_not_helper3():
return "Helper3"
GET('/dynamic_json').load_json('hello.json.py')在hello.json.py中
JSON = {
"message": "Python json",
"query_string": request.query_string,
"helper1": helpers.helper1(),
"helper2": helpers.helper2(),
"helper3": helpers.helper3(),
}GET('/redirect').redirect('https://github.com/gaorx/mookoo')GET('/http_rfc').proxy('https://tools.ietf.org/rfc/rfc2616.txt')注意: Proxy只会使用转发源Header中的Status和Content-Type,其余Header不转发。