add test_put and refactor a little

This commit is contained in:
meejah 2019-07-30 15:01:37 -06:00
parent 1af4593e16
commit 7738f9f4e3
3 changed files with 48 additions and 14 deletions

View File

@ -49,3 +49,26 @@ def test_upload_download(alice):
}
)
assert data == FILE_CONTENTS
def test_put(alice):
"""
use PUT to create a file
"""
import time; time.sleep(10) # XXX wat
FILE_CONTENTS = "added via PUT"
import requests
resp = requests.put(
util.node_url(alice._node_dir, "uri"),
files={
"file": FILE_CONTENTS,
},
)
print(resp)
print(resp.status_code)
print(resp.text)
print("\n\n\n\n\n\n\n")
assert resp.text.strip().startswith("URI:CHK:")
assert resp.text.strip().endswith(":2:4:153")

View File

@ -360,15 +360,28 @@ def cli(request, reactor, node_dir, *argv):
return proto.done
def node_url(node_dir, uri_fragment):
"""
Create a fully qualified URL by reading config from `node_dir` and
adding the `uri_fragment`
"""
with open(join(node_dir, "node.url"), "r") as f:
base = f.read().strip()
url = base + uri_fragment
return url
def web_get(node_dir, uri_fragment, **kw):
"""
Make a web-request to the webport of `node_dir`. This will look
like: `http://localhost:<webport>/<uri_fragment>`
"""
with open(join(node_dir, "node.url"), "r") as f:
base = f.read().strip()
url = base + uri_fragment
url = node_url(node_dir, uri_fragment)
resp = requests.get(url, **kw)
if resp.status_code < 200 or resp.status_code >= 300:
raise RuntimeError(
"Expected a 200 code, got {}".format(resp.status_code)
)
return resp.content
@ -377,10 +390,7 @@ def web_post(node_dir, uri_fragment, **kw):
Make a web-request to the webport of `node_dir`. This will look
like: `http://localhost:<webport>/<uri_fragment>`
"""
# XXXX same as above except requests.post
with open(join(node_dir, "node.url"), "r") as f:
base = f.read().strip()
url = base + uri_fragment
url = node_url(node_dir, uri_fragment)
resp = requests.post(url, **kw)
if resp.status_code < 200 or resp.status_code >= 300:
raise RuntimeError(

View File

@ -532,13 +532,14 @@ class CLI(CLITestMixin, unittest.TestCase):
# it's safe to drop it on the floor.
del d
patcher = MonkeyPatcher((runner, 'parse_or_exit_with_explanation',
call_parse_or_exit),
(sys, 'argv', ["tahoe"]),
(sys, 'exit', call_sys_exit),
(sys, 'stderr', stderr),
(task, 'react', fake_react),
)
patcher = MonkeyPatcher(
(runner, 'parse_or_exit_with_explanation', call_parse_or_exit),
(runner, "_setup_coverage", lambda r: None),
(sys, 'argv', ["tahoe"]),
(sys, 'exit', call_sys_exit),
(sys, 'stderr', stderr),
(task, 'react', fake_react),
)
patcher.runWithPatches(runner.run)
self.failUnless(ns.parse_called)