def decode(self, response, request): log.msg("Building credentials") auth = self._parseAuth(response) try: creds = StoopidCredential(auth["name"], auth["secret"]) except KeyError, ke: raise LoginFailed("{0!r} not in authorization.".format(*ke.args))
def cb_assertFileNotEmpty(response, path): fileSize = os.stat(path).st_size log.msg("%s is %dB: %r" % (path, fileSize, open(path).read())) self.assertTrue(fileSize > 500, "Expected more than %d bytes" % fileSize) return response
def render_POST(self, request): contentType = request.getHeader("Content-type") if contentType == "application/json": content = json.load(request.content) resourceName = content["resource"] if resourceName not in self.parent.children: url = self.buildUrl(request, resourceName) log.msg("%r: Creating resource: %s" % (request, url)) resource = _GettableResource() self.parent.putChild(resourceName, resource) log.msg("%r: Created resource: %s" % (request, url)) request.setResponseCode(http.CREATED) request.setHeader("Location", url) else: #log.msg("Resource already exists: %s" % resourceName) request.setResponseCode(http.CONFLICT) else: request.setResponseCode(http.BAD_REQUEST) #log.msg("%r: finished" % request) return ""
def _parseAuth(self, response): def unQuote(s): if s and (s[0] in "\"\'") and (s[0] == s[-1]): s = s[1:-1] return s auth = dict() log.msg("Parsing response: {0!r}".format(response)) for segment in response.replace("\n", " ").split(","): log.msg("Parsing segment: {0!r}".format(segment)) key, val = [s.strip() for s in segment.split("=", 1)] auth[key] = unQuote(val) return auth
def _openPage(self, url): log.msg("Opening url: %r" % url) return urlopen(url).read()
def _login(self, creds): log.msg("Logging in: {0!r}".format(creds)) return HTTPAuthSessionWrapper._login(self, creds)
def requestAvatarId(self, cred): log.msg("Requesting avatar id: {0}".format(cred)) if cred.secret != self.secret: raise UnauthorizedLogin("Unauthorized login.") return cred.name
def test_post(self): resource = "naboo" createdUrl = self.generatorUrl + resource postEntity = json.dumps({"resource": resource}) headers = {"Content-type":"application/json"} # Trying to GET a resource before we generate it should fail try: yield self.getPage(createdUrl, method="GET") except WebError, we: self.assertEquals(http.NOT_FOUND, we.status) else: self.fail("Fetched %s before it was created" % createdUrl) # Generating the new resource should work log.msg("POSTing to generate content.") result = yield self.getPage(self.generatorUrl, method="POST", headers=headers, data=postEntity) log.msg("POSTed to generate content.") self.assertEquals(http.CREATED, result.status) self.assertHeader(result, "Location", createdUrl) # Now we should be able to GET the generated resource result = yield self.getPage(createdUrl, method="GET") self.assertTrue(result.status in OKAY_CODES) # Trying to generate the same resource again should fail try: yield self.getPage(self.generatorUrl, method="POST", headers=headers, data=postEntity)