示例#1
0
    def test_files_raw(self):
        def test_callback(data,
                          response,
                          progress=[0],
                          is_response_streamed=None):
            self.assertTrue(len(data) > 0)
            if not is_response_streamed:
                self.assertFalse(response._content_consumed)
                progress[0] += len(data)
                total = float(response.headers['Content-Length'])
                print("Downloading... {}%".format(
                    int(progress[0] * 100 / total)))
            self.assertIsNotNone(response)

        config = AutoRestSwaggerBATFileServiceConfiguration(
            base_url="http://localhost:3000")
        config.log_level = log_level
        client = AutoRestSwaggerBATFileService(config)

        file_length = 0
        with io.BytesIO() as file_handle:

            response = client.files.get_file(
                raw=True,
                callback=lambda x, response, progress=[0]: test_callback(
                    x, response, progress, False))
            stream = response.output

            for data in stream:
                file_length += len(data)
                file_handle.write(data)

            self.assertNotEqual(file_length, 0)

            sample_file = realpath(
                join(cwd, pardir, pardir, pardir, "NodeJS", "NodeJS.Tests",
                     "AcceptanceTests", "sample.png"))

            with open(sample_file, 'rb') as data:
                sample_data = hash(data.read())
            self.assertEqual(sample_data, hash(file_handle.getvalue()))

        file_length = 0
        with io.BytesIO() as file_handle:

            response = client.files.get_empty_file(
                raw=True,
                callback=lambda x, response, progress=[0]: test_callback(
                    x, response, progress, False))
            stream = response.output

            for data in stream:
                file_length += len(data)
                file_handle.write(data)

            self.assertEqual(file_length, 0)
示例#2
0
    def test_files(self):

        config = AutoRestSwaggerBATFileServiceConfiguration(
            base_url="http://localhost:3000")
        config.connection.data_block_size = 1000
        client = AutoRestSwaggerBATFileService(config)

        def test_callback(data, response, progress=[0]):
            self.assertTrue(len(data) > 0)
            self.assertIsNotNone(response)
            self.assertFalse(response._content_consumed)
            total = float(response.headers['Content-Length'])
            if total < 4096:
                progress[0] += len(data)
                print("Downloading... {}%".format(
                    int(progress[0] * 100 / total)))

        file_length = 0
        with io.BytesIO() as file_handle:
            stream = client.files.get_file(callback=test_callback)

            for data in stream:
                file_length += len(data)
                file_handle.write(data)

            self.assertNotEqual(file_length, 0)

            sample_file = realpath(
                join(cwd, pardir, pardir, pardir, "NodeJS", "NodeJS.Tests",
                     "AcceptanceTests", "sample.png"))

            with open(sample_file, 'rb') as data:
                sample_data = hash(data.read())
            self.assertEqual(sample_data, hash(file_handle.getvalue()))

        config.connection.data_block_size = 4096
        file_length = 0
        with io.BytesIO() as file_handle:
            stream = client.files.get_empty_file(callback=test_callback)

            for data in stream:
                file_length += len(data)
                file_handle.write(data)

            self.assertEqual(file_length, 0)

        def add_headers(adapter, request, response, *args, **kwargs):
            response.headers['Content-Length'] = str(3000 * 1024 * 1024)

        file_length = 0
        client._client.add_hook('response', add_headers)
        stream = client.files.get_file_large(callback=test_callback)