Ticket #2879: http_test_case.diff

File http_test_case.diff, 4.5 KB (added by Mikeal Rogers <mikeal@…>, 18 years ago)

path to resole this enhancement

  • django/test/http.py

     
     1import unittest, thread, sys
     2from django.core.servers.basehttp import WSGIServer, AdminMediaHandler, WSGIServerException, WSGIRequestHandler
     3from django.core.handlers.wsgi import WSGIHandler
     4from django.conf import settings
     5from django.core.management import get_version, style
     6
     7ADDR = 'localhost'
     8PORT = 8888
     9TEST_SERVER_FAIL = 'Test server failed to start'
     10
     11class WSGIServerWrapper(WSGIServer):
     12    """
     13    Wrapper for WSGIServer.
     14   
     15    This is required for some added exception handling and to ignore an annoying unhandled
     16    exception when calling server_close() after serve_forever() has been called in a thread.
     17    """
     18    def serve_forever(self):
     19        try:
     20            WSGIServer.serve_forever(self)
     21        except WSGIServerException, e:
     22            # Use helpful error messages instead of ugly tracebacks.
     23            ERRORS = {
     24                13: "You don't have permission to access that port.",
     25                98: "That port is already in use.",
     26                99: "That IP address can't be assigned-to.",
     27            }
     28            try:
     29                error_text = ERRORS[e.args[0].args[0]]
     30            except (AttributeError, KeyError):
     31                error_text = str(e)
     32            self.error = "Error: %s" % error_text
     33        except:
     34            # This is a hacky way of dealing with the error received when the exception is triggered
     35            # from the server.server_close(). Because of this hack we must make sure the server came up
     36            # after calling serve_forever()
     37            pass
     38
     39             
     40def create_test_server(addr, port):
     41    """
     42    Method to create and return the test server object (instance of WSGIServerWrapper)
     43    """
     44    print "\nDjango version %s, on %r:%r" % (get_version(), ADDR, PORT)
     45   
     46    import django
     47    path = django.__path__[0] + '/contrib/admin/media'
     48    handler = AdminMediaHandler(WSGIHandler(), media_dir=path)
     49    server_address = (addr, port)
     50    httpd = WSGIServerWrapper(server_address, WSGIRequestHandler)
     51    httpd.set_app(handler)
     52   
     53    return httpd
     54
     55
     56class HttpTestCase(unittest.TestCase):
     57    """
     58    HTTPTestCase is intended to be used for live HTTP testing of your django applications
     59    using the django test server.
     60    """
     61   
     62    def setUp(self):
     63        # Setup server for test run and assign it to self
     64        self.server = create_test_server(ADDR, PORT)
     65        self.server_thread_id = thread.start_new_thread(self.server.serve_forever, ())
     66       
     67        print 'end of test'
     68       
     69        # Test that the server is up since we ignore any exceptions -- see above.
     70        import socket
     71        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     72        try:
     73            s.connect((ADDR, PORT))
     74            s.close()
     75        except:
     76            if hasattr(self.server, 'error'):
     77                sys.stderr.write(style(self.server.error) + '\n')
     78            else:
     79                raise TEST_SERVER_FAIL
     80
     81       
     82    def tearDown(self):
     83        #Kill the server
     84        self.server.server_close()
     85 No newline at end of file
  • django/test/utils.py

     
    4545def create_test_db(verbosity=1, autoclobber=False):
    4646    if verbosity >= 1:
    4747        print "Creating test database..."
    48     # If we're using SQLite, it's more convenient to test against an
    49     # in-memory database.
     48    # Check if we're using sqlite
    5049    if settings.DATABASE_ENGINE == "sqlite3":
    51         TEST_DATABASE_NAME = ":memory:"
     50        if settings.TEST_DATABASE_NAME:
     51            TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME
     52        else:
     53            TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME
    5254    else:
    5355        if settings.TEST_DATABASE_NAME:
    5456            TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME
     
    105107        time.sleep(1) # To avoid "database is being accessed by other users" errors.
    106108        cursor.execute("DROP DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME))
    107109        connection.close()
     110    # If using an in memory sqlite database do not attempt to remove it
     111    elif TEST_DATABASE_NAME != ":memory:":
     112        from os import remove
     113        remove(TEST_DATABASE_NAME)
     114       
     115           
Back to Top