Python CGI FieldStorage test harness

2019-05-01 00:56发布

问题:

How can I set up a small test harness for Python CGI script? I don't want to run a server in order to test it, but I do want to supply various GET/POST inputs for my test.

It appears to me that FieldStorage (or the object behind it) is utterly immutable, so I don't see how to supply the CGI data on the fly in a harness.

回答1:

You could use a mocking library, such as Mock to do the job. For example, suppose you want to test the function_to_test function from your CGI script, you could write a unittest class like this:

import unittest
import cgi

from mock import patch

def function_to_test():
    form = cgi.FieldStorage()
    if "name" not in form or "addr" not in form:
        return "<H1>Error</H1>\nPlease fill in the name and address.\n"
    text = "<p>name: {0}\n<p>addr: {1}\n"
    return text.format(form["name"].value, form["addr"].value)

@patch('cgi.FieldStorage')
class TestClass(unittest.TestCase):
    class TestField(object):
        def __init__(self, value):
            self.value = value

    FIELDS = { "name" : TestField("Bill"), "addr" : TestField("1 Two Street") }

    def test_cgi(self, MockClass):
        instance = MockClass.return_value
        instance.__getitem__ = lambda s, key: TestClass.FIELDS[key]
        instance.__contains__ = lambda s, key: key in TestClass.FIELDS
        text = function_to_test()
        self.assertEqual(text, "<p>name: Bill\n<p>addr: 1 Two Street\n")

    def test_err(self, MockClass):
        instance = MockClass.return_value
        instance.__contains__ = lambda self, key: False
        text = function_to_test()
        self.assertEqual(text,
            "<H1>Error</H1>\nPlease fill in the name and address.\n")

If I run this code as a unit test I get:

..
----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK


回答2:

In case you do not want to use an extra library such as Mock: it is possible to set up a cgi.FieldStorage object with some test data. The Python3 example below assumes that you expect a POST input:

import unittest
from io import BytesIO

class TestForm(unittest.TestCase):

    def setUp(self):
        """
        Makes a cgi.FieldStorage object
        with some bogus fields.
        """
        # provide a byte string with the parameters
        # using the format b"name1=value1&name2=value2..."
        urlencode_data = b"firstname=Joe&lastname=Bloggs&email=joe.bloggs@company.com"
        urlencode_environ = {
            'CONTENT_LENGTH':   str(len(urlencode_data)),
            'CONTENT_TYPE':     'application/x-www-form-urlencoded',
            'QUERY_STRING':     '',
            'REQUEST_METHOD':   'POST',
        }
        data = BytesIO(urlencode_data)
        data.seek(0)
        self.fs = cgi.FieldStorage(fp=data, environ=urlencode_environ)

    # unit test methods come here
    # form fields are accessible via `self.fs`

The idea comes from https://bugs.python.org/file9507/cgitest.py. There you can find other interesting examples, e.g. forms with file upload etc.

Note that the __init__ method of cgi.FieldStorage is undocumented, or at least I could not find it in the current cgi module documentation.