Pass a test file to CherryPy in test

15 Views Asked by At

My CherryPy app has an upload endpoint, where a user can complete a form and pass some data along with an uploaded file.

I'm interested in testing this endpoint, but I can't figure out how to create that kind of test setup.

Here's a minimal not-working-great example.

import cherrypy
from cherrypy.test import helper


class SimpleCPTest(helper.CPWebCase):
    def setup_server():
        class Root(object):
            @cherrypy.expose
            def echo(self, message):
                return message

            def parsefile(self, file):
                return file.file.read()

        cherrypy.tree.mount(Root())

    def test_message_should_be_returned_as_is(self):
        self.getPage("/echo?message=Hello%20world")
        self.assertStatus("200 OK")
        self.assertBody("Hello world")

    def test_file_should_be_parsed(self):
        self.getPage("/parsefile?file=file_contents")
        self.assertStatus("200 OK")
        self.assertInBody("file_contents")

The /echo test works fine, but unsurprisingly the /parsefile test doesn't.

I've tried byte encoding but I imagine there is a cherrypy builtin I need to use to encode the file contents to pass it in a way that would happen on a web page. What can I do to test this kind of situation?

0

There are 0 best solutions below