Skip to content

Commit ed9b615

Browse files
committed
api module should PUT to server with Content-Length HTTP header
Signed-off-by: Justin Cinkelj <justin.cinkelj@xlab.si>
1 parent 6307122 commit ed9b615

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

plugins/modules/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180

181181

182182
from ansible.module_utils.basic import AnsibleModule
183+
import os
183184

184185
from ..module_utils import errors, arguments
185186
from ..module_utils.client import Client
@@ -260,6 +261,7 @@ def delete_record(module, rest_client):
260261

261262

262263
def put_record(module, rest_client):
264+
file_size = os.stat(module.params["source"]).st_size
263265
with open(module.params["source"], "rb") as source_file:
264266
result = rest_client.put_record(
265267
endpoint=module.params["endpoint"],
@@ -271,6 +273,7 @@ def put_record(module, rest_client):
271273
headers={
272274
"Content-Type": "application/octet-stream",
273275
"Accept": "application/json",
276+
"Content-Length": file_size,
274277
},
275278
)
276279
return True, result

tests/unit/plugins/modules/test_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ def test_put_method(self, create_module, rest_client, mocker):
9797
data=dict(),
9898
)
9999
)
100-
mocker.patch("builtins.open", mocker.mock_open(read_data="this-data"))
100+
this_data = "this-data"
101+
mocker.patch("builtins.open", mocker.mock_open(read_data=this_data))
102+
mocker.patch("os.stat").return_value = mocker.Mock(st_size=len(this_data))
101103
rest_client.put_record.return_value = "this-value"
102104
result = api.put_record(module, rest_client)
103105
assert result == (True, "this-value")

0 commit comments

Comments
 (0)