WebTest *_json methods body encoding

19 views
Skip to first unread message

Igor Ippolitov

unread,
Dec 5, 2016, 10:29:50 AM12/5/16
to pylons-discuss
Hello everyone.

I'm using WebTest-2.0.23 and python 2.7 and noticed a difference in *_json (e.g. post_json) methods and real application application requests.

There is a WSGI application which accepts some data from a POST request. Data is a json encoded object. One of it's fields contains unicode string.
Each request is logged using default logger (like 'log.info(request)')

To test the application with an external tool, I use curl:
curl -H 'Content-Type: application/json' -d '{1:1, 2:1, 3:"擭䋰몙迋탺괶", 4:"test"}' http://localhost/post_test
In this case in logs I receive exact string posted.
The same result is observed is a request comes from any other application:  a real browser, wget, scripts.

To test the application using webtest I use a snippet like this ('app' is a configured webtest application):
app.post_json('/post_test', {1:1, 2:1, 3:"擭䋰몙迋탺괶", 4:"test"})
I assume this should result in the same request being formed for an application.
Yet, I end up with the third parameter logged as a sequence of '\uXXXX' escapes (like "\u7eea\u7c0e\u3ca5\u17e8\ub35c\uced2\u5a9b\u5da0")

If I change json_method in webtest/utils.py so that json.dumps is called with 'ensure_ascii=False' in the 'wrapper' function I get similar behaviour for WebTest and real applications.
This is somehow important because this results in some cases not being tested with webtest. For example, in case of careless cast to a string instead of a unicode type real application test causes an exception while webtest doesn't.

Is this an intended behaviour? Should I use 'app.post' and encode json on my own or do I provide a patch for this? Is there any other workaround?

Best regards,
Ippolitov igor.
Reply all
Reply to author
Forward
0 new messages