11""" API Tests """
2+ from __future__ import unicode_literals
3+
24import functools
35import logging
46import pdb
1416import six
1517import wordpress
1618from httmock import HTTMock , all_requests , urlmatch
17- from six import text_type , u
19+ from six import text_type
20+ from six .moves .urllib .parse import parse_qsl , urlparse
1821from wordpress import __default_api__ , __default_api_version__ , auth
1922from wordpress .api import API
2023from wordpress .auth import Auth , OAuth
2124from wordpress .helpers import SeqUtils , StrUtils , UrlUtils
2225from wordpress .transport import API_Requests_Wrapper
2326
24- try :
25- from urllib .parse import (
26- urlencode , quote , unquote , parse_qs , parse_qsl , urlparse , urlunparse
27- )
28- from urllib .parse import ParseResult as URLParseResult
29- except ImportError :
30- from urllib import urlencode , quote , unquote
31- from urlparse import parse_qs , parse_qsl , urlparse , urlunparse
32- from urlparse import ParseResult as URLParseResult
33-
3427
3528def debug_on (* exceptions ):
3629 if not exceptions :
@@ -55,6 +48,7 @@ def wrapper(*args, **kwargs):
5548
5649CURRENT_TIMESTAMP = int (time ())
5750SHITTY_NONCE = ""
51+ DEFAULT_ENCODING = sys .getdefaultencoding ()
5852
5953
6054class WordpressTestCase (unittest .TestCase ):
@@ -1007,47 +1001,71 @@ def test_APIPutWithSimpleQuery(self):
10071001
10081002 wcapi .put ('products/%s' % (product_id ), {"name" : original_title })
10091003
1004+ @unittest .skipIf (six .PY2 , "non-utf8 bytes not supported in python2" )
1005+ def test_APIPostWithBytesQuery (self ):
1006+ wcapi = API (** self .api_params )
1007+ nonce = b"%f\xff " % random .random ()
1008+
1009+ data = {
1010+ "name" : nonce ,
1011+ "type" : "simple" ,
1012+ }
1013+
1014+ response = wcapi .post ('products' , data )
1015+ response_obj = response .json ()
1016+ product_id = response_obj .get ('id' )
1017+
1018+ expected = StrUtils .to_text (nonce , encoding = 'ascii' , errors = 'replace' )
1019+
1020+ self .assertEqual (
1021+ response_obj .get ('name' ),
1022+ expected ,
1023+ )
1024+ wcapi .delete ('products/%s' % product_id )
1025+
1026+ @unittest .skipIf (six .PY2 , "non-utf8 bytes not supported in python2" )
10101027 def test_APIPostWithLatin1Query (self ):
10111028 wcapi = API (** self .api_params )
1012- nonce = u "%f\u00ae " % random .random ()
1029+ nonce = "%f\u00ae " % random .random ()
10131030
10141031 data = {
10151032 "name" : nonce .encode ('latin-1' ),
10161033 "type" : "simple" ,
10171034 }
10181035
1019- if six .PY2 :
1020- response = wcapi .post ('products' , data )
1021- response_obj = response .json ()
1022- product_id = response_obj .get ('id' )
1023- self .assertEqual (response_obj .get ('name' ), nonce )
1024- wcapi .delete ('products/%s' % product_id )
1025- return
1026- with self .assertRaises (TypeError ):
1027- response = wcapi .post ('products' , data )
1036+ response = wcapi .post ('products' , data )
1037+ response_obj = response .json ()
1038+ product_id = response_obj .get ('id' )
1039+
1040+ expected = StrUtils .to_text (
1041+ StrUtils .to_binary (nonce , encoding = 'latin-1' ),
1042+ encoding = 'ascii' , errors = 'replace'
1043+ )
1044+
1045+ self .assertEqual (
1046+ response_obj .get ('name' ),
1047+ expected
1048+ )
1049+ wcapi .delete ('products/%s' % product_id )
10281050
10291051 def test_APIPostWithUTF8Query (self ):
10301052 wcapi = API (** self .api_params )
1031- nonce = u "%f\u00ae " % random .random ()
1053+ nonce = "%f\u00ae " % random .random ()
10321054
10331055 data = {
10341056 "name" : nonce .encode ('utf8' ),
10351057 "type" : "simple" ,
10361058 }
10371059
1038- if six .PY2 :
1039- response = wcapi .post ('products' , data )
1040- response_obj = response .json ()
1041- product_id = response_obj .get ('id' )
1042- self .assertEqual (response_obj .get ('name' ), nonce )
1043- wcapi .delete ('products/%s' % product_id )
1044- return
1045- with self .assertRaises (TypeError ):
1046- response = wcapi .post ('products' , data )
1060+ response = wcapi .post ('products' , data )
1061+ response_obj = response .json ()
1062+ product_id = response_obj .get ('id' )
1063+ self .assertEqual (response_obj .get ('name' ), nonce )
1064+ wcapi .delete ('products/%s' % product_id )
10471065
10481066 def test_APIPostWithUnicodeQuery (self ):
10491067 wcapi = API (** self .api_params )
1050- nonce = u "%f\u00ae " % random .random ()
1068+ nonce = "%f\u00ae " % random .random ()
10511069
10521070 data = {
10531071 "name" : nonce ,
@@ -1100,7 +1118,7 @@ def test_APIGetWithSimpleQuery(self):
11001118 self .assertEqual (len (response_obj ), 2 )
11011119
11021120 def test_APIPostData (self ):
1103- nonce = u "%f\u00ae " % random .random ()
1121+ nonce = "%f\u00ae " % random .random ()
11041122
11051123 content = "api test post"
11061124
@@ -1120,7 +1138,7 @@ def test_APIPostBadData(self):
11201138 """
11211139 No excerpt so should fail to be created.
11221140 """
1123- nonce = u "%f\u00ae " % random .random ()
1141+ nonce = "%f\u00ae " % random .random ()
11241142
11251143 data = {
11261144 'a' : nonce
0 commit comments