Ticket #6401: 6401.patch

File 6401.patch, 10.3 KB (added by Kai Zhang, 7 years ago)

Now twisted.web.test.requesthelper.DummyChannel.TCP is inherit from twisted.test.proto_helpers.StringTransport. Tests in twisted.web.test are revised accordingly.

  • twisted/web/test/requesthelper.py

     
    1717from twisted.internet.address import IPv4Address
    1818from twisted.internet.interfaces import ISSLTransport
    1919
     20from twisted.test.proto_helpers import StringTransport
     21
    2022from twisted.web.http_headers import Headers
    2123from twisted.web.resource import Resource
    2224from twisted.web.server import NOT_DONE_YET, Session, Site
    2325
    2426
    2527class DummyChannel:
    26     class TCP:
     28    class TCP(StringTransport):
    2729        port = 80
    28         disconnected = False
    2930
    30         def __init__(self):
    31             self.written = BytesIO()
    32             self.producers = []
    33 
    34         def getPeer(self):
    35             return IPv4Address("TCP", '192.168.1.1', 12344)
    36 
    37         def write(self, data):
    38             if not isinstance(data, bytes):
    39                 raise TypeError("Can only write bytes to a transport, not %r" % (data,))
    40             self.written.write(data)
    41 
    42         def writeSequence(self, iovec):
    43             for data in iovec:
    44                 self.write(data)
    45 
    4631        def getHost(self):
    4732            return IPv4Address("TCP", '10.0.0.1', self.port)
    4833
    49         def registerProducer(self, producer, streaming):
    50             self.producers.append((producer, streaming))
    5134
    52         def loseConnection(self):
    53             self.disconnected = True
    54 
    55 
    5635    @implementer(ISSLTransport)
    5736    class SSL(TCP):
    5837        pass
  • twisted/web/test/test_wsgi.py

     
    907907        d, requestFactory = self.requestFactoryFactory()
    908908        def cbRendered(ignored):
    909909            self.assertTrue(
    910                 channel.transport.written.getvalue().startswith(
     910                channel.transport.value().startswith(
    911911                    'HTTP/1.1 107 Strange message'))
    912912        d.addCallback(cbRendered)
    913913
     
    938938
    939939        d, requestFactory = self.requestFactoryFactory()
    940940        def cbRendered(ignored):
    941             response = channel.transport.written.getvalue()
     941            response = channel.transport.value()
    942942            headers, rest = response.split('\r\n\r\n', 1)
    943943            headerLines = headers.split('\r\n')[1:]
    944944            headerLines.sort()
     
    10011001
    10021002        intermediateValues = []
    10031003        def record():
    1004             intermediateValues.append(channel.transport.written.getvalue())
     1004            intermediateValues.append(channel.transport.value())
    10051005
    10061006        def applicationFactory():
    10071007            def application(environ, startResponse):
     
    10331033
    10341034        intermediateValues = []
    10351035        def record():
    1036             intermediateValues.append(channel.transport.written.getvalue())
     1036            intermediateValues.append(channel.transport.value())
    10371037
    10381038        def applicationFactory():
    10391039            def application(environ, startResponse):
     
    10661066
    10671067        intermediateValues = []
    10681068        def record():
    1069             intermediateValues.append(channel.transport.written.getvalue())
     1069            intermediateValues.append(channel.transport.value())
    10701070
    10711071        def applicationFactory():
    10721072            def application(environ, startResponse):
     
    11121112        d, requestFactory = self.requestFactoryFactory()
    11131113        def cbRendered(ignored):
    11141114            self.assertTrue(
    1115                 channel.transport.written.getvalue().startswith(
     1115                channel.transport.value().startswith(
    11161116                    'HTTP/1.1 200 Bar\r\n'))
    11171117        d.addCallback(cbRendered)
    11181118
     
    11401140        d, requestFactory = self.requestFactoryFactory()
    11411141        def cbRendered(ignored):
    11421142            self.assertTrue(
    1143                 channel.transport.written.getvalue().startswith(
     1143                channel.transport.value().startswith(
    11441144                    'HTTP/1.1 100 Foo\r\n'))
    11451145        d.addCallback(cbRendered)
    11461146
     
    11831183        d, requestFactory = self.requestFactoryFactory()
    11841184        def cbRendered(ignored):
    11851185            self.assertTrue(
    1186                 channel.transport.written.getvalue().startswith(
     1186                channel.transport.value().startswith(
    11871187                    'HTTP/1.1 200 OK\r\n'))
    11881188            self.assertEqual(reraised[0][0], excInfo[0])
    11891189            self.assertEqual(reraised[0][1], excInfo[1])
     
    12071207
    12081208        intermediateValues = []
    12091209        def record():
    1210             intermediateValues.append(channel.transport.written.getvalue())
     1210            intermediateValues.append(channel.transport.value())
    12111211
    12121212        def applicationFactory():
    12131213            def application(environ, startResponse):
     
    12791279        def cbRendered(ignored):
    12801280            self.assertEqual(
    12811281                self.getContentFromResponse(
    1282                     channel.transport.written.getvalue()),
     1282                    channel.transport.value()),
    12831283                '012')
    12841284            self.assertFalse(result.open)
    12851285        d.addCallback(cbRendered)
     
    14651465            self.assertEqual(len(errors), 1)
    14661466
    14671467            self.assertTrue(
    1468                 channel.transport.written.getvalue().startswith(
     1468                channel.transport.value().startswith(
    14691469                    'HTTP/1.1 500 Internal Server Error'))
    14701470        d.addCallback(cbRendered)
    14711471
     
    15161516            errors = self.flushLoggedErrors(RuntimeError)
    15171517            self.assertEqual(len(errors), 1)
    15181518
    1519             response = channel.transport.written.getvalue()
     1519            response = channel.transport.value()
    15201520            self.assertTrue(response.startswith('HTTP/1.1 200 OK'))
    15211521            # Chunked transfer-encoding makes this a little messy.
    15221522            self.assertIn(responseContent, response)
     
    15271527            lambda: channel, 'GET', '1.1', [], [''], None, [])
    15281528
    15291529        # By now the connection should be closed.
    1530         self.assertTrue(channel.transport.disconnected)
     1530        self.assertTrue(channel.transport.disconnecting)
    15311531        # Give it a little push to go the rest of the way.
    15321532        requests[0].connectionLost(Failure(ConnectionLost("All gone")))
    15331533
  • twisted/web/test/test_web.py

     
    538538        request.requestHeaders.setRawHeaders(b"Accept-Encoding",
    539539                                             [b"gzip,deflate"])
    540540        request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
    541         data = self.channel.transport.written.getvalue()
     541        data = self.channel.transport.value()
    542542        self.assertNotIn(b"Content-Length", data)
    543543        self.assertIn(b"Content-Encoding: gzip\r\n", data)
    544544        body = data[data.find(b"\r\n\r\n") + 4:]
     
    556556        request.requestHeaders.setRawHeaders(b"Accept-Encoding",
    557557                                             [b"foo,bar"])
    558558        request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
    559         data = self.channel.transport.written.getvalue()
     559        data = self.channel.transport.value()
    560560        self.assertIn(b"Content-Length", data)
    561561        self.assertNotIn(b"Content-Encoding: gzip\r\n", data)
    562562        body = data[data.find(b"\r\n\r\n") + 4:]
     
    574574        request.requestHeaders.setRawHeaders(b"Accept-Encoding",
    575575                                             [b"deflate", b"gzip"])
    576576        request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
    577         data = self.channel.transport.written.getvalue()
     577        data = self.channel.transport.value()
    578578        self.assertNotIn(b"Content-Length", data)
    579579        self.assertIn(b"Content-Encoding: gzip\r\n", data)
    580580        body = data[data.find(b"\r\n\r\n") + 4:]
     
    594594        request.responseHeaders.setRawHeaders(b"Content-Encoding",
    595595                                             [b"deflate"])
    596596        request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
    597         data = self.channel.transport.written.getvalue()
     597        data = self.channel.transport.value()
    598598        self.assertNotIn(b"Content-Length", data)
    599599        self.assertIn(b"Content-Encoding: deflate,gzip\r\n", data)
    600600        body = data[data.find(b"\r\n\r\n") + 4:]
     
    615615        request.responseHeaders.setRawHeaders(b"Content-Encoding",
    616616                                             [b"foo", b"bar"])
    617617        request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
    618         data = self.channel.transport.written.getvalue()
     618        data = self.channel.transport.value()
    619619        self.assertNotIn(b"Content-Length", data)
    620620        self.assertIn(b"Content-Encoding: foo,bar,gzip\r\n", data)
    621621        body = data[data.find(b"\r\n\r\n") + 4:]
  • twisted/web/test/test_http.py

     
    12561256        req.setResponseCode(201)
    12571257        req.write(b'')
    12581258        self.assertEqual(
    1259             channel.transport.written.getvalue().splitlines()[0],
     1259            channel.transport.value().splitlines()[0],
    12601260            b"(no clientproto yet) 201 Created")
    12611261
    12621262
     
    12701270        req.setResponseCode(202, "happily accepted")
    12711271        req.write(b'')
    12721272        self.assertEqual(
    1273             channel.transport.written.getvalue().splitlines()[0],
     1273            channel.transport.value().splitlines()[0],
    12741274            b'(no clientproto yet) 202 happily accepted')
    12751275
    12761276
     
    15851585        req = http.Request(DummyChannel(), False)
    15861586        producer = DummyProducer()
    15871587        req.registerProducer(producer, True)
    1588         self.assertEqual([(producer, True)], req.transport.producers)
     1588        self.assertEqual(producer, req.transport.producer)
     1589        self.assertEqual(True, req.transport.streaming)
    15891590
    15901591
    15911592    def test_registerProducerWhenNotQueuedRegistersPullProducer(self):
     
    15971598        req = http.Request(DummyChannel(), False)
    15981599        producer = DummyProducer()
    15991600        req.registerProducer(producer, False)
    1600         self.assertEqual([(producer, False)], req.transport.producers)
     1601        self.assertEqual(producer, req.transport.producer)
     1602        self.assertEqual(False, req.transport.streaming)
    16011603
    16021604
    16031605    def test_connectionLostNotification(self):