Esempio n. 1
0
 def test_constructor_defaults(self):
     upload = _upload.MultipartUpload(sync_test.MULTIPART_URL)
     assert upload.upload_url == sync_test.MULTIPART_URL
     assert upload._headers == {}
     assert upload._checksum_type is None
     assert not upload._finished
     _check_retry_strategy(upload)
Esempio n. 2
0
 def test_constructor_explicit(self):
     headers = {u"spin": u"doctors"}
     upload = _upload.MultipartUpload(sync_test.MULTIPART_URL,
                                      headers=headers,
                                      checksum="md5")
     assert upload.upload_url == sync_test.MULTIPART_URL
     assert upload._headers is headers
     assert upload._checksum_type == "md5"
     assert not upload._finished
     _check_retry_strategy(upload)
Esempio n. 3
0
    def _prepare_request_helper(
        self,
        mock_get_boundary,
        headers=None,
        checksum=None,
        expected_checksum=None,
        test_overwrite=False,
    ):
        upload = _upload.MultipartUpload(sync_test.MULTIPART_URL,
                                         headers=headers,
                                         checksum=checksum)
        data = b"Hi"
        if test_overwrite and checksum:
            # Deliberately set metadata that conflicts with the chosen checksum.
            # This should be fully overwritten by the calculated checksum, so
            # the output should not change even if this is set.
            if checksum == "md5":
                metadata = {u"md5Hash": u"ZZZZZZZZZZZZZZZZZZZZZZ=="}
            else:
                metadata = {u"crc32c": u"ZZZZZZ=="}
        else:
            # To simplify parsing the response, omit other test metadata if a
            # checksum is specified.
            metadata = {u"Some": u"Stuff"} if not checksum else {}
        content_type = sync_test.BASIC_CONTENT
        method, url, payload, new_headers = upload._prepare_request(
            data, metadata, content_type)

        assert method == u"POST"
        assert url == sync_test.MULTIPART_URL

        preamble = b"--==3==\r\n" + sync_test.JSON_TYPE_LINE + b"\r\n"

        if checksum == "md5" and expected_checksum:
            metadata_payload = '{{"md5Hash": "{}"}}\r\n'.format(
                expected_checksum).encode("utf8")
        elif checksum == "crc32c" and expected_checksum:
            metadata_payload = '{{"crc32c": "{}"}}\r\n'.format(
                expected_checksum).encode("utf8")
        else:
            metadata_payload = b'{"Some": "Stuff"}\r\n'
        remainder = (b"--==3==\r\n"
                     b"content-type: text/plain\r\n"
                     b"\r\n"
                     b"Hi\r\n"
                     b"--==3==--")
        expected_payload = preamble + metadata_payload + remainder

        assert payload == expected_payload
        multipart_type = b'multipart/related; boundary="==3=="'
        mock_get_boundary.assert_called_once_with()

        return new_headers, multipart_type
Esempio n. 4
0
    def test_transmit(self):
        upload = _upload.MultipartUpload(sync_test.MULTIPART_URL)
        with pytest.raises(NotImplementedError) as exc_info:
            upload.transmit(None, None, None, None)

        exc_info.match(u"virtual")
Esempio n. 5
0
 def test__prepare_request_non_bytes_data(self):
     data = u"Nope not bytes."
     upload = _upload.MultipartUpload(sync_test.MULTIPART_URL)
     with pytest.raises(TypeError):
         upload._prepare_request(data, {}, sync_test.BASIC_CONTENT)
Esempio n. 6
0
 def test__prepare_request_already_finished(self):
     upload = _upload.MultipartUpload(sync_test.MULTIPART_URL)
     upload._finished = True
     with pytest.raises(ValueError):
         upload._prepare_request(b"Hi", {}, sync_test.BASIC_CONTENT)