繁体   English   中英

Flask test_client 删除查询字符串参数

[英]Flask test_client removes query string parameters

我正在使用 Flask 创建几个非常简单的服务。 从外部测试(使用 HTTPie)参数通过查询字符串到达​​服务。

但如果我使用类似的东西。

    data = {
        'param1': 'somevalue1',
        'param2': 'somevalue2'}

    response = self.client.get(url_for("api.my-service", **data))

我可以看到正在创建的正确 URI:

http://localhost:5000/api1.0/my-service?param1=somevalue1&param2=somevalue2

当我断点进入服务时:

request.args

实际上是空的。

self.client是通过在我配置的 Flask 应用程序上调用app.test_client()创建的。

任何人都知道为什么之后? 被扔掉或如何在仍然使用test_client同时解决它?

我刚刚找到了一个解决方法。

制作

data = {
    'param1': 'somevalue1',
    'param2': 'somevalue2'}

response = self.client.get(url_for("api.my-service", **data))

进入这个:

data = {
    'param1': 'somevalue1',
    'param2': 'somevalue2'}

response = self.client.get(url_for("api.my-service"), query_string = data)

这有效,但似乎有点不直观,并且调试有一个地方,URI 中提供的查询字符串被丢弃......

但无论如何,这暂时有效。

我知道这是一个旧帖子,但我也遇到了这个。 在flask github repository中有一个关于此的未决问题 看来这是预期的行为。 从问题线程中的响应:

mitsuhiko 于 2013 年 7 月 24 日发表评论
这是目前的预期行为。 测试客户端的第一个参数应该是一个相对 url。 如果不是,则参数将被删除,因为它被视为与第二个 url 连接。 这有效:

>>> from flask import Flask, request
>>> app = Flask(__name__)
>>> app.testing = True
>>> @app.route('/')
... def index():
...  return request.url
... 
>>> c = app.test_client()
>>> c.get('/?foo=bar').data
'http://localhost/?foo=bar'

将绝对 url 转换为相对 url 并保留查询字符串的一种方法是使用 urlparse:

from urlparse import urlparse

absolute_url = "http://someurl.com/path/to/endpoint?q=test"
parsed = urlparse(absolute_url)
path = parsed[2] or "/"
query = parsed[4]
relative_url = "{}?{}".format(path, query)

print relative_url

对我来说,解决方案是在with语句中使用客户端:

with app.app_context():
    with app.test_request_context():
        with app.test_client() as client:
            client.get(...)

代替

client = app.test_client()
client.get(...)

我将测试客户端的创建放在一个夹具中,以便为每个测试方法“自动”创建它:

from my_back_end import MyBackEnd

sut = None
app = None
client = None

@pytest.fixture(autouse=True)
def before_each():
    global sut, app, client
    sut = MyBackEnd()
    app = sut.create_application('frontEndPathMock')
    with app.app_context():
        with app.test_request_context():                
            with app.test_client() as client:
                yield

如果您正在尝试除 GET 之外的任何其他 HTTP 方法

response = self.client.patch(url_for("api.my-service"), query_string=data,
                             data="{}")

data="{}"data=json.dumps({})应该在那里,即使正文中没有内容。 否则,它会导致 BAD 请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM