[Inteproxy-commits] r194 - in trunk: . inteproxy test

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Sep 10 21:28:05 CEST 2009


Author: bh
Date: 2009-09-10 21:28:04 +0200 (Thu, 10 Sep 2009)
New Revision: 194

Added:
   trunk/test/test_inteproxy_url.py
Modified:
   trunk/ChangeLog
   trunk/inteproxy/config.py
   trunk/inteproxy/transcoder.py
   trunk/test/test_config.py
Log:
* inteproxy/config.py (HostEntry, InteProxyRule): Replace
HostEntry with the more elaborate class InteProxyRule which can
handle more parts of an url
(parse_inteproxy_url): New. Creates an InteProxyRule from an
InteProxy URL
(host_desc): In InteProxyRule the classname is called scheme
(read_config): Parse the inteproxy-rules section.  Use
InteProxyRule when parsing the old host sections

* inteproxy/transcoder.py (TranscoderMap.add_hosts): Adapt to new
InteProxyRule class

* test/test_config.py (TestReadConfigProxyAndOldHosts.test_readconfig):
Adapt to InteProxyRule class
(TestReadConfigNewHosts): Test for the new inteproxy-rules section

* test/test_inteproxy_url.py: New. testcases for the inteproxy url
parser


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-09-10 19:05:48 UTC (rev 193)
+++ trunk/ChangeLog	2009-09-10 19:28:04 UTC (rev 194)
@@ -1,5 +1,26 @@
 2009-09-10  Bernhard Herzog  <bh at intevation.de>
 
+	* inteproxy/config.py (HostEntry, InteProxyRule): Replace
+	HostEntry with the more elaborate class InteProxyRule which can
+	handle more parts of an url
+	(parse_inteproxy_url): New. Creates an InteProxyRule from an
+	InteProxy URL
+	(host_desc): In InteProxyRule the classname is called scheme
+	(read_config): Parse the inteproxy-rules section.  Use
+	InteProxyRule when parsing the old host sections
+
+	* inteproxy/transcoder.py (TranscoderMap.add_hosts): Adapt to new
+	InteProxyRule class
+
+	* test/test_config.py (TestReadConfigProxyAndOldHosts.test_readconfig):
+	Adapt to InteProxyRule class
+	(TestReadConfigNewHosts): Test for the new inteproxy-rules section
+
+	* test/test_inteproxy_url.py: New. testcases for the inteproxy url
+	parser
+
+2009-09-10  Bernhard Herzog  <bh at intevation.de>
+
 	* inteproxy/transcoder.py: Remove unused import
 
 2009-09-10  Bernhard Herzog  <bh at intevation.de>

Modified: trunk/inteproxy/config.py
===================================================================
--- trunk/inteproxy/config.py	2009-09-10 19:05:48 UTC (rev 193)
+++ trunk/inteproxy/config.py	2009-09-10 19:28:04 UTC (rev 194)
@@ -1,4 +1,4 @@
-# Copyright (C) 2007, 2008 by Intevation GmbH
+# Copyright (C) 2007, 2008, 2009 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
 #
@@ -8,6 +8,8 @@
 """Read the InteProxy config file."""
 
 import sys
+import urlparse
+from urllib import quote_plus, splitport, splituser, splitpasswd
 from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
 
 
@@ -35,27 +37,64 @@
         return self.default is not nodefault
 
 
-class HostEntry(object):
+class InteProxyRule(object):
 
-    def __init__(self, host, path, classname):
+    def __init__(self, scheme, host, path, query="", fragment="",
+                 port=None, user=None, password=None):
+        self.scheme = scheme
         self.host = host
         self.path = path
-        self.classname = classname
+        self.query = query
+        self.fragment = fragment
+        self.port = port
+        self.user = user
+        self.password = password
 
     def __repr__(self):
-        return "Host(%r, %r, %r)" % (self.host, self.path, self.classname)
+        return ("InteProxyRule(%r, %r, %r, query=%r, fragment=%r, port=%r,"
+                " user=%r, password=%r)"
+                % (self.scheme, self.host, self.path, self.query, self.fragment,
+                   self.port, self.user, self.password))
 
     def __eq__(self, other):
-        return (self.host == other.host
+        return (self.scheme == other.scheme
+                and self.host == other.host
                 and self.path == other.path
-                and self.classname == other.classname)
+                and self.query == other.query
+                and self.fragment == other.fragment
+                and self.port == other.port
+                and self.user == other.user
+                and self.password == other.password)
 
     def __lt__(self, other):
-        return (self.host < other.host
-                or self.path < other.path
-                or self.classname < other.classname)
+        for attr in ["scheme", "host", "path", "query", "fragment", "port",
+                     "user", "password"]:
+            this_value = getattr(self, attr)
+            other_value = getattr(other, attr)
+            if this_value == other_value:
+                continue
+            else:
+                return this_value < other_value
 
 
+
+def parse_inteproxy_url(url):
+    real_scheme, rest = url.split(":", 1)
+    scheme, netloc, path, query, fragment = urlparse.urlsplit("http:" + rest)
+    userpass, hostport = splituser(netloc)
+    if userpass is not None:
+        user, password = splitpasswd(userpass)
+    else:
+        user = password = None
+    host, port = splitport(hostport)
+    if port is not None:
+        port = int(port)
+    return InteProxyRule(scheme=real_scheme, host=host, port=port,
+                         path=path, query=query, fragment=fragment, user=user,
+                         password=password)
+
+
+
 inteproxy_desc = [Option("http_proxy", default=None),
                   Option("https_proxy", default=None)]
 
@@ -66,7 +105,7 @@
 
 host_desc = [Option("host"),
              Option("path"),
-             Option("class", attribute="classname")]
+             Option("class", attribute="scheme")]
 
 
 def read_config_section(parser, section, item_desc, defaults=None,
@@ -91,6 +130,8 @@
     return settings_class(**options)
 
 
+rules_section_name = "inteproxy-rules"
+
 def read_config(filename):
     """Reads the InteProxy configuration from the file given by filename.
     """
@@ -112,9 +153,17 @@
             sections.discard(section)
 
     config.hosts = []
+
+    if rules_section_name in sections:
+        sections.discard(rules_section_name)
+        raw_urls = parser.get(rules_section_name, "urls")
+        for url in raw_urls.splitlines():
+            if url:
+                config.hosts.append(parse_inteproxy_url(url))
+
     for section in sections:
         config.hosts.append(read_config_section(parser, section, host_desc,
-                                                settings_class=HostEntry))
+                                                settings_class=InteProxyRule))
 
 
     return config

Modified: trunk/inteproxy/transcoder.py
===================================================================
--- trunk/inteproxy/transcoder.py	2009-09-10 19:05:48 UTC (rev 193)
+++ trunk/inteproxy/transcoder.py	2009-09-10 19:28:04 UTC (rev 194)
@@ -206,7 +206,7 @@
 
     def add_hosts(self, hosts):
         for entry in hosts:
-            self.add_host(entry.host, entry.path, entry.classname)
+            self.add_host(entry.host, entry.path, entry.scheme)
 
     def lookup(self, method, host, path):
         """Returns the python class implementing the transcoder for path on host

Modified: trunk/test/test_config.py
===================================================================
--- trunk/test/test_config.py	2009-09-10 19:05:48 UTC (rev 193)
+++ trunk/test/test_config.py	2009-09-10 19:28:04 UTC (rev 194)
@@ -1,4 +1,4 @@
-# Copyright (C) 2008 by Intevation GmbH
+# Copyright (C) 2008, 2009 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
 #
@@ -15,7 +15,7 @@
 from filesupport import FileTestMixin
 from support import AttributeTestMixin
 
-from inteproxy.config import read_config, HostEntry
+from inteproxy.config import read_config, InteProxyRule
 
 
 class ReadConfigTest(unittest.TestCase, FileTestMixin, AttributeTestMixin):
@@ -86,7 +86,7 @@
                               host="localhost", port=8080,
                               username="john", password="secret")
 
-class TestReadConfigProxyAndHosts(ReadConfigTest):
+class TestReadConfigProxyAndOldHosts(ReadConfigTest):
 
     config_contents = """\
 [inteproxy]
@@ -116,7 +116,35 @@
                               host="localhost", port=8080,
                               username="john", password="secret")
         self.assertEquals(sorted(config.hosts),
-                          [HostEntry("inteproxy-demo.intevation.org",
-                                     "/cgi-bin/frida-wms", "owsproxy"),
-                           HostEntry("wms.example.org", "/wms", "basicauth"),
+                          [InteProxyRule("basicauth",
+                                         "wms.example.org",
+                                         "/wms"),
+                           InteProxyRule("owsproxy",
+                                         "inteproxy-demo.intevation.org",
+                                         "/cgi-bin/frida-wms"),
                            ])
+
+
+class TestReadConfigNewHosts(ReadConfigTest):
+
+    config_contents = """\
+[inteproxy-rules]
+urls=owsproxy://inteproxy-demo.intevation.org/cgi-bin/frida-wms
+     basicauth://wms.example.org/wms
+# comments are allowed
+     basicauth://bob:tomato@data.intevation.*/
+"""
+
+    def test_readconfig(self):
+        config = read_config(self.config_file)
+        self.check_attributes(config, http_proxy=None, https_proxy=None)
+        self.assertEquals(sorted(config.hosts),
+                          [InteProxyRule("basicauth", "data.intevation.*", "/",
+                                         user="bob", password="tomato"),
+                           InteProxyRule("basicauth",
+                                         "wms.example.org",
+                                         "/wms"),
+                           InteProxyRule("owsproxy",
+                                         "inteproxy-demo.intevation.org",
+                                         "/cgi-bin/frida-wms"),
+                           ])

Added: trunk/test/test_inteproxy_url.py
===================================================================
--- trunk/test/test_inteproxy_url.py	2009-09-10 19:05:48 UTC (rev 193)
+++ trunk/test/test_inteproxy_url.py	2009-09-10 19:28:04 UTC (rev 194)
@@ -0,0 +1,64 @@
+# Copyright (C) 2009 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.transcoder.parse_inteproxy_url"""
+
+import unittest
+
+import support
+
+from inteproxy.config import parse_inteproxy_url
+
+
+class TestParseInteProxyUrl(unittest.TestCase):
+
+    rule_attributes = ["scheme", "host", "port", "path",
+                       ("query", ""), ("fragment", ""),
+                       "user", "password"]
+
+    def check_parse_result(self, url, **expected):
+        rule = parse_inteproxy_url(url)
+        for desc in self.rule_attributes:
+            if isinstance(desc, tuple):
+                attribute, default = desc
+            else:
+                attribute = desc
+                default = None
+            self.assertEquals(getattr(rule, attribute),
+                              expected.get(attribute, default))
+
+    def test_owsproxy_no_user_no_port_no_query_no_fragment(self):
+        self.check_parse_result("owsproxy://example.com/a/directory",
+                                scheme="owsproxy", host="example.com",
+                                path="/a/directory")
+
+    def test_owsproxy_no_user_no_port_query_fragment(self):
+        self.check_parse_result("owsproxy://localhost/wms?method=get#baz",
+                                scheme="owsproxy", host="localhost",
+                                path="/wms", query="method=get", fragment="baz")
+
+    def test_owsproxy_no_user_port_no_query_no_fragment(self):
+        self.check_parse_result("owsproxy://localhost:123/wms",
+                                scheme="owsproxy", host="localhost", port=123,
+                                path="/wms")
+
+    def test_owsproxy_user_no_passwd_port_no_query_no_fragment(self):
+        self.check_parse_result("owsproxy://bob@localhost:123/wms",
+                                scheme="owsproxy", host="localhost", port=123,
+                                path="/wms", user="bob")
+
+    def test_owsproxy_user_passwd_port_no_query_no_fragment(self):
+        self.check_parse_result("owsproxy://bob:tomato@localhost:123/my/wms",
+                                scheme="owsproxy", host="localhost", port=123,
+                                path="/my/wms", user="bob", password="tomato")
+
+    def test_owsproxy_quoteduser_quotedpasswd_port_no_query_no_fragment(self):
+        self.check_parse_result("owsproxy://bob%40example.com:tom%25to"
+                                "@localhost:1024/wms",
+                                scheme="owsproxy", host="localhost", port=1024,
+                                path="/wms",
+                                user="bob at example.com", password="tom%to")


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



More information about the Inteproxy-commits mailing list