Skip to content

Commit

Permalink
Encode string request body as utf-8 (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
gtopper authored Aug 25, 2024
1 parent 07ff984 commit 11d3dde
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
21 changes: 9 additions & 12 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,37 +215,34 @@ def setUp(self):
self._delete_dir(self._object_dir)

def test_object(self):
contents = "vegans are better than everyone"
body = "this unicode character Ě triggers ML-7498"
body_bytes = body.encode("utf-8")

response = self._client.object.get(
container=self._container, path=self._object_path, raise_for_status=v3io.dataplane.RaiseForStatus.never
)

self.assertEqual(404, response.status_code)

# put contents to some object
self._client.object.put(container=self._container, path=self._object_path, body=contents)
# put body to some object
self._client.object.put(container=self._container, path=self._object_path, body=body)

# get the object contents
# get the object body
response = self._client.object.get(container=self._container, path=self._object_path)
if not isinstance(response.body, str):
response.body = response.body.decode("utf-8")
self.assertEqual(response.body, contents)
self.assertEqual(response.body, body_bytes)

response = self._client.object.get(container=self._container, path=self._object_path, offset=0, num_bytes=10)
if not isinstance(response.body, str):
response.body = response.body.decode("utf-8")
self.assertEqual(response.body, contents[0:10])
self.assertEqual(response.body, body_bytes[0:10])

# get the head of the object
response = self._client.object.head(container=self._container, path=self._object_path)

self.assertIn(("Content-Length", str(len(contents))), response.headers.items())
self.assertIn(("Content-Length", str(len(body_bytes))), response.headers.items())

# get the head of the dir-object
response = self._client.object.head(container=self._container, path=self._object_dir)

self.assertIn(("Content-Length", str(0)), response.headers.items())
self.assertIn(("Content-Length", "0"), response.headers.items())

# delete the object
self._client.object.delete(container=self._container, path=self._object_path)
Expand Down
10 changes: 8 additions & 2 deletions v3io/dataplane/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,19 @@ def encode_get_object(container_name, access_key, kwargs):


def encode_put_object(container_name, access_key, kwargs):
headers = None
headers = {
"Content-Type": "application-octet-stream",
}

# if the append flag is passed, add a range header
if kwargs["append"]:
headers = {"Range": "-1"}

return _encode("PUT", container_name, access_key, kwargs["path"], None, headers, kwargs["body"])
body = kwargs["body"]
if isinstance(body, str):
body = body.encode("utf-8")

return _encode("PUT", container_name, access_key, kwargs["path"], None, headers, body)


def encode_delete_object(container_name, access_key, kwargs):
Expand Down

0 comments on commit 11d3dde

Please sign in to comment.