Ticket #6135: 6135.patch

File 6135.patch, 4.5 KB (added by Kai Zhang, 9 years ago)

Add tests for twisted.web.http.HTTPFactory.

  • twisted/web/test/test_http.py

     
    55Test HTTP support.
    66"""
    77
     8import __builtin__
    89import random, cgi, base64
     10import os
    911
    1012try:
    1113    from urlparse import (
     
    1416    from urllib.parse import (
    1517        ParseResultBytes, urlparse, urlunsplit, clear_cache)
    1618
     19from twisted.python import log
    1720from twisted.python.compat import _PY3, iterbytes, networkString, unicode, intToBytes
    1821from twisted.python.failure import Failure
    1922from twisted.trial import unittest
     
    19231926              b"Version: HTTP/1.1",
    19241927              b"Request: /foo",
    19251928              b"'''\n4\ndefg'''\n")])
     1929
     1930
     1931
     1932class FakeFile(object):
     1933    """
     1934    A fake file-like object that acts enough like a file for L{HTTPFactory}.
     1935    """
     1936
     1937    def __init__(self):
     1938        self.outchunks = []
     1939        self.closed = False
     1940
     1941
     1942    def write(self, chunk):
     1943        """
     1944        Append the given item to the 'outchunks' list.
     1945        """
     1946        if self.closed:
     1947            raise IOError("the file was closed")
     1948        self.outchunks.append(chunk)
     1949
     1950
     1951    def close(self):
     1952        """
     1953        Set the 'closed' flag to True, explicitly marking that it has been
     1954        closed.
     1955        """
     1956        self.closed = True
     1957
     1958
     1959
     1960def _fakeOpen(path, mode, buffering):
     1961    """
     1962    A fake open method.
     1963
     1964    @param path: The path of the file to open.
     1965    @type path: C{str}.
     1966
     1967    @param mode: The mode of the file to be opened.
     1968    @type mode: C{str}.
     1969
     1970    @param buffering: Specifies the file's desired buffer size: 0 means
     1971        unbuffered, 1 means line buffered, any other positive value means use a
     1972        buffer of (approximately) that size (in bytes).
     1973    @type mode: C{int}.
     1974
     1975    @return fakeFile: A L{FakeFile} instance.
     1976    """
     1977    fakeFile = FakeFile()
     1978    fakeFile.path = path
     1979    fakeFile.mode = mode
     1980    fakeFile.buffering = buffering
     1981    return fakeFile
     1982
     1983
     1984
     1985class HTTPFactoryTests(unittest.TestCase):
     1986    """
     1987    Tests for L{twisted.web.http.HTTPFactory}.
     1988    """
     1989    def test_buildProtocol(self):
     1990        """
     1991        L{HTTPFactory.buildProtocol} returns a L{HTTPChannel} instance and set
     1992        timeOut as its attribute.
     1993        """
     1994        httpFactory = http.HTTPFactory()
     1995        p = httpFactory.buildProtocol(None)
     1996        self.assertIsInstance(p, http.HTTPChannel)
     1997        self.assertIdentical(p.factory, httpFactory)
     1998        self.assertEqual(p.timeOut, httpFactory.timeOut)
     1999
     2000
     2001    def test_buildProtocolWithTimeOut(self):
     2002        """
     2003        L{HTTPFactory.buildProtocol} returns a L{HTTPChannel} instance and set
     2004        timeOut to the value user supplied when create the L{HTTPFactory}.
     2005        """
     2006        timeOut = 10
     2007        httpFactory = http.HTTPFactory(timeout=timeOut)
     2008        self.assertEqual(httpFactory.timeOut, timeOut)
     2009        p = httpFactory.buildProtocol(None)
     2010        self.assertEqual(p.timeOut, httpFactory.timeOut)
     2011
     2012
     2013    def test_startFactory(self):
     2014        """
     2015        L{HTTPFactory.startFactory} sets up request logging.
     2016        """
     2017        httpFactory = http.HTTPFactory()
     2018        httpFactory.startFactory()
     2019        self.assertIdentical(httpFactory.logFile, log.logfile)
     2020        httpFactory.stopFactory()
     2021
     2022
     2023    def test_startFactoryWithLogPath(self):
     2024        """
     2025        L{HTTPFactory.startFactory} opens file in C{logPath}, which is
     2026        supplied when the {HTTPFactory} is created, as the log file.
     2027        """
     2028        _old_open = __builtin__.open
     2029        __builtin__.open = _fakeOpen
     2030        logPath = "path/to/log/file"
     2031        httpFactory = http.HTTPFactory(logPath=logPath)
     2032        httpFactory.startFactory()
     2033        self.assertEqual(httpFactory.logFile.path, os.path.abspath(logPath))
     2034        self.assertEqual(httpFactory.logFile.mode, "a")
     2035        self.assertEqual(httpFactory.logFile.buffering, 1)
     2036        httpFactory.stopFactory()
     2037        __builtin__.open = _old_open
     2038
     2039
     2040    def test_stopFactoryWithLogFileClosed(self):
     2041        """
     2042        L{HTTPFactory.stopFactory} closes the C{logFile} if it is not
     2043        L{twisted.python.log.logFile}
     2044        """
     2045        _old_open = __builtin__.open
     2046        __builtin__.open = _fakeOpen
     2047        logPath = "path/to/log/file"
     2048        httpFactory = http.HTTPFactory(logPath=logPath)
     2049        httpFactory.startFactory()
     2050        logFile = httpFactory.logFile
     2051        httpFactory.stopFactory()
     2052        self.assertTrue(logFile.closed)
     2053        __builtin__.open = _old_open