Reputation: 1680
I'm trying to write some pytests for the Falcon framework and am following the instructions here. I want to simulate a POST request. However, I keep getting the following error
tests/test_app.py:29: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../../harvester-venv/lib/python2.7/site-packages/falcon/testing/client.py:170: in json return json.loads(self.text) /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py:338: in loads return _default_decoder.decode(s) /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py:366: in decode 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
Here is the Falcon test I am attempting to write. When I validate the JSON I put in the data
variable on JSONlint.com it shows me the data is valid so the issue doesn't seem to be with the formatting.
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
Any clue why this occurs?
Upvotes: 0
Views: 2385
Reputation: 86
The first step would be to set the {"Content-Type": "application/json"} in your header.
Also, the way you're sending your payload could be adjusted as below:
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)
Then, this should fix the issue. Let me know.
Upvotes: 1
Reputation: 1
It's the call to result.json
rather than your POSTed JSON that is triggering the validation error. Presumably the elasticsearch endpoint you're testing is sending back a non-JSON error document.
https://github.com/falconry/falcon/blob/master/falcon/testing/client.py#L153
The docs do say that result.json
will throw an error if the results aren't parseable as JSON but this could definitely be clearer!
http://falcon.readthedocs.io/en/stable/api/testing.html#falcon.testing.Result.json
Upvotes: 0
Reputation: 2089
Not familiar with Falcon. but taking a wild guess here:
in your fixture you just pass api class/function definition but you are not calling api()
or calling api.create()
. Try:
@pytest.fixture()
def client():
return testing.TestClient(api()) # or api.create() ?
Also error you posted points to the line 29 but the code snippet you posted only contains 27 lines.
Upvotes: 0