Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions agithub.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class Client(object):

def __init__(self, username=None,
password=None, token=None,
connection_properties=None
connection_properties=None,
httpClient = http.client
):

# Set up connection properties
Expand All @@ -171,6 +172,11 @@ def __init__(self, username=None,
self.username = username
self.auth_header = self.hash_pass(password)

# This argument is only used by the testing harness
if httpClient is None:
raise Exception("Invalid provider for http.CLIENT Injected")
self.httpClient = httpClient

def setConnectionProperties(self, props):
'''
Initialize the connection properties. This must be called
Expand Down Expand Up @@ -271,9 +277,9 @@ def hash_pass(self, password):

def get_connection(self):
if self.prop.secure_http:
conn = http.client.HTTPSConnection(self.prop.api_url)
conn = self.httpClient.HTTPSConnection(self.prop.api_url)
elif self.auth_header is None:
conn = http.client.HTTPConnection(self.prop.api_url)
conn = self.httpClient.HTTPConnection(self.prop.api_url)
else:
raise ConnectionError(
'Refusing to authenticate over non-secure (HTTP) connection.')
Expand Down
11 changes: 11 additions & 0 deletions agithub_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import agithub
import mock
import mock.http.client
import unittest

class TestGithubObjectCreation(unittest.TestCase):
Expand Down Expand Up @@ -62,5 +63,15 @@ def test_callMethodTest(self):
, "params" : { "url" : "/path" }
})

class TestClient(unittest.TestCase):
def newClient(self, *args, **params):
return agithub.Client(*args, httpClient = mock.http.client, **params)

def test_anonymousClient(self):
client = self.newClient()

def test_passwordClient(self):
client = self.newClient(username="user", password="freepass")

if __name__ == '__main__':
unittest.main()
File renamed without changes.
Empty file added mock/http/__init__.py
Empty file.
65 changes: 65 additions & 0 deletions mock/http/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Mock module for http.client

class MethodCallRecorder(object):
method_calls = []

def recordMethodCall(self, methodName, args, params):
self.method_calls.append(MethodCall(methodName, args, params))

class MethodCall(object):
__slots__ = "method args params".split()

def __init__(self, methodName, args, params):
self.method = methodName
self.args = args
self.params = params

def __str__(self):
return {
"method": self.method,
"args" : self.args,
"parms" : self.parms
}.__str__()


class HTTPConnection(MethodCallRecorder):
connType = "HTTP"
def __init__(self, *args, **params):
self.recordMethodCall("__init__", args, params)

def request(self, *args, **parms):
self.recordMethodCall("request", args, params)

def getresponse(self, *args, **parms):
self.recordMethodCall("getresponse", args, params)
return Response()

def close(self, *args, **parms):
recordMethodCall("close", args, params)


class HTTPSConnection(HTTPConnection):
connType = "HTTPS"


class Response(MethodCallRecorder):
status = "status"

# headers and body should be overridden by test code
# if that should be tested
headers = {
"content-type": "text/plain"
}
body = "body text"

def getheader(self, *args, **params):
self.recordMethodCall("getheader", args, params)
return headers[args[0].lower()]

def getheaders(self, *args, **params):
self.recordMethodCall("getheaders", args, params)
return headers

def read(self, *args, **params):
self.recordMethodCall("read", args, params)
return self.body