import unittest
import urlfetch
import json
import random

import testlib

class PostTest(unittest.TestCase):

    def test_fetch(self):
        d = testlib.randdict()
        data = urlfetch.urlencode(d)

        r = urlfetch.fetch('http://127.0.0.1:8800/', data=data)
        o = json.loads(r.body)

        self.assertEqual(r.status, 200)
        self.assertEqual(o['method'], 'POST')
        self.assertEqual(o['post'], d)

    def test_post(self):
        d = testlib.randdict()
        data = urlfetch.urlencode(d)

        r = urlfetch.post('http://127.0.0.1:8800/', data=data)
        o = json.loads(r.body)

        self.assertEqual(r.status, 200)
        self.assertEqual(o['method'], 'POST')
        self.assertEqual(o['post'], d)

    def test_post_query_string(self):
        d = testlib.randdict()
        data = urlfetch.urlencode(d)

        qs = testlib.randdict(5)
        query_string = urlfetch.urlencode(qs)

        r = urlfetch.post('http://127.0.0.1:8800/?'+query_string, data=data)
        o = json.loads(r.body)

        self.assertEqual(r.status, 200)
        self.assertEqual(o['method'], 'POST')
        self.assertEqual(o['post'], d)
        self.assertEqual(o['get'], qs)
        self.assertEqual(o['query_string'], query_string)

    def test_file_upload(self):
        content = open('test.file', 'rb').read()
        files = {}
        files['test.file1'] = ('test.file1', '', content)
        files['test.file2'] = ('test.file2', '', content)
        files['test.file3'] = ('test.file3', 'wangxiaobo', content)
        files['test.file4'] = ('test.file4', 'wangtwo', content)

        r = urlfetch.post(
                'http://127.0.0.1:8800/',
                files = {
                    'test.file1' : open('test.file'),
                    'test.file2' : content,
                    'test.file3' : ('wangxiaobo', open('test.file')),
                    'test.file4' : ('wangtwo', content)
                },
            )
        o = json.loads(r.body)

        self.assertEqual(r.status, 200)
        self.assertEqual(o['method'], 'POST')
        self.assertItemsEqual(o['files'].keys(), files.keys())
        for i in files:
            self.assertEqual(o['files'][i], files[i])
        
    def test_one_file_upload(self):
        content = open('test.file', 'rb').read()
        files = {'test.file': ('test.file', '', content)}

        r = urlfetch.post(
                'http://127.0.0.1:8800/',
                files = {
                    'test.file' : content,
                },
            )
        o = json.loads(r.body)

        self.assertEqual(r.status, 200)
        self.assertEqual(o['method'], 'POST')
        self.assertItemsEqual(o['files'].keys(), files.keys())
        for i in files:
            self.assertEqual(o['files'][i], files[i])

if __name__ == '__main__':
    unittest.main()

@@ -1397,24 +1397,33 @@
 st.file1', '
+test.file
 ', content)%0A
@@ -1422,32 +1422,33 @@
 ontent)%0A        
+#
 files%5B'test.file
@@ -1776,32 +1776,33 @@
                 
+#
 'test.file2' : c
@@ -2165,32 +2165,68 @@
 for i in files:%0A
+            for j in xrange(3):%0A    
             self
@@ -2243,32 +2243,35 @@
 al(o%5B'files'%5D%5Bi%5D
+%5Bj%5D
 , files%5Bi%5D)%0A    
@@ -2264,26 +2264,38 @@
 files%5Bi%5D
-)%0A        
+%5Bj%5D.decode('utf-8'))%0A%0A
 %0A    def
@@ -2413,24 +2413,33 @@
 est.file', '
+test.file
 ', content)%7D
@@ -2568,24 +2568,38 @@
 .file' :
+ ('test.file',
  content
 ,%0A      
@@ -2582,32 +2582,33 @@
 t.file', content
+)
 ,%0A              
@@ -2823,32 +2823,68 @@
 for i in files:%0A
+            for j in xrange(3):%0A    
             self
@@ -2909,16 +2909,19 @@
 les'%5D%5Bi%5D
+%5Bj%5D
 , files%5B
@@ -2922,16 +2922,35 @@
 files%5Bi%5D
+%5Bj%5D.decode('utf-8')
 )%0A%0Aif __

fix test_post