Code

Ticket #2879: http_test_case.diff

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

path to resole this enhancement

Line 
1Index: django/test/http.py
2===================================================================
3--- django/test/http.py (revision 0)
4+++ django/test/http.py (revision 0)
5@@ -0,0 +1,84 @@
6+import unittest, thread, sys
7+from django.core.servers.basehttp import WSGIServer, AdminMediaHandler, WSGIServerException, WSGIRequestHandler
8+from django.core.handlers.wsgi import WSGIHandler
9+from django.conf import settings
10+from django.core.management import get_version, style
11+
12+ADDR = 'localhost'
13+PORT = 8888
14+TEST_SERVER_FAIL = 'Test server failed to start'
15+
16+class WSGIServerWrapper(WSGIServer):
17+    """
18+    Wrapper for WSGIServer.
19+   
20+    This is required for some added exception handling and to ignore an annoying unhandled
21+    exception when calling server_close() after serve_forever() has been called in a thread.
22+    """
23+    def serve_forever(self):
24+        try:
25+            WSGIServer.serve_forever(self)
26+        except WSGIServerException, e:
27+            # Use helpful error messages instead of ugly tracebacks.
28+            ERRORS = {
29+                13: "You don't have permission to access that port.",
30+                98: "That port is already in use.",
31+                99: "That IP address can't be assigned-to.",
32+            }
33+            try:
34+                error_text = ERRORS[e.args[0].args[0]]
35+            except (AttributeError, KeyError):
36+                error_text = str(e)
37+            self.error = "Error: %s" % error_text
38+        except:
39+            # This is a hacky way of dealing with the error received when the exception is triggered
40+            # from the server.server_close(). Because of this hack we must make sure the server came up
41+            # after calling serve_forever()
42+            pass
43+
44+             
45+def create_test_server(addr, port):
46+    """
47+    Method to create and return the test server object (instance of WSGIServerWrapper)
48+    """
49+    print "\nDjango version %s, on %r:%r" % (get_version(), ADDR, PORT)
50+   
51+    import django
52+    path = django.__path__[0] + '/contrib/admin/media'
53+    handler = AdminMediaHandler(WSGIHandler(), media_dir=path)
54+    server_address = (addr, port)
55+    httpd = WSGIServerWrapper(server_address, WSGIRequestHandler)
56+    httpd.set_app(handler)
57+   
58+    return httpd
59+
60+
61+class HttpTestCase(unittest.TestCase):
62+    """
63+    HTTPTestCase is intended to be used for live HTTP testing of your django applications
64+    using the django test server.
65+    """
66+   
67+    def setUp(self):
68+        # Setup server for test run and assign it to self
69+        self.server = create_test_server(ADDR, PORT)
70+        self.server_thread_id = thread.start_new_thread(self.server.serve_forever, ())
71+       
72+        print 'end of test'
73+       
74+        # Test that the server is up since we ignore any exceptions -- see above.
75+        import socket
76+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
77+        try:
78+            s.connect((ADDR, PORT))
79+            s.close()
80+        except:
81+            if hasattr(self.server, 'error'):
82+                sys.stderr.write(style(self.server.error) + '\n')
83+            else:
84+                raise TEST_SERVER_FAIL
85+
86+       
87+    def tearDown(self):
88+        #Kill the server
89+        self.server.server_close()
90\ No newline at end of file
91Index: django/test/utils.py
92===================================================================
93--- django/test/utils.py        (revision 3896)
94+++ django/test/utils.py        (working copy)
95@@ -45,10 +45,12 @@
96 def create_test_db(verbosity=1, autoclobber=False):
97     if verbosity >= 1:
98         print "Creating test database..."
99-    # If we're using SQLite, it's more convenient to test against an
100-    # in-memory database.
101+    # Check if we're using sqlite
102     if settings.DATABASE_ENGINE == "sqlite3":
103-        TEST_DATABASE_NAME = ":memory:"
104+        if settings.TEST_DATABASE_NAME:
105+            TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME
106+        else:
107+            TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME
108     else:
109         if settings.TEST_DATABASE_NAME:
110             TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME
111@@ -105,3 +107,9 @@
112         time.sleep(1) # To avoid "database is being accessed by other users" errors.
113         cursor.execute("DROP DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME))
114         connection.close()
115+    # If using an in memory sqlite database do not attempt to remove it
116+    elif TEST_DATABASE_NAME != ":memory:":
117+        from os import remove
118+        remove(TEST_DATABASE_NAME)
119+       
120+