31
31
import urllib
32
32
import urlparse
33
33
from types import ListType
34
-
34
+ import mox
35
+ import httplib2
35
36
36
37
# Fix for python2.5 compatibility
37
38
try :
@@ -737,6 +738,7 @@ class TestClient(unittest.TestCase):
737
738
host = 'http://oauth-sandbox.sevengoslings.net'
738
739
739
740
def setUp (self ):
741
+ self .mox = mox .Mox ()
740
742
self .consumer = oauth .Consumer (key = self .consumer_key ,
741
743
secret = self .consumer_secret )
742
744
@@ -747,13 +749,31 @@ def setUp(self):
747
749
'blah' : 599999
748
750
}
749
751
752
+ def tearDown (self ):
753
+ self .mox .UnsetStubs ()
754
+
750
755
def _uri (self , type ):
751
756
uri = self .oauth_uris .get (type )
752
757
if uri is None :
753
758
raise KeyError ("%s is not a valid OAuth URI type." % type )
754
759
755
760
return "%s%s" % (self .host , uri )
756
761
762
+ def create_simple_multipart_data (self , data ):
763
+ boundary = '---Boundary-%d' % random .randint (1 ,1000 )
764
+ crlf = '\r \n '
765
+ items = []
766
+ for key , value in data .iteritems ():
767
+ items += [
768
+ '--' + boundary ,
769
+ 'Content-Disposition: form-data; name="%s"' % str (key ),
770
+ '' ,
771
+ str (value ),
772
+ ]
773
+ items += ['' , '--' + boundary + '--' , '' ]
774
+ content_type = 'multipart/form-data; boundary=%s' % boundary
775
+ return content_type , crlf .join (items )
776
+
757
777
def test_access_token_get (self ):
758
778
"""Test getting an access token via GET."""
759
779
client = oauth .Client (self .consumer , None )
@@ -789,6 +809,32 @@ def test_two_legged_get(self):
789
809
resp , content = self ._two_legged ("GET" )
790
810
self .assertEquals (int (resp ['status' ]), 200 )
791
811
812
+ def test_multipart_post_does_not_alter_body (self ):
813
+ self .mox .StubOutWithMock (httplib2 .Http , 'request' )
814
+ random_result = random .randint (1 ,100 )
815
+
816
+ data = {
817
+ 'rand-%d' % random .randint (1 ,100 ):random .randint (1 ,100 ),
818
+ }
819
+ content_type , body = self .create_simple_multipart_data (data )
820
+
821
+ client = oauth .Client (self .consumer , None )
822
+ uri = self ._uri ('two_legged' )
823
+
824
+ expected_kwargs = {
825
+ 'method' :'POST' ,
826
+ 'body' :body ,
827
+ 'redirections' :httplib2 .DEFAULT_MAX_REDIRECTS ,
828
+ 'connection_type' :None ,
829
+ 'headers' :mox .IsA (dict ),
830
+ }
831
+ httplib2 .Http .request (client , uri , ** expected_kwargs ).AndReturn (random_result )
832
+
833
+ self .mox .ReplayAll ()
834
+ result = client .request (uri , 'POST' , headers = {'Content-Type' :content_type }, body = body )
835
+ self .assertEqual (result , random_result )
836
+ self .mox .VerifyAll ()
837
+
792
838
if __name__ == "__main__" :
793
839
unittest .main ()
794
840
0 commit comments