Beispiel #1
0
    def test_custom_setting(self):
        test_file_dir = "tests/settings/test_file_dir"
        config = {
            Settings.Key.FILE_UPLOAD_DIR: test_file_dir,
            Settings.Key.FILE_UPLOAD_MAX_MEMORY: 10 * ((2**10) * (2**10))
        }

        custom_setting = Settings(config)

        self.assertTrue(hasattr(custom_setting, "FILE_UPLOAD_TEMP_DIR"))
        self.assertTrue(hasattr(custom_setting, "MAX_HEADER_SIZE"))
        self.assertTrue(hasattr(custom_setting, "FILE_UPLOAD_MAX_MEMORY_SIZE"))
        self.assertTrue(hasattr(custom_setting, "DATA_UPLOAD_MAX_MEMORY_SIZE"))
        self.assertTrue(
            hasattr(custom_setting, "DATA_UPLOAD_MAX_NUMBER_FIELDS"))
        self.assertTrue(hasattr(custom_setting, "DEFAULT_CHARSET"))

        #confirm the values
        self.assertIn('test_file_dir', custom_setting.FILE_UPLOAD_TEMP_DIR)
        self.assertEqual(16, custom_setting.MAX_HEADER_SIZE)
        self.assertEqual(10 * ((2**10) * (2**10)),
                         custom_setting.FILE_UPLOAD_MAX_MEMORY_SIZE)
        self.assertEqual(5 * ((2**10) * (2**10)),
                         custom_setting.DATA_UPLOAD_MAX_MEMORY_SIZE)
        self.assertEqual(4096, custom_setting.DATA_UPLOAD_MAX_NUMBER_FIELDS)
        self.assertEqual('ISO-8859-1', custom_setting.DEFAULT_CHARSET)

        test_file_dir = get_abs_path(test_file_dir)
        rmdir(test_file_dir)
def requestparser():
    curr_dir = "tests/request parse test files/"
    get_request1 = "get-request1.txt"
    get_request1 = curr_dir + get_request1
    get_request1 = get_abs_path(get_request1)

    stream1 = ''

    with open(get_request1, 'r') as stream1:
        try:
            http_get_request1 = HttpRequest(stream1)
            http_get_request1.parse_request_header()
            print "Method: " + http_get_request1.method
            print "Scheme is: " + http_get_request1.scheme
            print "Path is: " + http_get_request1.path
            print "Protocol info: " + http_get_request1.protocol_info
            print "Host is: " + http_get_request1.get_host()
            print "Port is: " + http_get_request1.get_port()
            print "Encoding is: " + http_get_request1.encoding
            print "Content-Type is: " + http_get_request1.content_type
            print "Request URI is: " + http_get_request1.get_uri(raw=True)
            print "GET query string dict is: "
            print http_get_request1.GET
            print "Cookies are: "
            print http_get_request1.META[MetaDict.Info.REQ_HEADERS].getlist(
                "Cookies")
            print "Request headers are: "
            print http_get_request1.META['REQUEST_HEADERS']
            print "Request is: "
            print http_get_request1

        except Exception as e:
            print "Exception is: {}".format(e)
Beispiel #3
0
def test1():
    settings_upload_handlers = [
        #hold uploaded files in-memory
        'request_parser.files.uploadhandler.MemoryFileUploadHandler',
        #hold uploaded files in a temp file
        'request_parser.files.uploadhandler.TemporaryFileUploadHandler',
    ]

    #Set the _upload_handlers to an array of upload handlers loaded from
    #settings.FILE_UPLOAD_HANDLERS
    upload_handlers = [
        uploadhandler.load_handler(handler)
        for handler in settings_upload_handlers
    ]

    META = {
        'Content-Length':
        554,
        'Content-Type':
        'multipart/form-data; boundary=-------------------------9051914041544843365972754266'
    }

    test_dir = "tests/multipart test files/"
    test_file1 = test_dir + "mp-test1.txt"
    test_file1 = utils.get_abs_path(test_file1)
    stream1 = ''

    with open(test_file1, 'r') as stream1:
        try:
            multipartparser_1 = MultiPartParser(META, stream1, upload_handlers,
                                                Settings.default())
            post, files = multipartparser_1.parse()
            print "Done parsing!"
        except Exception as e:
            print "Exception is: {}".format(e)
Beispiel #4
0
    def setUpClass(cls):
        cls.maxDiff = None

        test_files_dir = "tests/request parse test files"
        test_files_dir = get_abs_path(test_files_dir)

        #GET request
        get_request_with_query_file = "get-request-with-query-string.txt"
        cls.get_request_with_query = join(test_files_dir,
                                          get_request_with_query_file)

        #POST request
        post_request_with_query_file = "post-request-with-query.txt"
        cls.post_request_with_query_file = join(test_files_dir,
                                                post_request_with_query_file)

        #PUT request with multipart-form-data
        put_request_multipart_file = "complex-request1.txt"
        cls.put_request_multipart_file = join(test_files_dir,
                                              put_request_multipart_file)
Beispiel #5
0
def requestparser():
    curr_dir = "request parse test files"
    curr_dir = get_abs_path(curr_dir)

    put_multipart_request = "complex-request1.txt"
    put_multipart_request = curr_dir + put_multipart_request
    stream = ''

    with open(put_multipart_request, 'r') as stream:
        try:
            http_get_request1 = HttpRequest(stream)
            #http_get_request1.parse_request_header()
            body = http_get_request1.parse()
            print "Method: " + http_get_request1.method
            print "Scheme is: " + http_get_request1.scheme
            print "Path is: " + http_get_request1.path
            print "Protocol info: " + http_get_request1.protocol_info
            print "Host is: " + http_get_request1.get_host()
            print "Port is: " + http_get_request1.get_port()
            print "Encoding is: " + http_get_request1.encoding
            print "Content-Type is: " + http_get_request1.content_type
            print "Request URI is: " + http_get_request1.get_uri()
            print "GET query string dict is: "
            print http_get_request1.GET
            print "Cookies are: "
            print http_get_request1.META[MetaDict.Info.REQ_HEADERS].getlist(
                "Cookies")
            print "Request headers are: "
            print http_get_request1.META['REQUEST_HEADERS']
            print "Request is: "
            print http_get_request1
            files = http_get_request1.FILES
            print_files_details(files)
            #print "Body is: "+body

        except Exception as e:
            print "Exception is: {}".format(e)
Beispiel #6
0
    def _check_upload_dir(self, check_presence=False):
        """
        Method that canonicalizes the FILE_UPLOAD_DIR if required and checks for permission issues.
        """
        file_upload_dir = self.FILE_UPLOAD_TEMP_DIR
        file_upload_dir = normpath(file_upload_dir)

        #if the provided directory path is not present(already) or
        #if it's an absolute path
        if not isabs(file_upload_dir) or not isdir(file_upload_dir):
            #try to get a directory path with
            #FILE_UPLOAD_TEMP_DIR in the path
            file_upload_dir = get_abs_path(file_upload_dir)

        if check_presence:
            #check permission for creating directory and writing files
            try:
                #check if directory exists if not create
                if not isdir(file_upload_dir):
                    mkdir(file_upload_dir)

                test_file_path = join(file_upload_dir, "acdr423x.tmp")
                test_file = open(test_file_path, "w+")
                test_file.write("Permissions Check!")
                test_file.close()
                remove(test_file_path)
            except IOError as ioError:
                if ioError.errno == errno.EACCES:
                    raise InvalidDirectory(
                        "No write permissions to directory: {}".format(
                            file_upload_dir))
                else:
                    raise InvalidDirectory(
                        "Error: Invalid directory: {}".format(file_upload_dir))

        return file_upload_dir
    def setUpClass(cls):
        cls.FILE_UPLOAD_HANDLERS = [
            #hold uploaded files in-memory
            'request_parser.files.uploadhandler.MemoryFileUploadHandler',
            #hold uploaded files in a temp file
            'request_parser.files.uploadhandler.TemporaryFileUploadHandler',
            #convenient upload handler
            'request_parser.files.uploadhandler.ConvenientFileUploadHandler'
        ]
        cls.upload_handlers = [uploadhandler.load_handler(handler) for  handler in cls.FILE_UPLOAD_HANDLERS]

        #get a request handle
        test_files_dir = "tests/multipart test files/"
        test_files_dir = get_abs_path(test_files_dir)

        #PUT request with multipart-form-data
        test_file = "kitten.jpg"
        cls.test_file_path = join(test_files_dir, test_file)

        #chunk size is 64KB
        cls.chunk_size = 64 * (2 ** 10)

        #file is 572562 bytes long
        cls.test_file_size = 572.562 * 1000
Beispiel #8
0
    def setUpClass(cls):
        test_files_dir = "tests/request parse test files"
        cls.test_files_dir = get_abs_path(test_files_dir)

        request_file = "complex-request1.txt"
        cls.request_file = join(cls.test_files_dir, request_file)
Beispiel #9
0
    def test_http_request_encoding_and_bodystream_metadata_reset(self):
        """
        This test covers the encoding set/reset and the body_stream reset
        cases.
        """
        #charset/encoding reset test
        encoded_body_dir = "tests/request parse test files/encoded body"
        encoded_body_dir = get_abs_path(encoded_body_dir)

        iso_88591_1_file = "ISO-8859-1-Barca.txt"
        utf8_file = "UTF8-Barca.txt"
        utf16_BEBOM_file = "UTF16 BEBOM-Barca.txt"

        iso_88591_1_file = join(encoded_body_dir, iso_88591_1_file)
        utf8_file = join(encoded_body_dir, utf8_file)
        utf16_BEBOM_file = join(encoded_body_dir, utf16_BEBOM_file)

        iso_88591_1_encoding = "ISO-8859-1"
        utf8_encoding = "UTF-8"
        utf16_BEBOM_encoding = "UTF-16"

        #an http_request
        request_stream = open(self.request_file, 'r')
        http_request = HttpRequest(request_stream)
        http_request.parse_request_header()

        #ISO-88591-1
        #reset content-type and encoding
        iso_88591_1_body = open(iso_88591_1_file, 'r')
        http_request.content_type = "text/plain"
        http_request.encoding = iso_88591_1_encoding.lower()
        #set the request_body stream
        http_request.body_stream = iso_88591_1_body
        http_request.parse_request_body()

        #check if the request body was properly decoded as ISO-8859-1
        _body_file = open(iso_88591_1_file, 'r')
        _body_bytes = _body_file.read()
        _body_bytes = _body_bytes.decode(iso_88591_1_encoding.lower())
        http_body = http_request.body()
        #the body should be in the encoding specified in the request
        self.assertEqual(_body_bytes, http_body)

        #UTF-16
        #reset content-type and encoding
        utf16_BEBOM_body = open(utf16_BEBOM_file, 'r')
        http_request.content_type = "text/plain"
        http_request.encoding = utf16_BEBOM_encoding.lower()
        #set the request_body stream
        http_request.body_stream = utf16_BEBOM_body
        http_request.parse_request_body()

        #check if the request body was properly decoded as ISO-8859-1
        _body_file = open(utf16_BEBOM_file, 'r')
        _body_bytes = _body_file.read()
        _body_bytes = _body_bytes.decode(utf16_BEBOM_encoding.lower())
        http_body = http_request.body()
        #the body should be in the encoding specified in the request
        self.assertEqual(_body_bytes, http_body)

        #UTF-8
        #reset content-type and encoding
        utf8_body = open(utf8_file, 'r')
        http_request.content_type = "text/plain"
        http_request.encoding = utf8_encoding.lower()
        #set the request_body stream
        http_request.body_stream = utf8_body
        http_request.parse_request_body()

        #check if the request body was properly decoded as ISO-8859-1
        _body_file = open(utf8_file, 'r')
        _body_bytes = _body_file.read()
        _body_bytes = _body_bytes.decode(utf8_encoding.lower())
        http_body = http_request.body()
        #the body should be in the encoding specified in the request
        self.assertEqual(_body_bytes, http_body)

        #close the file/stream
        request_stream.close()