0

我正在尝试为 Falcon 框架编写一些 pytest,并按照此处的说明进行操作。我想模拟一个 POST 请求。但是,我不断收到以下错误

测试/test_app.py:29: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../../harvester -venv/lib/python2.7/site-packages/falcon/testing/client.py:170:在json中返回json.loads(self.text)/System/Library/Frameworks/Python.framework/Versions/2.7/lib /python2.7/json/ init .py:338:在加载中返回 _default_decoder.decode(s) /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py:366 : 在解码 obj 中,end = self.raw_decode(s, idx=_w(s, 0).end())


自我 = , s = '', idx = 0

def raw_decode(self, s, idx=0):
    """Decode a JSON document from ``s`` (a ``str`` or ``unicode``
        beginning with a JSON document) and return a 2-tuple of the Python
        representation and the index in ``s`` where the document ended.

        This can be used to decode a JSON document from a string that may
        have extraneous data at the end.

        """
    try:
        obj, end = self.scan_once(s, idx)
    except StopIteration:
      raise ValueError("No JSON object could be decoded") E           ValueError: No JSON object could be decoded

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py:384:ValueError

这是我正在尝试编写的 Falcon 测试。当我验证我在JSONlint.comdata上放入变量的 JSON 时,它向我显示数据是有效的,因此问题似乎与格式无关。

import json

import pytest
from falcon import testing

from harvester.app import api


@pytest.fixture()
def client():
    return testing.TestClient(api)


def test_elasticsearch_endpoint(client):
    data = {
        "Type": "SubscriptionConfirmation",
        "MessageId": "0a069ec4-2e6f-4436-9f1d-aa55c3b048f9",
        "Token": "2336412f37fb68751e6e241d59b68cb9ca332001818266bdd4984dd60a76ff2c8a43220b28241ad0ae6659d6313bb2336e98d19bdbc52e0c99578ad43934324b5e73a20e9ad517741cf14a57793d052e9986038ee688a059b34e49746d106bcd597f18f7ff3560be204ef8cd339a3c5276bfa3cc784a7904c8720519387a0",
        "TopicArn": "arn:aws:sns:ap-south-1:141592612890",
        "SubscribeURL": "https://sns.ap-south-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:ap-south-1:141592612890:harvester_test&Token=2336412f37fb687f5d51e6e241d59b68cb9ca332001818266bdd4984dd60a76ff2c8a41ad0ae6659d6313bb2336e98d19bdbc52e0c99578ad43934324b5e73a20e9ad517741cf14a57793d052e9986038ee688a059b34e49746d106bcd597f18f7ff3560be204ef8cd339a3c5276bfa3cc784a7904c8720519387a0",
        "Timestamp": "2017-06-01T13:22:49.849Z",
        "SignatureVersion": "1",
        "Signature": "Pj9F8PrgqPkSuLjHtrJ9pmh3ZH3kZBaLs5Ywx1C0rrOc4PJp3hYiria9SZr1Xm8uE549khxDFIdAsnGee9fSeO7tZWSNI3W3gRLVnIJ0uAjxU0oicj3P7NnGQ5kUnihKva//Q39RlZOIr4OsxTvOrXnag6M32aC3pEFdBaXJqO0iJJOokT+mmoWa9BWfHXnb/ORAigo50BXsVNSN92PRZAZ7qTeypZSU70EF1+vKNt7mbxrOE2/wpOtb7uDfg/ZW8yZQQqr100bnQVfStDSp6MzID+vupQhM2PR/gS84INA+VdOUhxll/kEkDE98tR9OrNz/PITts5XSg==",
        "SigningCertURL": "https://sns.ap-south-1.amazonaws.com/SimpleNotificationService-b95095beb82e8f6a04.pem"
    }
    result = client.simulate_post('/v1/track/analytics', body=json.dumps(data))
    print result.json

任何线索为什么会发生这种情况?

4

3 回答 3

1

第一步是在标题中设置 {"Content-Type": "application/json"}。

此外,您发送有效载荷的方式可以调整如下:

from urllib.parse import urlencode

data = urlencode({
        "Type": "SubscriptionConfirmation",
        "MessageId": "0a069ec4-2e6f-4436-9f1d-aa55c3b048f9",
        "Token": "2336412f37fb68751e6e241d59b68cb9ca332001818266bdd4984dd60a76ff2c8a43220b28241ad0ae6659d6313bb2336e98d19bdbc52e0c99578ad43934324b5e73a20e9ad517741cf14a57793d052e9986038ee688a059b34e49746d106bcd597f18f7ff3560be204ef8cd339a3c5276bfa3cc784a7904c8720519387a0",
        "TopicArn": "arn:aws:sns:ap-south-1:141592612890",
        "SubscribeURL": "https://sns.ap-south-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:ap-south-1:141592612890:harvester_test&Token=2336412f37fb687f5d51e6e241d59b68cb9ca332001818266bdd4984dd60a76ff2c8a41ad0ae6659d6313bb2336e98d19bdbc52e0c99578ad43934324b5e73a20e9ad517741cf14a57793d052e9986038ee688a059b34e49746d106bcd597f18f7ff3560be204ef8cd339a3c5276bfa3cc784a7904c8720519387a0",
        "Timestamp": "2017-06-01T13:22:49.849Z",
        "SignatureVersion": "1",
        "Signature": "Pj9F8PrgqPkSuLjHtrJ9pmh3ZH3kZBaLs5Ywx1C0rrOc4PJp3hYiria9SZr1Xm8uE549khxDFIdAsnGee9fSeO7tZWSNI3W3gRLVnIJ0uAjxU0oicj3P7NnGQ5kUnihKva//Q39RlZOIr4OsxTvOrXnag6M32aC3pEFdBaXJqO0iJJOokT+mmoWa9BWfHXnb/ORAigo50BXsVNSN92PRZAZ7qTeypZSU70EF1+vKNt7mbxrOE2/wpOtb7uDfg/ZW8yZQQqr100bnQVfStDSp6MzID+vupQhM2PR/gS84INA+VdOUhxll/kEkDE98tR9OrNz/PITts5XSg==",
        "SigningCertURL": "https://sns.ap-south-1.amazonaws.com/SimpleNotificationService-b95095beb82e8f6a04.pem"
})

headers = {"Content-Type": "application/json"}
result = client.simulate_post('/v1/track/analytics', body=data, headers=headers)

然后,这应该可以解决问题。让我知道。

于 2019-01-26T09:57:45.960 回答
0

不熟悉猎鹰。但在这里进行疯狂的猜测:在您的夹具中,您只需传递 api 类/函数定义,但您没有调用api()或调用api.create(). 尝试:

@pytest.fixture()
def client():
    return testing.TestClient(api())  # or api.create() ?

您发布的错误也指向第 29 行,但您发布的代码片段仅包含 27 行。

于 2017-06-13T06:08:23.533 回答
0

触发验证错误的是调用result.json而不是您的 POSTed JSON。大概您正在测试的 elasticsearch 端点正在发回一个非 JSON 错误文档。

https://github.com/falconry/falcon/blob/master/falcon/testing/client.py#L153

文档确实说result.json如果结果无法解析为 JSON,则会引发错误,但这肯定会更清楚!

http://falcon.readthedocs.io/en/stable/api/testing.html#falcon.testing.Result.json

于 2018-05-25T11:13:39.523 回答