def test_getMultiFormatedResource(self): """ Get resource in a certain format using multiple style sheets. """ proc = Processor(self.env) # create resource proc.run(POST, '/transformation-test/rt/test.xml', StringIO(XML_DOC)) # transform using a format chain proc.args = {'format': ['xml2html', 'html2txt']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, TXT_DOC) # missing formats proc.args = {'format': ['XXX', 'YYY', 'ZZZ']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, XML_DOC) # one valid format proc.args = {'format': ['XXX', 'xml2html', 'YYY']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, HTML_DOC) # transform using a format chain but last style sheet is missing proc.args = {'format': ['xml2html', 'html2txt', 'XXX']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, TXT_DOC) # transform using a format chain but one style sheet is missing proc.args = {'format': ['xml2html', 'XXX', 'html2txt']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, TXT_DOC) # delete resource proc.run(DELETE, '/transformation-test/rt/test.xml')
def test_getFormatedResource(self): """ Get resource in a certain format using a single style sheet. """ proc = Processor(self.env) # create resource proc.run(POST, '/transformation-test/rt/test.xml', StringIO(XML_DOC)) # without format proc.args = {'format': []} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, XML_DOC) # transform to existing format HTML proc.args = {'format': ['xml2html']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, HTML_DOC) # transform to existing format SVG proc.args = {'format': ['xml2svg']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, SVG_DOC) # missing format proc.args = {'format': ['XXX']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, XML_DOC) # delete resource proc.run(DELETE, '/transformation-test/rt/test.xml')
def test_putFormatedResource(self): """ Upload resource in a certain format using a single style sheets. """ proc = Processor(self.env) # create + transform resource proc.args = {'format': ['xml2html']} proc.run(POST, '/transformation-test/rt/test.xml', StringIO(XML_DOC)) # get uploaded resource proc.args = {} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, HTML_DOC.strip()) # get uploaded resource with format proc.args = {'format': ['html2txt']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, TXT_DOC) # delete resource proc.run(DELETE, '/transformation-test/rt/test.xml')
def test_postMultiFormatedResource(self): """ Update resource in a certain format using multiple style sheets. """ proc = Processor(self.env) # create resource proc.run(POST, '/transformation-test/rt/test.xml', StringIO(XML_DOC)) # update resource with transformation proc.args = {'format': ['xml2html', 'html2xml']} proc.run(PUT, '/transformation-test/rt/test.xml', StringIO(XML_DOC)) # get uploaded resource proc.args = {} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, XML_DOC_2) # get uploaded resource with format proc.args = {'format': ['xml2html', 'html2txt']} res = proc.run(GET, '/transformation-test/rt/test.xml') data = res.render(proc) self.assertEquals(data, 'Sales Results By Division\n\n') # delete resource proc.run(DELETE, '/transformation-test/rt/test.xml')
def _send_request(self, method, url, file_or_fileobject=None, args=None): """ Uploads a file with the given method to the given url. :type method: string :param method: GET, POST, PUT, or DELETE :type url: string :param url: The url to upload to :type file_or_fileobject: string or file-like object :param file_or_fileobject: The file or file like object to upload. If None, then nothing will be uploaded. :type args: dictionary :param args: The arguments of the request. This is the same as any parameters appended to the URL. file_or_fileobject can be either a StringIO with some data or a filename. Returns the respone from the request. """ if method.upper() == "GET": method = GET elif method.upper() == "POST": method = POST elif method.upper() == "PUT": method = PUT elif method.upper() == "DELETE": method = DELETE else: msg = "Unknown method '%s'." % method raise ValueError(msg) proc = Processor(self.env) # Append potential arguments. if args: # Convert everything to strings. The processor usually deals with # URL where everything is a string by default. proc.args = {key: [str(value)] for (key, value) in args.iteritems()} if file_or_fileobject: if not hasattr(file_or_fileobject, "read") or \ not hasattr(file_or_fileobject, "seek"): with open(file_or_fileobject, "r") as open_file: file_or_fileobject = StringIO.StringIO(open_file.read()) else: file_or_fileobject = None response = proc.run(method, url, file_or_fileobject) if method == "GET" and hasattr(response, "render_GET"): class dummy(object): pass dum = dummy() dum.args = {} dum.env = dummy() dum.env.auth = dummy() temp = dummy() temp.permissions = 755 dum.env.auth.getUser = lambda x: temp return self._strip_xml_declaration(response.render_GET(dum)) return response