d.addCallback(_after_mkdir)
return d
+ def test_POST_mkdir_no_parentdir_redirect(self):
+ d = self.POST("/uri/?t=mkdir&redirect_to_result=true")
+ d.addBoth(self.shouldRedirect, None, statuscode='303')
+ return d
+
def test_POST_mkdir_replace(self): # return value?
d = self.POST(self.public_url + "/foo", t="mkdir", name="sub")
d.addCallback(lambda res: self._foo_node.get("sub"))
d.addCallback(self.failUnlessIsFooJSON)
return d
- def shouldRedirect(self, res, target):
+ def shouldRedirect(self, res, target=None, statuscode=None):
+ """ If target is not None then the redirection has to go to target. If
+ statuscode is not None then the redirection has to be accomplished with
+ that HTTP status code."""
if not isinstance(res, failure.Failure):
- self.fail("we were expecting to get redirected to %s, not get an"
- " actual page: %s" % (target, res))
+ self.fail("we were expecting to get redirected %s, not get an"
+ " actual page: %s" % ((target is None) and "somewhere" or ("to " + target), res))
res.trap(error.PageRedirect)
- # the PageRedirect does not seem to capture the uri= query arg
- # properly, so we can't check for it.
- realtarget = self.webish_url + target
- self.failUnlessEqual(res.value.location, realtarget)
+ if statuscode is not None:
+ self.failUnlessEqual(res.value.status, statuscode)
+ if target is not None:
+ # the PageRedirect does not seem to capture the uri= query arg
+ # properly, so we can't check for it.
+ realtarget = self.webish_url + target
+ self.failUnlessEqual(res.value.location, realtarget)
def test_GET_URI_form(self):
base = "/uri?uri=%s" % self._bar_txt_uri
"/uri only accepts PUT and PUT?t=mkdir")
return d
- def test_PUT_NEWDIR_URI(self):
+ def test_PUT_mkdir(self):
d = self.PUT("/uri?t=mkdir", "")
def _check(uri):
n = self.s.create_node_from_uri(uri.strip())
"""Return True if t=upload&localdir= is allowed, giving anyone who
can talk to the webserver control over the local (disk) filesystem."""
+def boolean_of_arg(arg):
+ assert arg.lower() in ("true", "t", "1", "false", "f", "0")
+ return arg.lower() in ("true", "t", "1")
# we must override twisted.web.http.Request.requestReceived with a version
# that doesn't use cgi.parse_multipart() . Since we actually use Nevow, we
when_done = req.fields["when_done"].value
if "replace" in req.fields:
- if req.fields["replace"].value.lower() in ("false", "0"):
+ if not boolean_of_arg(req.fields["replace"].value):
self._replace = False
if t == "mkdir":
replace = True
if "replace" in req.args:
- if req.args["replace"][0].lower() in ("false", "0"):
+ if not boolean_of_arg(req.args["replace"][0]):
replace = False
if method == "GET":
# "PUT /uri?t=mkdir", to create an unlinked directory.
d = IClient(ctx).create_empty_dirnode()
d.addCallback(lambda dirnode: dirnode.get_uri())
+ # XXX add redirect_to_result
return d
req.setResponseCode(http.BAD_REQUEST)
if t == "mkdir":
# "PUT /uri?t=mkdir", to create an unlinked directory.
d = IClient(ctx).create_empty_dirnode()
- d.addCallback(lambda dirnode: dirnode.get_uri())
+ redirect = req.args.has_key("redirect_to_result") and boolean_of_arg(req.args["redirect_to_result"][0])
+ if redirect:
+ def _then_redir(res):
+ req.setResponseCode(303)
+ req.setHeader('location', res.get_uri())
+ req.finish()
+ return ''
+ d.addCallback(_then_redir)
+ else:
+ d.addCallback(lambda dirnode: dirnode.get_uri())
return d
req.setResponseCode(http.BAD_REQUEST)
return URIPUTHandler(), ()
elif req.method == "POST":
# "POST /uri?t=upload&file=newfile" to upload an unlinked
- # file
+ # file or "POST /uri?t=mkdir" to create a new directory
return URIPOSTHandler(), ()
if len(segments) < 2:
return rend.NotFound