Skip to content
This repository has been archived by the owner on Dec 17, 2020. It is now read-only.

Commit

Permalink
Refactor BasicAuthTransport to reuse as much as possible from Transport
Browse files Browse the repository at this point in the history
that enables to do a simple SafeBasicAuthTransport
  • Loading branch information
Adam Groszer committed Aug 29, 2010
1 parent e91a8a1 commit 31d5d8f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 29 deletions.
3 changes: 2 additions & 1 deletion CHANGES.txt
Expand Up @@ -5,7 +5,8 @@ CHANGES
0.5.3dev (unreleased)
---------------------

- ...
- Refactor BasicAuthTransport to reuse as much as possible from Transport
that enables to do a simple SafeBasicAuthTransport


0.5.2 (2009-02-24)
Expand Down
7 changes: 6 additions & 1 deletion buildout.cfg
@@ -1,11 +1,16 @@
[buildout]
develop = .
parts = test checker coverage
parts = test checker coverage coverage-test

[test]
recipe = zc.recipe.testrunner
eggs = z3c.json [test]

[coverage-test]
recipe = zc.recipe.testrunner
eggs = ${test:eggs}
defaults = ['--coverage', '../../coverage']

[checker]
recipe = lovely.recipe:importchecker
path = src/z3c/json
Expand Down
47 changes: 47 additions & 0 deletions src/z3c/json/tests.py
Expand Up @@ -38,6 +38,7 @@
from z3c.json.exceptions import ResponseError, ProtocolError

from z3c.json.proxy import JSONRPCProxy
from z3c.json.transport import BasicAuthTransport


def spaceless(aString):
Expand Down Expand Up @@ -546,6 +547,8 @@ def do_POST(self):

global last_request_body
last_request_body = body
global last_request_headers
last_request_headers = self.headers

global next_response_body
global next_response_status
Expand Down Expand Up @@ -596,6 +599,10 @@ def get_last_request():
global last_request_body
return last_request_body

def get_last_headers():
global last_request_headers
return last_request_headers

def serve_requests(server):
global json_test_server_stop
while not json_test_server_stop:
Expand Down Expand Up @@ -668,6 +675,46 @@ def testSimple(self):
else:
self.fail("ResponseError expected")

def testSimpleBasicAuth(self):
transport = BasicAuthTransport('user','passwd')

proxy = JSONRPCProxy('http://localhost:%d/' % self.TEST_PORT,
transport=transport)
set_next_response_json(True, "jsonrpc")
y = proxy.hello()
self.assertEqual(y, True)

x = get_last_request()
self.assertEqual(x,
"""{"params":[],"jsonrpc":"2.0","method":"hello","id":"jsonrpc"}""")
x = get_last_headers()
self.assertEqual(x['authorization'],
'Basic dXNlcjpwYXNzd2Q=')

set_next_response_json(123, "jsonrpc")
y = proxy.greeting(u'Jessy')
self.assertEqual(y, 123)

x = get_last_request()
self.assertEqual(x,
"""{"params":["Jessy"],"jsonrpc":"2.0","method":"greeting","id":"jsonrpc"}""")

set_next_response('blabla')
try:
y = proxy.hello()
except ResponseError:
pass
else:
self.fail("ResponseError expected")

set_next_response('{blabla}')
try:
y = proxy.hello()
except ResponseError:
pass
else:
self.fail("ResponseError expected")

dataToTest = [
{'response_json': True,
'call_method': 'hello',
Expand Down
33 changes: 6 additions & 27 deletions src/z3c/json/transport.py
Expand Up @@ -228,35 +228,14 @@ def __init__(self, username=None, password=None, verbose=0):
self.password=password
self.verbose=verbose

def request(self, host, handler, request_body, verbose=0):
# issue JSON-RPC request

self.verbose = verbose

h = httplib.HTTP(host)
h.putrequest("POST", handler)

# required by HTTP/1.1
h.putheader("Host", host)

# required by JSON-RPC
h.putheader("User-Agent", self.user_agent)
h.putheader("Content-Type", "application/json")
h.putheader("Content-Length", str(len(request_body)))

# basic auth
def send_content(self, connection, request_body):
# send basic auth
if self.username is not None and self.password is not None:
h.putheader("AUTHORIZATION", "Basic %s" %
connection.putheader("AUTHORIZATION", "Basic %s" %
base64.encodestring("%s:%s" % (self.username, self.password)
).replace("\012", ""))
h.endheaders()

if request_body:
h.send(request_body)

errcode, errmsg, headers = h.getreply()

if errcode != 200:
raise ProtocolError(host + handler, errcode, errmsg, headers)
Transport.send_content(self, connection, request_body)

return self.parse_response(h.getfile())
class SafeBasicAuthTransport(SafeTransport, BasicAuthTransport):
"""Basic AUTH through HTTPS"""

0 comments on commit 31d5d8f

Please sign in to comment.