[Inteproxy-commits] r133 - in trunk: . test

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Jun 10 20:13:39 CEST 2008


Author: bh
Date: 2008-06-10 20:13:39 +0200 (Tue, 10 Jun 2008)
New Revision: 133

Added:
   trunk/test/serversupport.py
   trunk/test/test_httpserver.py
Modified:
   trunk/ChangeLog
Log:
* test/serversupport.py, test/test_httpserver.py: New files.
Support code for test cases that run http servers and test cases
for the support code and inteproxy.httpserver.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-06-10 18:11:11 UTC (rev 132)
+++ trunk/ChangeLog	2008-06-10 18:13:39 UTC (rev 133)
@@ -1,5 +1,11 @@
 2008-06-10  Bernhard Herzog  <bh at intevation.de>
 
+	* test/serversupport.py, test/test_httpserver.py: New files.
+	Support code for test cases that run http servers and test cases
+	for the support code and inteproxy.httpserver.
+
+2008-06-10  Bernhard Herzog  <bh at intevation.de>
+
 	Move the http openers from the request handler class to the server
 	instance.
 

Added: trunk/test/serversupport.py
===================================================================
--- trunk/test/serversupport.py	2008-06-10 18:11:11 UTC (rev 132)
+++ trunk/test/serversupport.py	2008-06-10 18:13:39 UTC (rev 133)
@@ -0,0 +1,50 @@
+# Copyright (C) 2007, 2008 by Intevation GmbH
+# Authors:
+# Bernhard Herzog <bh at intevation.de>
+#
+# This program is free software under the GPL (>=v2)
+# Read the file COPYING coming with the software for details.
+
+"""Simple HTTP server with some static data that can be run in a thread"""
+
+import BaseHTTPServer
+
+import inteproxy.httpserver as httpserver
+
+
+class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+
+    def do_GET(self):
+        """Handle a GET request from the server's contents attribute.
+        See HTTPServer for more details.
+        """
+        for path, headers, data in self.server.contents:
+            if path == self.path:
+                self.send_response(200)
+                for header, value in headers:
+                    self.send_header(header, value)
+                self.send_header("Content-Length", len(data))
+                self.end_headers()
+                self.wfile.write(data)
+                return
+        else:
+            self.send_error(404)
+
+
+class HTTPServer(httpserver.HTTPServer):
+
+    """HTTPServer that serves a limited set of predefined contents.
+
+    The contents are described by a list of tuples of the form (PATH,
+    HEADERS, DATA), where PATH and DATA are strings describing the path
+    on the server and the data found at that path.  HEADERS should be a
+    list of (HEADER-KEYWORD, VALUE) pairs describing the HTTP-header
+    fields of the resource at PATH.  The Content-Length header field is
+    automatically added by the request handler.
+    """
+
+    def __init__(self, contents, server_address=("127.0.0.1", 0),
+                 RequestHandlerClass=HTTPRequestHandler):
+        self.contents = contents
+        httpserver.HTTPServer.__init__(self, server_address,
+                                       RequestHandlerClass)


Property changes on: trunk/test/serversupport.py
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native

Added: trunk/test/test_httpserver.py
===================================================================
--- trunk/test/test_httpserver.py	2008-06-10 18:11:11 UTC (rev 132)
+++ trunk/test/test_httpserver.py	2008-06-10 18:13:39 UTC (rev 133)
@@ -0,0 +1,55 @@
+# Copyright (C) 2007, 2008 by Intevation GmbH
+# Authors:
+# Bernhard Herzog <bh at intevation.de>
+#
+# This program is free software under the GPL (>=v2)
+# Read the file COPYING coming with the software for details.
+
+"""Tests for inteproxy.httpserver and serversupport
+
+The tests of inteproxy.httpserver are done indirectly via the server
+implemented in serversupport so that the tests server more or less as
+tests for both modules.
+"""
+
+import sys
+import os
+import httplib
+import unittest
+
+import serversupport
+
+import inteproxy.httpserver as httpserver
+
+
+class SimpleHTTPServerTests(unittest.TestCase):
+
+    server_contents = [
+        ("/wms", [("Content-Type", "text/plain")], "wms data"),
+        ]
+
+    def setUp(self):
+        self.old_stderr = sys.stderr
+        sys.stderr = open(os.path.devnull, "w")
+        server = serversupport.HTTPServer(self.server_contents)
+        self.server = httpserver.ServerThread(server)
+        self.server.start(daemon=True)
+
+    def tearDown(self):
+        try:
+            self.server.stop()
+        finally:
+            sys.stderr = self.old_stderr
+
+    def test_get_200(self):
+        http = httplib.HTTPConnection("localhost", self.server.server_port)
+        http.request("GET", "/wms")
+        response = http.getresponse()
+        self.assertEquals(response.status, 200)
+        self.assertEquals(response.read(), "wms data")
+
+    def test_get_404(self):
+        http = httplib.HTTPConnection("localhost", self.server.server_port)
+        http.request("GET", "/something/that/doesnt/exist")
+        response = http.getresponse()
+        self.assertEquals(response.status, 404)


Property changes on: trunk/test/test_httpserver.py
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native



More information about the Inteproxy-commits mailing list