Commits

ramusus committed 4825b42

added right quote formatting for json in request and test it

  • Participants
  • Parent commits ad9744e

Comments (0)

Files changed (2)

 import random
 import time
 import urllib
+import re
 from hashlib import md5
 from functools import partial
 try:
             url = API_URL
             secure = False
         data = urllib.urlencode(params)
+
+        # replace single quotes with double quotes for nested dictionaries.
+        # because right JSON format is necessary by php server-side parser of API requests
+        data = re.sub(r'%27(.?)%27(%2C|%7D)', r'%22\1%22\2', data)
+        data = re.sub(r'%27(.+?)%27%3A', r'%22\1%22%3A', data)
         headers = {"Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}
 
         # urllib2 doesn't support timeouts for python 2.5 so

vkontakte/tests.py

 
 import os
 import sys
+import urllib
 sys.path.insert(0, os.path.abspath('..'))
 
 import unittest
     def test_api_creation_error(self):
         self.assertRaises(ValueError, lambda: vkontakte.API())
 
-
 class SignatureTest(unittest.TestCase):
     def test_signature_supports_unicode(self):
         params = {'foo': u'клен'}
             '560b3f1e09ff65167b8dc211604fed2b'
         )
 
-
 class VkontakteMagicTest(unittest.TestCase):
 
     def setUp(self):
         self.assertEqual(res, 'foo')
         _get.assert_called_once_with('friends.get', uid=642177)
 
-
     @mock.patch('vkontakte.http.post')
     def test_urlencode_bug(self, post):
         post.return_value = 200, '{"response":123}'
         res = self.api.search(q=u'клен')
         self.assertEqual(res, 123)
 
-
+    @mock.patch('vkontakte.http.post')
+    def test_valid_quoted_json(self, post):
+        post.return_value = 200, '{"response": 123}'
+        self.api.ads.getStat(data={'type': '1', 'id': 1})
+        self.assertTrue('data={"type":+"1",+"id":+1}' in urllib.unquote(post.call_args[0][1]))
 
 if __name__ == '__main__':
     unittest.main()