Ticket #6401: 6401.1.patch

File 6401.1.patch, 13.2 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/test_wsgi.py

     
    1717from twisted.python.log import addObserver, removeObserver, err
    1818from twisted.python.failure import Failure
    1919from twisted.python.threadpool import ThreadPool
     20from twisted.internet.address import IPv4Address
    2021from twisted.internet.defer import Deferred, gatherResults
    2122from twisted.internet import reactor
    2223from twisted.internet.error import ConnectionLost
     
    403404        def makeChannel():
    404405            channel = DummyChannel()
    405406            channel.transport = DummyChannel.TCP()
    406             channel.transport.port = portNumber
     407            channel.transport.hostAddr = IPv4Address("TCP", '10.0.0.1', portNumber)
    407408            return channel
    408409        self.channelFactory = makeChannel
    409410
     
    907908        d, requestFactory = self.requestFactoryFactory()
    908909        def cbRendered(ignored):
    909910            self.assertTrue(
    910                 channel.transport.written.getvalue().startswith(
     911                channel.transport.value().startswith(
    911912                    'HTTP/1.1 107 Strange message'))
    912913        d.addCallback(cbRendered)
    913914
     
    938939
    939940        d, requestFactory = self.requestFactoryFactory()
    940941        def cbRendered(ignored):
    941             response = channel.transport.written.getvalue()
     942            response = channel.transport.value()
    942943            headers, rest = response.split('\r\n\r\n', 1)
    943944            headerLines = headers.split('\r\n')[1:]
    944945            headerLines.sort()
     
    10011002
    10021003        intermediateValues = []
    10031004        def record():
    1004             intermediateValues.append(channel.transport.written.getvalue())
     1005            intermediateValues.append(channel.transport.value())
    10051006
    10061007        def applicationFactory():
    10071008            def application(environ, startResponse):
     
    10331034
    10341035        intermediateValues = []
    10351036        def record():
    1036             intermediateValues.append(channel.transport.written.getvalue())
     1037            intermediateValues.append(channel.transport.value())
    10371038
    10381039        def applicationFactory():
    10391040            def application(environ, startResponse):
     
    10661067
    10671068        intermediateValues = []
    10681069        def record():
    1069             intermediateValues.append(channel.transport.written.getvalue())
     1070            intermediateValues.append(channel.transport.value())
    10701071
    10711072        def applicationFactory():
    10721073            def application(environ, startResponse):
     
    11121113        d, requestFactory = self.requestFactoryFactory()
    11131114        def cbRendered(ignored):
    11141115            self.assertTrue(
    1115                 channel.transport.written.getvalue().startswith(
     1116                channel.transport.value().startswith(
    11161117                    'HTTP/1.1 200 Bar\r\n'))
    11171118        d.addCallback(cbRendered)
    11181119
     
    11401141        d, requestFactory = self.requestFactoryFactory()
    11411142        def cbRendered(ignored):
    11421143            self.assertTrue(
    1143                 channel.transport.written.getvalue().startswith(
     1144                channel.transport.value().startswith(
    11441145                    'HTTP/1.1 100 Foo\r\n'))
    11451146        d.addCallback(cbRendered)
    11461147
     
    11831184        d, requestFactory = self.requestFactoryFactory()
    11841185        def cbRendered(ignored):
    11851186            self.assertTrue(
    1186                 channel.transport.written.getvalue().startswith(
     1187                channel.transport.value().startswith(
    11871188                    'HTTP/1.1 200 OK\r\n'))
    11881189            self.assertEqual(reraised[0][0], excInfo[0])
    11891190            self.assertEqual(reraised[0][1], excInfo[1])
     
    12071208
    12081209        intermediateValues = []
    12091210        def record():
    1210             intermediateValues.append(channel.transport.written.getvalue())
     1211            intermediateValues.append(channel.transport.value())
    12111212
    12121213        def applicationFactory():
    12131214            def application(environ, startResponse):
     
    12791280        def cbRendered(ignored):
    12801281            self.assertEqual(
    12811282                self.getContentFromResponse(
    1282                     channel.transport.written.getvalue()),
     1283                    channel.transport.value()),
    12831284                '012')
    12841285            self.assertFalse(result.open)
    12851286        d.addCallback(cbRendered)
     
    14651466            self.assertEqual(len(errors), 1)
    14661467
    14671468            self.assertTrue(
    1468                 channel.transport.written.getvalue().startswith(
     1469                channel.transport.value().startswith(
    14691470                    'HTTP/1.1 500 Internal Server Error'))
    14701471        d.addCallback(cbRendered)
    14711472
     
    15161517            errors = self.flushLoggedErrors(RuntimeError)
    15171518            self.assertEqual(len(errors), 1)
    15181519
    1519             response = channel.transport.written.getvalue()
     1520            response = channel.transport.value()
    15201521            self.assertTrue(response.startswith('HTTP/1.1 200 OK'))
    15211522            # Chunked transfer-encoding makes this a little messy.
    15221523            self.assertIn(responseContent, response)
     
    15271528            lambda: channel, 'GET', '1.1', [], [''], None, [])
    15281529
    15291530        # By now the connection should be closed.
    1530         self.assertTrue(channel.transport.disconnected)
     1531        self.assertTrue(channel.transport.disconnecting)
    15311532        # Give it a little push to go the rest of the way.
    15321533        requests[0].connectionLost(Failure(ConnectionLost("All gone")))
    15331534
  • 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):
  • twisted/web/test/test_web.py

     
    427427
    428428    def testPrePathURLNonDefault(self):
    429429        d = DummyChannel()
    430         d.transport.port = 81
     430        d.transport.hostAddr = IPv4Address("TCP", '10.0.0.1', 81)
    431431        request = server.Request(d, 1)
    432432        request.setHost(b'example.com', 81)
    433433        request.gotLength(0)
     
    436436
    437437    def testPrePathURLSSLPort(self):
    438438        d = DummyChannel()
    439         d.transport.port = 443
     439        d.transport.hostAddr = IPv4Address("TCP", '10.0.0.1', 443)
    440440        request = server.Request(d, 1)
    441441        request.setHost(b'example.com', 443)
    442442        request.gotLength(0)
     
    446446    def testPrePathURLSSLPortAndSSL(self):
    447447        d = DummyChannel()
    448448        d.transport = DummyChannel.SSL()
    449         d.transport.port = 443
     449        d.transport.hostAddr = IPv4Address("TCP", '10.0.0.1', 443)
    450450        request = server.Request(d, 1)
    451451        request.setHost(b'example.com', 443)
    452452        request.gotLength(0)
     
    456456    def testPrePathURLHTTPPortAndSSL(self):
    457457        d = DummyChannel()
    458458        d.transport = DummyChannel.SSL()
    459         d.transport.port = 80
     459        d.transport.hostAddr = IPv4Address("TCP", '10.0.0.1', 80)
    460460        request = server.Request(d, 1)
    461461        request.setHost(b'example.com', 80)
    462462        request.gotLength(0)
     
    466466    def testPrePathURLSSLNonDefault(self):
    467467        d = DummyChannel()
    468468        d.transport = DummyChannel.SSL()
    469         d.transport.port = 81
     469        d.transport.hostAddr = IPv4Address("TCP", '10.0.0.1', 81)
    470470        request = server.Request(d, 1)
    471471        request.setHost(b'example.com', 81)
    472472        request.gotLength(0)
     
    475475
    476476    def testPrePathURLSetSSLHost(self):
    477477        d = DummyChannel()
    478         d.transport.port = 81
     478        d.transport.hostAddr = IPv4Address("TCP", '10.0.0.1', 81)
    479479        request = server.Request(d, 1)
    480480        request.setHost(b'foo.com', 81, 1)
    481481        request.gotLength(0)
     
    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/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:
    27         port = 80
    28         disconnected = False
     28    class TCP(StringTransport):
     29        pass
    2930
    30         def __init__(self):
    31             self.written = BytesIO()
    32             self.producers = []
    3331
    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 
    46         def getHost(self):
    47             return IPv4Address("TCP", '10.0.0.1', self.port)
    48 
    49         def registerProducer(self, producer, streaming):
    50             self.producers.append((producer, streaming))
    51 
    52         def loseConnection(self):
    53             self.disconnected = True
    54 
    55 
    5632    @implementer(ISSLTransport)
    5733    class SSL(TCP):
    5834        pass
     
    6036    site = Site(Resource())
    6137
    6238    def __init__(self):
    63         self.transport = self.TCP()
     39        self.transport = self.TCP(IPv4Address("TCP", '10.0.0.1', 80))
    6440
    6541
    6642    def requestDone(self, request):