Commits

Anonymous committed 6b01fb6

add a little test for authentification

  • Participants
  • Parent commits 8ee768b

Comments (0)

Files changed (4)

File restclient/http.py

                                           self.status,
                                           self.final_url)
 
-
-
 class HTTPClient(object):
     """ Interface for HTTP clients """
 
 
 class Urllib2HTTPClient(HTTPClient):
     """ HTTP Client that use urllib2 """
-    def __init__(self, handler=None):
-        if handler is not None:
-            self.handler = handler
-        else:
-            self.handler = False
 
+    def __init__(self, *handlers):
+        openers = []
+        if handlers:
+            openers = [handler for handler in handlers]
+        self.openers = openers
+    
     def request(self, url, method='GET', body=None, headers=None):
         headers = headers or {}
         body = body or ''
         headers.setdefault('User-Agent',
             "%s Python-urllib/%s" % (USER_AGENT, urllib2.__version__,))
 
-        if self.handler:
-            opener = urllib2.build_opener(self.handler)
+        if self.openers:
+            opener = urllib2.build_opener(*self.openers)
             urllib2.install_opener(opener)
 
         req = CustomRequest(url=url, data=body, method=method)

File tests/_server_test.py

 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #
 
+import base64
 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
 import cgi
 import os
 
 class HTTPTestHandler(BaseHTTPRequestHandler):
 
+    def __init__(self, request, client_address, server):
+        self.auth = 'Basic ' + base64.encodestring('test:test')[:-1]
+        BaseHTTPRequestHandler.__init__(self, request, client_address, server)
+        
     def do_GET(self):
         self.parsed_uri = urlparse.urlparse(self.path)
         self.query = {}
                 self._respond(200, extra_headers, "ok")
             else:
                 self.error_Response()
+        elif path == "/auth":
+            if self.headers.get('authorization') != self.auth:
+                realm = "test"
+                self.send_response(401)
+                self.send_header('WWW-Authenticate', 'Basic realm="%s"' % realm)
+                self.end_headers()
+                return
+
         else:
             self._respond(404, 
                     [('Content-type', 'text/plain')], "Not Found" )

File tests/clients_test.py

 import unittest
 import urlparse
 
+
 from restclient.http import Urllib2HTTPClient, CurlHTTPClient, \
 HTTPLib2HTTPClient
 from restclient.rest import Resource, RestClient, RequestFailed, \

File tests/resource_test.py

 import threading
 import unittest
 import urlparse
+import urllib2
 
 from restclient.http import Urllib2HTTPClient, CurlHTTPClient, \
 HTTPLib2HTTPClient
     def setUp(self):
         httpclient = Urllib2HTTPClient()
         self.url = 'http://%s:%s' % (HOST, PORT)
-        print self.url
         self.res = Resource(self.url, httpclient)
 
     def tearDown(self):
         self.assert_(self.res.status_code == 200)
 
 
+    def testAuth(self):
+        password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
+        password_mgr.add_password(None, "%s/%s" % (self.url, "auth"),
+                "test", "test")
+        auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
 
+        httpclient = Urllib2HTTPClient(auth_handler)
+        
+        res = Resource(self.url, httpclient)
+        
     
 if __name__ == '__main__':
     from _server_test import run_server_test