-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support pre.parse function to parase input params
- Loading branch information
1 parent
74b7ada
commit 43b376b
Showing
8 changed files
with
195 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-05-26 10:50' | ||
from flask import Flask | ||
from pre_request import pre, Rule | ||
from pre_request import ParamsValueError | ||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
# 指定email=True,此时框架会自动判断用户入参是否符合email正则 | ||
params = { | ||
"params": Rule(email=True) | ||
} | ||
|
||
|
||
@app.errorhandler(ParamsValueError) | ||
def params_value_error(e): | ||
return pre.fmt_resp(e) | ||
|
||
|
||
@app.route("/email", methods=["GET", "POST"]) | ||
def example_email_handler(): | ||
return str(pre.parse(params)) | ||
|
||
|
||
def example_email_filter(): | ||
""" 演示邮箱验证 | ||
""" | ||
resp = client.get("/email", data={ | ||
"params": "wudong@eastwu.cn" | ||
}) | ||
print(resp.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_email_filter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-05-26 10:54' | ||
import json | ||
from flask import Flask, make_response | ||
from pre_request import pre, Rule | ||
from pre_request import ParamsValueError | ||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
def json_resp(result): | ||
result = json.dumps(result) | ||
resp = make_response(result) | ||
resp.headers['Content-Type'] = 'application/json' | ||
return resp | ||
|
||
|
||
# 指定email=True,此时框架会自动判断用户入参是否符合email正则 | ||
email_params = { | ||
"params": Rule(email=True) | ||
} | ||
|
||
|
||
@app.errorhandler(ParamsValueError) | ||
def params_value_error(e): | ||
return pre.fmt_resp(e) | ||
|
||
|
||
@app.route("/email", methods=["GET", "POST"]) | ||
def example_email_handler(): | ||
params = pre.parse(get=email_params) | ||
return json_resp(params) | ||
|
||
|
||
class TestParse: | ||
|
||
def test_parse_smoke(self): | ||
resp = app.test_client().get("/email", data={ | ||
"params": "wudong@eastwu.cn" | ||
}) | ||
assert resp.json == {"params": "wudong@eastwu.cn"} | ||
|
||
def test_parse_error(self): | ||
resp = app.test_client().get("/email", data={ | ||
"params": "wudong@@eastwu.cn" | ||
}) | ||
assert resp.json["respCode"] == 564 |