Ejemplo n.º 1
0
    def testHopByHop(self):
        for hop in (
            "Connection Keep-Alive Proxy-Authenticate Proxy-Authorization "
            "TE Trailers Transfer-Encoding Upgrade"
        ).split():
            for alt in hop, hop.title(), hop.upper(), hop.lower():
                self.failUnless(util.is_hop_by_hop(alt))

        # Not comprehensive, just a few random header names
        for hop in (
            "Accept Cache-Control Date Pragma Trailer Via Warning"
        ).split():
            for alt in hop, hop.title(), hop.upper(), hop.lower():
                self.failIf(util.is_hop_by_hop(alt))
Ejemplo n.º 2
0
    def finish_response(self):
        """Send any iterable data, then close self and the iterable

        Subclasses intended for use in asynchronous servers will
        want to redefine this method, such that it sets up callbacks
        in the event loop to iterate over the data, and to call
        'self.close()' once the response is finished.
        """
        if hasattr(self.result, '__call__'):
            raise AssertionError('This server does not support asynchronous '
                                 'responses')

        status, headers, body = self.result

        if not isinstance(status, bytes):
            raise AssertionError(
                "Status must be bytes: %r" % status)
        if not len(status)>=4:
            raise AssertionError(
                "Status must be at least 4 characters: %r" % status)
        if not int(status[:3]):
            raise AssertionError(
                "Status message must begin w/3-digit code: %r" % status)
        if not status[3:4]==b" ":
            import pdb; pdb.set_trace()
            raise AssertionError(
                "Status message must have a space after code: %r" % status)

        for name, val in headers:
            if not isinstance(name, bytes):
                raise AssertionError(
                    "Header names must be bytes: %r" % name)
            if not isinstance(val, bytes):
                raise AssertionError(
                    "Header values must be bytes: %r" % val)
            if is_hop_by_hop(name):
                raise AssertionError(
                    "Hop-by-hop headers not allowed: %r" % name)

        self.status = status
        self.headers = headers
        self.body = body

        self.send_headers()

        for data in body:
            self.write(data)

        self.close()