Example #1
0
    def test_upload_multiple_files(self):
        with open("./tests/fixtures/v1.png", "rb") as test_file:
            with requests_mock.Mocker() as mock:
                mock.post(
                    raw.get_full_url("data/new-file"),
                    text='{"id": "person-01", "first_name": "John"}',
                )

                def verify_file_callback(request):
                    body_file = io.BytesIO(request.body)
                    _, pdict = cgi.parse_header(
                        request.headers['Content-Type'])
                    if sys.version_info[0] == 3:
                        pdict["boundary"] = bytes(pdict["boundary"], "UTF-8")
                    else:
                        pdict["boundary"] = bytes(pdict["boundary"])
                    parsed = cgi.parse_multipart(fp=body_file, pdict=pdict)
                    assert "file" in parsed
                    assert "test" in parsed and parsed["test"]
                    file_data = test_file.read()
                    assert file_data == parsed["file"][0]
                    assert file_data == parsed["file-2"][0]
                    return None

                mock.post("data/new-file", json={})
                mock.add_matcher(verify_file_callback)
                raw.upload("data/new-file",
                           "./tests/fixtures/v1.png",
                           data={
                               "test": True,
                           },
                           extra_files=["./tests/fixtures/v1.png"])
Example #2
0
    def test_upload_multiple_files(self):
        with open("./tests/fixtures/v1.png", "rb") as test_file:
            with requests_mock.Mocker() as mock:
                mock_route(
                    mock,
                    "POST",
                    "data/new-file",
                    text={"id": "person-01", "first_name": "John"},
                )
                test_file_read = test_file.read()
                add_verify_file_callback(
                    mock,
                    {
                        "file": test_file_read,
                        "file-2": test_file_read,
                        "test": "True",
                    },
                )

                raw.upload(
                    "data/new-file",
                    "./tests/fixtures/v1.png",
                    data={
                        "test": True,
                    },
                    extra_files=["./tests/fixtures/v1.png"],
                )
Example #3
0
    def test_upload(self):
        with open("./tests/fixtures/v1.png", "rb") as test_file:
            with requests_mock.Mocker() as mock:
                mock_route(
                    mock,
                    "POST",
                    "data/new-file",
                    text={"id": "person-01", "first_name": "John"},
                )

                add_verify_file_callback(
                    mock, {"file": test_file.read(), "test": "True"}
                )

                raw.upload(
                    "data/new-file",
                    "./tests/fixtures/v1.png",
                    data={"test": True},
                )
            with requests_mock.Mocker() as mock:
                mock_route(
                    mock,
                    "POST",
                    "data/new-file",
                    text={"message": "Error"},
                )
                with self.assertRaises(gazu.client.UploadFailedException):
                    raw.upload(
                        "data/new-file",
                        "./tests/fixtures/v1.png",
                        data={"test": True},
                    )
Example #4
0
    def test_upload(self):
        with open("./tests/fixtures/v1.png", "rb") as test_file:
            with requests_mock.Mocker() as mock:
                mock.post(
                    raw.get_full_url("data/new-file"),
                    text=json.dumps({
                        "id": "person-01",
                        "first_name": "John"
                    }),
                )

                def verify_file_callback(request):
                    body_file = io.BytesIO(request.body)
                    _, pdict = cgi.parse_header(
                        request.headers["Content-Type"])
                    if sys.version_info[0] == 3:
                        pdict["boundary"] = bytes(pdict["boundary"], "UTF-8")
                    else:
                        pdict["boundary"] = bytes(pdict["boundary"])
                    parsed = cgi.parse_multipart(fp=body_file, pdict=pdict)
                    assert "file" in parsed
                    assert "test" in parsed and parsed["test"]
                    assert test_file.read() == parsed["file"][0]
                    return None

                mock.post("data/new-file", json={})
                mock.add_matcher(verify_file_callback)
                raw.upload(
                    "data/new-file",
                    "./tests/fixtures/v1.png",
                    data={"test": True},
                )
            with requests_mock.Mocker() as mock:
                mock.post(
                    raw.get_full_url("data/new-file"),
                    text=json.dumps({"message": "Error"}),
                )
                mock.post("data/new-file", json={})
                with self.assertRaises(gazu.client.UploadFailedException):
                    raw.upload(
                        "data/new-file",
                        "./tests/fixtures/v1.png",
                        data={"test": True},
                    )