Skip to content

Commit

Permalink
Merge pull request #69 from Eastwu5788/wudong
Browse files Browse the repository at this point in the history
Wudong
  • Loading branch information
Eastwu5788 committed Apr 23, 2020
2 parents 2f90b17 + e1a6cbc commit bc2af62
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include README.md LICENSE
include README.rst DESCRIPTION.rst LICENSE
4 changes: 4 additions & 0 deletions pre_request/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def location(self, value):
"""
df_location = ["args", "form", "values", "headers", "cookies", "json"]

if value is None:
self._location = value
return

if not isinstance(value, str) and not isinstance(value, list):
raise TypeError("location must be type of list or str")

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

readme = None

if os.path.exists('README.rst'):
if os.path.exists('DESCRIPTION.rst'):
with open('DESCRIPTION.rst', 'rb') as f:
readme = f.read().decode("utf-8")

Expand Down
27 changes: 27 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,33 @@ def test_lte_key_handler(params):
return json_resp(params)


# 读取指定位置的数据
location_params = {
"p1": Rule(type=int, location="args"),
"p2": Rule(type=int, location="form"),
"p3": Rule(type=int, location="values"),
"p4": Rule(type=int, location="headers"),
"p5": Rule(type=int, location="cookies"),
"p7": Rule(type=int, location=["cookies", "args", "headers"])
}

location_json_params = {
"p6": Rule(type=int, location="json")
}


@app.route("/location", methods=["GET", "POST"])
@pre.catch(location_params)
def example_location_handler(params):
return json_resp(params)


@app.route("/location/json", methods=["GET", "POST"])
@pre.catch(location_json_params)
def example_location_json_handler(params):
return json_resp(params)


@pytest.fixture
def client():
""" 构建测试用例
Expand Down
27 changes: 27 additions & 0 deletions tests/test_flask/test_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# !/usr/local/python/bin/python
# -*- coding: utf-8 -*-
# (C) Wu Dong, 2020
# All rights reserved
# @Author: 'Wu Dong <wudong@eastwu.cn>'
# @Time: '2020-04-23 15:52'


class TestLocation:

def test_location_filter(self, client):
""" 测试指定Location位置的数据
"""
client.set_cookie("localhost", "p5", "5")
resp = client.get("/location?p1=1", data={
"p2": 2,
"p3": 3,
}, headers={
"p4": "4",
"p7": "7"
})
assert resp.json == {"p1": 1, "p2": 2, "p3": 3, "p4": 4, "p5": 5, "p7": 7}

resp = client.get("/location/json", json={
"p6": 6
})
assert resp.json == {"p6": 6}

0 comments on commit bc2af62

Please sign in to comment.