aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/web/test/requesthelper.py
blob: a3b0904427ebc3ced38791828da5d0577bee529c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Helpers related to HTTP requests, used by tests.
"""

from __future__ import annotations

__all__ = ["DummyChannel", "DummyRequest"]

from io import BytesIO
from typing import Dict, List, Optional

from zope.interface import implementer, verify

from incremental import Version

from twisted.internet.address import IPv4Address, IPv6Address
from twisted.internet.defer import Deferred
from twisted.internet.interfaces import IAddress, ISSLTransport
from twisted.internet.task import Clock
from twisted.python.deprecate import deprecated
from twisted.trial import unittest
from twisted.web._responses import FOUND
from twisted.web.http_headers import Headers
from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET, Session, Site

textLinearWhitespaceComponents = [f"Foo{lw}bar" for lw in ["\r", "\n", "\r\n"]]

sanitizedText = "Foo bar"
bytesLinearWhitespaceComponents = [
    component.encode("ascii") for component in textLinearWhitespaceComponents
]
sanitizedBytes = sanitizedText.encode("ascii")


@implementer(IAddress)
class NullAddress:
    """
    A null implementation of L{IAddress}.
    """


class DummyChannel:
    class TCP:
        port = 80
        disconnected = False

        def __init__(self, peer=None):
            if peer is None:
                peer = IPv4Address("TCP", "192.168.1.1", 12344)
            self._peer = peer
            self.written = BytesIO()
            self.producers = []

        def getPeer(self):
            return self._peer

        def write(self, data):
            if not isinstance(data, bytes):
                raise TypeError(f"Can only write bytes to a transport, not {data!r}")
            self.written.write(data)

        def writeSequence(self, iovec):
            for data in iovec:
                self.write(data)

        def getHost(self):
            return IPv4Address("TCP", "10.0.0.1", self.port)

        def registerProducer(self, producer, streaming):
            self.producers.append((producer, streaming))

        def unregisterProducer(self):
            pass

        def loseConnection(self):
            self.disconnected = True

    @implementer(ISSLTransport)
    class SSL(TCP):
        def abortConnection(self):
            # ITCPTransport.abortConnection
            pass

        def getTcpKeepAlive(self):
            # ITCPTransport.getTcpKeepAlive
            pass

        def getTcpNoDelay(self):
            # ITCPTransport.getTcpNoDelay
            pass

        def loseWriteConnection(self):
            # ITCPTransport.loseWriteConnection
            pass

        def setTcpKeepAlive(self, enabled):
            # ITCPTransport.setTcpKeepAlive
            pass

        def setTcpNoDelay(self, enabled):
            # ITCPTransport.setTcpNoDelay
            pass

        def getPeerCertificate(self):
            # ISSLTransport.getPeerCertificate
            pass

    site = Site(Resource())

    def __init__(self, peer=None):
        self.transport = self.TCP(peer)

    def requestDone(self, request):
        pass

    def writeHeaders(self, version, code, reason, headers):
        response_line = version + b" " + code + b" " + reason + b"\r\n"
        headerSequence = [response_line]
        headerSequence.extend(name + b": " + value + b"\r\n" for name, value in headers)
        headerSequence.append(b"\r\n")
        self.transport.writeSequence(headerSequence)

    def getPeer(self):
        return self.transport.getPeer()

    def getHost(self):
        return self.transport.getHost()

    def registerProducer(self, producer, streaming):
        self.transport.registerProducer(producer, streaming)

    def unregisterProducer(self):
        self.transport.unregisterProducer()

    def write(self, data):
        self.transport.write(data)

    def writeSequence(self, iovec):
        self.transport.writeSequence(iovec)

    def loseConnection(self):
        self.transport.loseConnection()

    def endRequest(self):
        pass

    def isSecure(self):
        return isinstance(self.transport, self.SSL)

    def abortConnection(self):
        # ITCPTransport.abortConnection
        pass

    def getTcpKeepAlive(self):
        # ITCPTransport.getTcpKeepAlive
        pass

    def getTcpNoDelay(self):
        # ITCPTransport.getTcpNoDelay
        pass

    def loseWriteConnection(self):
        # ITCPTransport.loseWriteConnection
        pass

    def setTcpKeepAlive(self):
        # ITCPTransport.setTcpKeepAlive
        pass

    def setTcpNoDelay(self):
        # ITCPTransport.setTcpNoDelay
        pass

    def getPeerCertificate(self):
        # ISSLTransport.getPeerCertificate
        pass


class DummyRequest:
    """
    Represents a dummy or fake request. See L{twisted.web.server.Request}.

    @ivar _finishedDeferreds: L{None} or a C{list} of L{Deferreds} which will
        be called back with L{None} when C{finish} is called or which will be
        errbacked if C{processingFailed} is called.

    @type requestheaders: C{Headers}
    @ivar requestheaders: A Headers instance that stores values for all request
        headers.

    @type responseHeaders: C{Headers}
    @ivar responseHeaders: A Headers instance that stores values for all
        response headers.

    @type responseCode: C{int}
    @ivar responseCode: The response code which was passed to
        C{setResponseCode}.

    @type written: C{list} of C{bytes}
    @ivar written: The bytes which have been written to the request.
    """

    uri = b"http://dummy/"
    method = b"GET"
    client: Optional[IAddress] = None
    sitepath: List[bytes]
    written: List[bytes]
    prepath: List[bytes]
    args: Dict[bytes, List[bytes]]
    _finishedDeferreds: List[Deferred[None]]

    def registerProducer(self, prod, s):
        """
        Call an L{IPullProducer}'s C{resumeProducing} method in a
        loop until it unregisters itself.

        @param prod: The producer.
        @type prod: L{IPullProducer}

        @param s: Whether or not the producer is streaming.
        """
        # XXX: Handle IPushProducers
        self.go = 1
        while self.go:
            prod.resumeProducing()

    def unregisterProducer(self):
        self.go = 0

    def __init__(
        self,
        postpath: list[bytes],
        session: Optional[Session] = None,
        client: Optional[IAddress] = None,
    ) -> None:
        self.sitepath = []
        self.written = []
        self.finished = 0
        self.postpath = postpath
        self.prepath = []
        self.session = None
        self.protoSession = session or Session(site=None, uid=b"0", reactor=Clock())
        self.args = {}
        self.requestHeaders = Headers()
        self.responseHeaders = Headers()
        self.responseCode = None
        self._finishedDeferreds = []
        self._serverName = b"dummy"
        self.clientproto = b"HTTP/1.0"

    def getAllHeaders(self):
        """
        Return dictionary mapping the names of all received headers to the last
        value received for each.

        Since this method does not return all header information,
        C{self.requestHeaders.getAllRawHeaders()} may be preferred.

        NOTE: This function is a direct copy of
        C{twisted.web.http.Request.getAllRawHeaders}.
        """
        headers = {}
        for k, v in self.requestHeaders.getAllRawHeaders():
            headers[k.lower()] = v[-1]
        return headers

    def getHeader(self, name):
        """
        Retrieve the value of a request header.

        @type name: C{bytes}
        @param name: The name of the request header for which to retrieve the
            value.  Header names are compared case-insensitively.

        @rtype: C{bytes} or L{None}
        @return: The value of the specified request header.
        """
        return self.requestHeaders.getRawHeaders(name.lower(), [None])[0]

    def setHeader(self, name, value):
        """TODO: make this assert on write() if the header is content-length"""
        self.responseHeaders.addRawHeader(name, value)

    def getSession(self, sessionInterface=None):
        if self.session:
            return self.session
        assert (
            not self.written
        ), "Session cannot be requested after data has been written."
        self.session = self.protoSession
        return self.session

    def render(self, resource):
        """
        Render the given resource as a response to this request.

        This implementation only handles a few of the most common behaviors of
        resources.  It can handle a render method that returns a string or
        C{NOT_DONE_YET}.  It doesn't know anything about the semantics of
        request methods (eg HEAD) nor how to set any particular headers.
        Basically, it's largely broken, but sufficient for some tests at least.
        It should B{not} be expanded to do all the same stuff L{Request} does.
        Instead, L{DummyRequest} should be phased out and L{Request} (or some
        other real code factored in a different way) used.
        """
        result = resource.render(self)
        if result is NOT_DONE_YET:
            return
        self.write(result)
        self.finish()

    def write(self, data):
        if not isinstance(data, bytes):
            raise TypeError("write() only accepts bytes")
        self.written.append(data)

    def notifyFinish(self) -> Deferred[None]:
        """
        Return a L{Deferred} which is called back with L{None} when the request
        is finished.  This will probably only work if you haven't called
        C{finish} yet.
        """
        finished: Deferred[None] = Deferred()
        self._finishedDeferreds.append(finished)
        return finished

    def finish(self):
        """
        Record that the request is finished and callback and L{Deferred}s
        waiting for notification of this.
        """
        self.finished = self.finished + 1
        if self._finishedDeferreds is not None:
            observers = self._finishedDeferreds
            self._finishedDeferreds = None
            for obs in observers:
                obs.callback(None)

    def processingFailed(self, reason):
        """
        Errback and L{Deferreds} waiting for finish notification.
        """
        if self._finishedDeferreds is not None:
            observers = self._finishedDeferreds
            self._finishedDeferreds = None
            for obs in observers:
                obs.errback(reason)

    def addArg(self, name, value):
        self.args[name] = [value]

    def setResponseCode(self, code, message=None):
        """
        Set the HTTP status response code, but takes care that this is called
        before any data is written.
        """
        assert (
            not self.written
        ), "Response code cannot be set after data has" "been written: {}.".format(
            "@@@@".join(self.written)
        )
        self.responseCode = code
        self.responseMessage = message

    def setLastModified(self, when):
        assert (
            not self.written
        ), "Last-Modified cannot be set after data has " "been written: {}.".format(
            "@@@@".join(self.written)
        )

    def setETag(self, tag):
        assert (
            not self.written
        ), "ETag cannot be set after data has been " "written: {}.".format(
            "@@@@".join(self.written)
        )

    @deprecated(Version("Twisted", 18, 4, 0), replacement="getClientAddress")
    def getClientIP(self):
        """
        Return the IPv4 address of the client which made this request, if there
        is one, otherwise L{None}.
        """
        if isinstance(self.client, (IPv4Address, IPv6Address)):
            return self.client.host
        return None

    def getClientAddress(self):
        """
        Return the L{IAddress} of the client that made this request.

        @return: an address.
        @rtype: an L{IAddress} provider.
        """
        if self.client is None:
            return NullAddress()
        return self.client

    def getRequestHostname(self):
        """
        Get a dummy hostname associated to the HTTP request.

        @rtype: C{bytes}
        @returns: a dummy hostname
        """
        return self._serverName

    def getHost(self):
        """
        Get a dummy transport's host.

        @rtype: C{IPv4Address}
        @returns: a dummy transport's host
        """
        return IPv4Address("TCP", "127.0.0.1", 80)

    def setHost(self, host, port, ssl=0):
        """
        Change the host and port the request thinks it's using.

        @type host: C{bytes}
        @param host: The value to which to change the host header.

        @type ssl: C{bool}
        @param ssl: A flag which, if C{True}, indicates that the request is
            considered secure (if C{True}, L{isSecure} will return C{True}).
        """
        self._forceSSL = ssl  # set first so isSecure will work
        if self.isSecure():
            default = 443
        else:
            default = 80
        if port == default:
            hostHeader = host
        else:
            hostHeader = b"%b:%d" % (host, port)
        self.requestHeaders.addRawHeader(b"host", hostHeader)

    def redirect(self, url):
        """
        Utility function that does a redirect.

        The request should have finish() called after this.
        """
        self.setResponseCode(FOUND)
        self.setHeader(b"location", url)


class DummyRequestTests(unittest.SynchronousTestCase):
    """
    Tests for L{DummyRequest}.
    """

    def test_getClientIPDeprecated(self):
        """
        L{DummyRequest.getClientIP} is deprecated in favor of
        L{DummyRequest.getClientAddress}
        """

        request = DummyRequest([])
        request.getClientIP()

        warnings = self.flushWarnings(
            offendingFunctions=[self.test_getClientIPDeprecated]
        )

        self.assertEqual(1, len(warnings))
        [warning] = warnings
        self.assertEqual(warning.get("category"), DeprecationWarning)
        self.assertEqual(
            warning.get("message"),
            (
                "twisted.web.test.requesthelper.DummyRequest.getClientIP "
                "was deprecated in Twisted 18.4.0; "
                "please use getClientAddress instead"
            ),
        )

    def test_getClientIPSupportsIPv6(self):
        """
        L{DummyRequest.getClientIP} supports IPv6 addresses, just like
        L{twisted.web.http.Request.getClientIP}.
        """
        request = DummyRequest([])
        client = IPv6Address("TCP", "::1", 12345)
        request.client = client

        self.assertEqual("::1", request.getClientIP())

    def test_getClientAddressWithoutClient(self):
        """
        L{DummyRequest.getClientAddress} returns an L{IAddress}
        provider no C{client} has been set.
        """
        request = DummyRequest([])
        null = request.getClientAddress()
        verify.verifyObject(IAddress, null)

    def test_getClientAddress(self):
        """
        L{DummyRequest.getClientAddress} returns the C{client}.
        """
        request = DummyRequest([])
        client = IPv4Address("TCP", "127.0.0.1", 12345)
        request.client = client
        address = request.getClientAddress()
        self.assertIs(address, client)