Beispiel #1
0
    def alter(self, message, arguments):
        """
        Performs operations specified in the arguments to modify the message in
        whatever way.
        """

        message = message.copy()

        # Entity

        if arguments.entity:
            message["entity"] = arguments.entity
        if arguments.readEntity:
            message["entity"] = open(arguments.readEntity, "r").read()
        if arguments.entityStdin:
            message["entity"] = stdin.read()
        if arguments.appendEntity:
            message["entity"] += arguments.appendEntity
        if arguments.appendEntityStdin:
            message["entity"] += stdin.read()
        if arguments.appendEntityFile:
            message["entity"] += open(arguments.appendEntityFile, "r").read()

        # Top-line

        if arguments.version:
            message["version"] = arguments.version
        if arguments.method:
            message["method"] = arguments.method.upper()
        if arguments.path:
            message["path"] = arguments.path
        if arguments.status:
            message["status"] = arguments.status
        if arguments.reason:
            message["reason"] = arguments.reason.upper()

        # Headers

        for header in arguments.header:
            message["headers"] = setValuesByName(message["headers"], *header)
        if arguments.host:
            message["headers"] = setValuesByName(message["headers"], "Host", arguments.host)
        if arguments.auto: # obviously must come after entity
            message["headers"] = setValuesByName(
                message["headers"],
                "Content-length",
                str(len(message["entity"]))
        )

        return message 
Beispiel #2
0
    def testSettingValuesByName(self):

        self.assertEqual(
            tuple(setValuesByName((("A", "B"),), "A", "C")),
            (("A", "C"),)
        )
        self.assertEqual(
            tuple(setValuesByName((("A", "B"),), "C", "D")),
            (("A", "B"), ("C", "D"))
        )
        self.assertEqual(
            tuple(setValuesByName((("A", "B"), ("A", "C")), "A", "Z")),
            (("A", "Z"), ("A", "Z"))
        )
        self.assertEqual(
            tuple(setValuesByName((("a", "B"), ("A", "C")), "A", "Z")),
            (("a", "Z"), ("A", "Z"))
        )