Ticket #3432: test_client_unicode.py

File test_client_unicode.py, 3.0 KB (added by Simon Willison, 16 years ago)

Test demonstrating the bug

Line 
1from django.test.client import Client
2import unittest
3import settings
4
5class TestPostGetPost(unittest.TestCase):
6
7    def echo_view(self, request):
8        # Django view, gets monkey-patched in to urls.py
9        from django.http import HttpResponse
10        response = HttpResponse()
11        response.write(request.method + '\n')
12        keys = request.POST.keys()
13        keys.sort()
14        response.write(','.join(keys))
15        return response
16
17    def setUp(self):
18        # Monkey-patch in our test view
19        urls = __import__(settings.ROOT_URLCONF, {}, {}, [''])
20        import copy
21        urls.oldpatterns = copy.copy(urls.urlpatterns)
22        urls.urlpatterns += urls.patterns('', 
23            ('^echomethod/$', self.echo_view)
24        )
25
26    def tearDown(self):
27        urls = __import__(settings.ROOT_URLCONF, {}, {}, [''])
28        urls.urlpatterns = urls.oldpatterns
29        del urls.oldpatterns
30
31    def test_get(self):
32        response = Client().get('/echomethod/')
33        self.assert_(response.content.startswith('GET'))
34
35    def test_post(self):
36        response = Client().post('/echomethod/', {'foo': 'bar'})
37        self.assertEqual(response.content, 'POST\nfoo')
38
39    def test_post_multiple_fields(self):
40        response = Client().post('/echomethod/', {'foo': 'bar', 'baz': '1'})
41        self.assertEqual(response.content, 'POST\nbaz,foo')
42
43    def test_get_post_get(self):
44        client = Client()
45       
46        response = client.get('/echomethod/')
47        self.assert_(response.content.startswith('GET'))
48       
49        response = client.post('/echomethod/', {'foo': 'bar', 'baz': '1'})
50        self.assertEqual(response.content, 'POST\nbaz,foo')
51       
52        response = client.get('/echomethod/')
53        self.assert_(response.content.startswith('GET'))
54
55    def test_post_get_post_not_unicode(self):
56        client = Client()
57        data = {
58            'firstname': 'Test',
59            'lastname': 'Example',
60            'email': 'test@example.com',
61        }
62        keys = data.keys()
63        keys.sort()
64
65        response = client.post('/echomethod/', data)
66        self.assertEqual(response.content, 'POST\n%s' % ','.join(keys))
67
68        response = client.get('/echomethod/')
69        self.assert_(response.content.startswith('GET'))
70
71        response = client.post('/echomethod/', data)
72        self.assertEqual(response.content, 'POST\n%s' % ','.join(keys))
73
74    def test_post_get_post_unicode(self):
75        client = Client()
76        data = {
77            u'firstname': u'Test',
78            u'lastname': u'Example',
79            u'email': u'test@example.com',
80        }
81        keys = data.keys()
82        keys.sort()
83       
84        response = client.post('/echomethod/', data)
85        self.assertEqual(response.content, 'POST\n%s' % ','.join(keys))
86
87        response = client.get('/echomethod/')
88        self.assert_(response.content.startswith('GET'))
89
90        response = client.post('/echomethod/', data)
91        self.assertEqual(response.content, 'POST\n%s' % ','.join(keys))
92
93if __name__ == '__main__':
94    unittest.main()
Back to Top