Python twisted.web.http.Request() Examples

The following are 30 code examples of twisted.web.http.Request(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.web.http , or try the search function .
Example #1
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_basicAuth(self):
        """
        L{HTTPChannel} provides username and password information supplied in
        an I{Authorization} header to the L{Request} which makes it available
        via its C{getUser} and C{getPassword} methods.
        """
        requests = []
        class Request(http.Request):
            def process(self):
                self.credentials = (self.getUser(), self.getPassword())
                requests.append(self)

        for u, p in [(b"foo", b"bar"), (b"hello", b"there:z")]:
            s = base64.encodestring(b":".join((u, p))).strip()
            f = b"GET / HTTP/1.0\nAuthorization: Basic " + s + b"\n\n"
            self.runRequest(f, Request, 0)
            req = requests.pop()
            self.assertEqual((u, p), req.credentials) 
Example #2
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_chunkedResponses(self):
        """
        Test that the L{HTTPChannel} correctly chunks responses when needed.
        """
        channel = http.HTTPChannel()
        req = http.Request(channel, False)
        trans = StringTransport()

        channel.transport = trans

        req.setResponseCode(200)
        req.clientproto = b"HTTP/1.1"
        req.responseHeaders.setRawHeaders(b"test", [b"lemur"])
        req.write(b'Hello')
        req.write(b'World!')

        self.assertResponseEquals(
            trans.value(),
            [(b"HTTP/1.1 200 OK",
              b"Test: lemur",
              b"Transfer-Encoding: chunked",
              b"5\r\nHello\r\n6\r\nWorld!\r\n")]) 
Example #3
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_addCookieNonStringArgument(self):
        """
        L{http.Request.setCookie} will raise a L{DeprecationWarning} if
        non-string (not L{bytes} or L{unicode}) arguments are given, and will
        call C{str()} on it to preserve past behaviour.
        """
        expectedCookieValue = b"foo=10"

        self._checkCookie(expectedCookieValue, b"foo", 10)

        warnings = self.flushWarnings([self._checkCookie])
        self.assertEqual(1, len(warnings))
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "Passing non-bytes or non-unicode cookie arguments is "
            "deprecated since Twisted 16.1.") 
Example #4
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_firstWrite(self):
        """
        For an HTTP 1.0 request, L{http.Request.write} sends an HTTP 1.0
        Response-Line and whatever response headers are set.
        """
        channel = DummyChannel()
        req = http.Request(channel, False)
        trans = StringTransport()

        channel.transport = trans

        req.setResponseCode(200)
        req.clientproto = b"HTTP/1.0"
        req.responseHeaders.setRawHeaders(b"test", [b"lemur"])
        req.write(b'Hello')

        self.assertResponseEquals(
            trans.value(),
            [(b"HTTP/1.0 200 OK",
              b"Test: lemur",
              b"Hello")]) 
Example #5
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def testGET(self):
        httpRequest = b'''\
GET /?key=value&multiple=two+words&multiple=more%20words&empty= HTTP/1.0

'''
        method = []
        args = []
        testcase = self
        class MyRequest(http.Request):
            def process(self):
                method.append(self.method)
                args.extend([
                        self.args[b"key"],
                        self.args[b"empty"],
                        self.args[b"multiple"]])
                testcase.didRequest = True
                self.finish()

        self.runRequest(httpRequest, MyRequest)
        self.assertEqual(method, [b"GET"])
        self.assertEqual(
            args, [[b"value"], [b""], [b"two words", b"more words"]]) 
Example #6
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_losingConnection(self):
        """
        Calling L{http.Request.loseConnection} causes the transport to be
        disconnected.
        """
        b = StringTransport()
        a = http.HTTPChannel()
        a.requestFactory = self.ShutdownHTTPHandler
        a.makeConnection(b)
        a.dataReceived(self.request)

        # The transport should have been shut down.
        self.assertTrue(b.disconnecting)

        # No response should have been written.
        value = b.value()
        self.assertEqual(value, b'') 
Example #7
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_firstWriteLastModified(self):
        """
        For an HTTP 1.0 request for a resource with a known last modified time,
        L{http.Request.write} sends an HTTP Response-Line, whatever response
        headers are set, and a last-modified header with that time.
        """
        channel = DummyChannel()
        req = http.Request(channel, False)
        trans = StringTransport()

        channel.transport = trans

        req.setResponseCode(200)
        req.clientproto = b"HTTP/1.0"
        req.lastModified = 0
        req.responseHeaders.setRawHeaders(b"test", [b"lemur"])
        req.write(b'Hello')

        self.assertResponseEquals(
            trans.value(),
            [(b"HTTP/1.0 200 OK",
              b"Test: lemur",
              b"Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT",
              b"Hello")]) 
Example #8
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_extraQuestionMark(self):
        """
        While only a single '?' is allowed in an URL, several other servers
        allow several and pass all after the first through as part of the
        query arguments.  Test that we emulate this behavior.
        """
        httpRequest = b'GET /foo?bar=?&baz=quux HTTP/1.0\n\n'

        method = []
        path = []
        args = []
        testcase = self
        class MyRequest(http.Request):
            def process(self):
                method.append(self.method)
                path.append(self.path)
                args.extend([self.args[b'bar'], self.args[b'baz']])
                testcase.didRequest = True
                self.finish()

        self.runRequest(httpRequest, MyRequest)
        self.assertEqual(method, [b'GET'])
        self.assertEqual(path, [b'/foo'])
        self.assertEqual(args, [[b'?'], [b'quux']]) 
Example #9
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def testCookies(self):
        """
        Test cookies parsing and reading.
        """
        httpRequest = b'''\
GET / HTTP/1.0
Cookie: rabbit="eat carrot"; ninja=secret; spam="hey 1=1!"

'''
        cookies = {}
        testcase = self
        class MyRequest(http.Request):
            def process(self):
                for name in [b'rabbit', b'ninja', b'spam']:
                    cookies[name] = self.getCookie(name)
                testcase.didRequest = True
                self.finish()

        self.runRequest(httpRequest, MyRequest)

        self.assertEqual(
            cookies, {
                b'rabbit': b'"eat carrot"',
                b'ninja': b'secret',
                b'spam': b'"hey 1=1!"'}) 
Example #10
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_missingContentDisposition(self):
        """
        If the C{Content-Disposition} header is missing, the request is denied
        as a bad request.
        """
        req = b'''\
POST / HTTP/1.0
Content-Type: multipart/form-data; boundary=AaB03x
Content-Length: 103

--AaB03x
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

abasdfg
--AaB03x--
'''
        channel = self.runRequest(req, http.Request, success=False)
        self.assertEqual(
            channel.transport.value(),
            b"HTTP/1.1 400 Bad Request\r\n\r\n") 
Example #11
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_invalidNonAsciiMethod(self):
        """
        When client sends invalid HTTP method containing
        non-ascii characters HTTP 400 'Bad Request' status will be returned.
        """
        processed = []
        class MyRequest(http.Request):
            def process(self):
                processed.append(self)
                self.finish()

        badRequestLine = b"GE\xc2\xa9 / HTTP/1.1\r\n\r\n"
        channel = self.runRequest(badRequestLine, MyRequest, 0)
        self.assertEqual(
            channel.transport.value(),
            b"HTTP/1.1 400 Bad Request\r\n\r\n")
        self.assertTrue(channel.transport.disconnecting)
        self.assertEqual(processed, []) 
Example #12
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_headers(self):
        """
        Headers received by L{HTTPChannel} in a request are made available to
        the L{Request}.
        """
        processed = []
        class MyRequest(http.Request):
            def process(self):
                processed.append(self)
                self.finish()

        requestLines = [
            b"GET / HTTP/1.0",
            b"Foo: bar",
            b"baz: Quux",
            b"baz: quux",
            b"",
            b""]

        self.runRequest(b'\n'.join(requestLines), MyRequest, 0)
        [request] = processed
        self.assertEqual(
            request.requestHeaders.getRawHeaders(b'foo'), [b'bar'])
        self.assertEqual(
            request.requestHeaders.getRawHeaders(b'bAz'), [b'Quux', b'quux']) 
Example #13
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def _compatHeadersTest(self, oldName, newName):
        """
        Verify that each of two different attributes which are associated with
        the same state properly reflect changes made through the other.

        This is used to test that the C{headers}/C{responseHeaders} and
        C{received_headers}/C{requestHeaders} pairs interact properly.
        """
        req = http.Request(DummyChannel(), False)
        getattr(req, newName).setRawHeaders(b"test", [b"lemur"])
        self.assertEqual(getattr(req, oldName)[b"test"], b"lemur")
        setattr(req, oldName, {b"foo": b"bar"})
        self.assertEqual(
            list(getattr(req, newName).getAllRawHeaders()),
            [(b"Foo", [b"bar"])])
        setattr(req, newName, http_headers.Headers())
        self.assertEqual(getattr(req, oldName), {}) 
Example #14
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_setLastModifiedTwiceCached(self):
        """
        When L{http.Request.setLastModified} is called multiple times, the
        highest supplied value is honored. If that value is lower than the
        if-modified-since date in the request header, the method returns
        L{http.CACHED}.
        """
        req = http.Request(DummyChannel(), False)
        req.requestHeaders.setRawHeaders(
            networkString('if-modified-since'),
                          [b'01 Jan 1999 00:00:01 GMT']
            )
        req.setLastModified(1)

        result = req.setLastModified(0)

        self.assertEqual(result, http.CACHED) 
Example #15
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_setLastModifiedTwiceNotCached(self):
        """
        When L{http.Request.setLastModified} is called multiple times, the
        highest supplied value is honored. If that value is higher than the
        if-modified-since date in the request header, the method returns None.
        """
        req = http.Request(DummyChannel(), False)
        req.requestHeaders.setRawHeaders(
            networkString('if-modified-since'),
                          [b'01 Jan 1970 00:00:01 GMT']
            )
        req.setLastModified(1000000)

        result = req.setLastModified(0)

        self.assertEqual(result, None) 
Example #16
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_invalidContentLengthHeader(self):
        """
        If a Content-Length header with a non-integer value is received, a 400
        (Bad Request) response is sent to the client and the connection is
        closed.
        """
        processed = []
        class MyRequest(http.Request):
            def process(self):
                processed.append(self)
                self.finish()

        requestLines = [b"GET / HTTP/1.0", b"Content-Length: x", b"", b""]
        channel = self.runRequest(b"\n".join(requestLines), MyRequest, 0)
        self.assertEqual(
            channel.transport.value(),
            b"HTTP/1.1 400 Bad Request\r\n\r\n")
        self.assertTrue(channel.transport.disconnecting)
        self.assertEqual(processed, []) 
Example #17
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_invalidHeaderNoColon(self):
        """
        If a header without colon is received a 400 (Bad Request) response
        is sent to the client and the connection is closed.
        """
        processed = []
        class MyRequest(http.Request):
            def process(self):
                processed.append(self)
                self.finish()

        requestLines = [b"GET / HTTP/1.0", b"HeaderName ", b"", b""]
        channel = self.runRequest(b"\n".join(requestLines), MyRequest, 0)
        self.assertEqual(
            channel.transport.value(),
            b"HTTP/1.1 400 Bad Request\r\n\r\n")
        self.assertTrue(channel.transport.disconnecting)
        self.assertEqual(processed, []) 
Example #18
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def _checkCookie(self, expectedCookieValue, *args, **kwargs):
        """
        Call L{http.Request.setCookie} with C{*args} and C{**kwargs}, and check
        that the cookie value is equal to C{expectedCookieValue}.
        """
        channel = DummyChannel()
        req = http.Request(channel, False)
        req.addCookie(*args, **kwargs)
        self.assertEqual(req.cookies[0], expectedCookieValue)

        # Write nothing to make it produce the headers
        req.write(b"")
        writtenLines = channel.transport.written.getvalue().split(b"\r\n")

        # There should be one Set-Cookie header
        setCookieLines = [x for x in writtenLines
                          if x.startswith(b"Set-Cookie")]
        self.assertEqual(len(setCookieLines), 1)
        self.assertEqual(setCookieLines[0],
                         b"Set-Cookie: " + expectedCookieValue) 
Example #19
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_registerProducerTwiceFails(self):
        """
        Calling L{Request.registerProducer} when a producer is already
        registered raises ValueError.
        """
        req = http.Request(DummyChannel(), False)
        req.registerProducer(DummyProducer(), True)
        self.assertRaises(
            ValueError, req.registerProducer, DummyProducer(), True) 
Example #20
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_parseCookiesStripLeftSpace(self):
        """
        L{http.Request.parseCookies} strips leading whitespace in the
        cookie key.
        """
        req = http.Request(DummyChannel(), False)
        req.requestHeaders.setRawHeaders(
            b"cookie", [b' foo=bar'])
        req.parseCookies()
        self.assertEqual(
            req.received_cookies, {b'foo': b'bar'}) 
Example #21
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setResponseCodeAcceptsLongIntegers(self):
        """
        L{http.Request.setResponseCode} accepts C{long} for the code
        parameter.
        """
        req = http.Request(DummyChannel(), False)
        req.setResponseCode(long(1)) 
Example #22
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setHeader(self):
        """
        L{http.Request.setHeader} sets the value of the given response header.
        """
        req = http.Request(DummyChannel(), False)
        req.setHeader(b"test", b"lemur")
        self.assertEqual(req.responseHeaders.getRawHeaders(b"test"), [b"lemur"]) 
Example #23
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setHostSSLNonDefaultPort(self):
        """
        L{http.Request.setHost} sets the value of the host request header.
        The port should be added because it is not the default.
        """
        d = DummyChannel()
        d.transport = DummyChannel.SSL()
        req = http.Request(d, False)
        req.setHost(b"example.com", 81)
        self.assertEqual(
            req.requestHeaders.getRawHeaders(b"host"), [b"example.com:81"]) 
Example #24
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setHostNonDefaultPort(self):
        """
        L{http.Request.setHost} sets the value of the host request header.
        The port should be added because it is not the default.
        """
        req = http.Request(DummyChannel(), False)
        req.setHost(b"example.com", 81)
        self.assertEqual(
            req.requestHeaders.getRawHeaders(b"host"), [b"example.com:81"]) 
Example #25
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setHost(self):
        """
        L{http.Request.setHost} sets the value of the host request header.
        The port should not be added because it is the default.
        """
        req = http.Request(DummyChannel(), False)
        req.setHost(b"example.com", 80)
        self.assertEqual(
            req.requestHeaders.getRawHeaders(b"host"), [b"example.com"]) 
Example #26
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setLastModifiedNotCached(self):
        """
        If the resource is newer than the if-modified-since date in the request
        header, L{http.Request.setLastModified} returns None
        """
        req = http.Request(DummyChannel(), False)
        req.requestHeaders.setRawHeaders(
            networkString('if-modified-since'),
                          [b'01 Jan 1970 00:00:00 GMT']
            )

        result = req.setLastModified(1000000)

        self.assertEqual(result, None) 
Example #27
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setLastModifiedCached(self):
        """
        If the resource is older than the if-modified-since date in the request
        header, L{http.Request.setLastModified} returns L{http.CACHED}.
        """
        req = http.Request(DummyChannel(), False)
        req.requestHeaders.setRawHeaders(
            networkString('if-modified-since'),
                          [b'02 Jan 1970 00:00:00 GMT']
            )

        result = req.setLastModified(42)

        self.assertEqual(result, http.CACHED) 
Example #28
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setLastModifiedUpdate(self):
        """
        If the supplied timestamp is later than the lastModified attribute's
        value, L{http.Request.setLastModified} updates the lastModifed
        attribute.
        """
        req = http.Request(DummyChannel(), False)
        req.setLastModified(0)

        req.setLastModified(1)

        self.assertEqual(req.lastModified, 1) 
Example #29
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setLastModifiedNeverSet(self):
        """
        When no previous value was set and no 'if-modified-since' value was
        requested, L{http.Request.setLastModified} takes a timestamp in seconds
        since the epoch and sets the request's lastModified attribute.
        """
        req = http.Request(DummyChannel(), False)

        req.setLastModified(42)

        self.assertEqual(req.lastModified, 42) 
Example #30
Source File: test_http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_getHeaderNotFound(self):
        """
        L{http.Request.getHeader} returns L{None} when asked for the value of a
        request header which is not present.
        """
        req = http.Request(DummyChannel(), False)
        self.assertEqual(req.getHeader(b"test"), None)