~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to tests/regressiontests/test_client_regress/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2010-10-12 11:34:35 UTC
  • mfrom: (1.2.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 30.
  • Revision ID: james.westby@ubuntu.com-20101012113435-9lnsrh0i3mxozbt0
Tags: upstream-1.2.3
ImportĀ upstreamĀ versionĀ 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
from django.core.exceptions import SuspiciousOperation
12
12
from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
13
13
from django.template import loader
 
14
from django.test.client import encode_file
14
15
 
15
16
class AssertContainsTests(TestCase):
16
17
    def setUp(self):
821
822
        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
822
823
                                    content_type="application/json; charset=koi8-r")
823
824
        self.assertEqual(response.content, json.encode('koi8-r'))
 
825
 
 
826
class DummyFile(object):
 
827
    def __init__(self, filename):
 
828
        self.name = filename
 
829
    def read(self):
 
830
        return 'TEST_FILE_CONTENT'
 
831
 
 
832
class UploadedFileEncodingTest(TestCase):
 
833
    def test_file_encoding(self):
 
834
        encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', DummyFile('test_name.bin'))
 
835
        self.assertEqual('--TEST_BOUNDARY', encoded_file[0])
 
836
        self.assertEqual('Content-Disposition: form-data; name="TEST_KEY"; filename="test_name.bin"', encoded_file[1])
 
837
        self.assertEqual('TEST_FILE_CONTENT', encoded_file[-1])
 
838
 
 
839
    def test_guesses_content_type_on_file_encoding(self):
 
840
        self.assertEqual('Content-Type: application/octet-stream',
 
841
                         encode_file('IGNORE', 'IGNORE', DummyFile("file.bin"))[2])
 
842
        self.assertEqual('Content-Type: text/plain',
 
843
                         encode_file('IGNORE', 'IGNORE', DummyFile("file.txt"))[2])
 
844
        self.assertEqual('Content-Type: application/zip',
 
845
                         encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2])
 
846
        self.assertEqual('Content-Type: application/octet-stream',
 
847
                         encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2])
 
848
 
 
849
class RequestHeadersTest(TestCase):
 
850
    def test_client_headers(self):
 
851
        "A test client can receive custom headers"
 
852
        response = self.client.get("/test_client_regress/check_headers/", HTTP_X_ARG_CHECK='Testing 123')
 
853
        self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123")
 
854
        self.assertEquals(response.status_code, 200)
 
855
 
 
856
    def test_client_headers_redirect(self):
 
857
        "Test client headers are preserved through redirects"
 
858
        response = self.client.get("/test_client_regress/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123')
 
859
        self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123")
 
860
        self.assertRedirects(response, '/test_client_regress/check_headers/',
 
861
            status_code=301, target_status_code=200)