我正在尝试为Falcon框架编写一些pytests,并遵循here的说明。我想模拟一个POST请求。但是,我一直收到以下错误
测试/ test_app.py:29: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .. / .. / harvester-venv / lib / python2.7 /site-packages/falcon/testing/client.py:170: 在json return json.loads(self.text)/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py:338: 在负载 return _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())
self =,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异常
这是我试图写的猎鹰测试。当我验证我在JSONlint.com上的data
变量中放入的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
为什么会发生这种情况的任何线索?
答案 0 :(得分:0)
不熟悉猎鹰。但在这里猜测:
在你的灯具中你只需要传递api类/函数定义,但你不是在调用api()
或调用api.create()
。尝试:
@pytest.fixture()
def client():
return testing.TestClient(api()) # or api.create() ?
同样错误的是您将积分发布到第29行,但您发布的代码段只包含27行。
答案 1 :(得分:0)
调用result.json
而不是触发验证错误的POSTed JSON。据推测,您正在测试的弹性搜索端点正在发回非JSON错误文档。
https://github.com/falconry/falcon/blob/master/falcon/testing/client.py#L153
如果结果不能解析为JSON,那么文档会说result.json
会抛出错误,但这肯定会更清楚!
http://falcon.readthedocs.io/en/stable/api/testing.html#falcon.testing.Result.json
答案 2 :(得分:0)
第一步是在标题中设置{“ Content-Type”:“ application / json”}。
此外,您可以如下调整发送有效载荷的方式:
import 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)
然后,这应该可以解决问题。 让我知道。