view tests/suite_12_methods/files/name.py @ 71:88adf10be709 draft

Add tests.
author David Barts <n5jrn@me.com>
date Mon, 15 Jul 2019 13:16:31 -0700
parents
children
line wrap: on
line source

# It is *technically* an HTTP violation to use POST for an idempotent
# page like this, but it is a good simple test.

from tincan import Page
from jsdict import JSDict

class Name(Page):
    def handle(self):
        self._error_message = JSDict({"subject": "Error", "style": "color: red;"})
        if "name" not in self.request.forms:
            if self.request.method == "GET":
                self.message = None
            else:
                self.message = self.error("This should not happen!")
        else:
            name = self.request.forms["name"]
            if name.strip() == "":
                self.message = self.error("Please enter your name above.")
            else:
                self.message = JSDict({
                    "subject": "Hello, {0}".format(name.split()[0]),
                    "style": None,
                    "body": "Pleased to meet you!"
                })

    def error(self, message):
        self._error_message.body = message
        return self._error_message