Commits

Chris Perl committed 1412637

More refactoring, working toward unit tests for BayeuxReceiver class

  • Participants
  • Parent commits ce34fd7

Comments (0)

Files changed (2)

 		assert self._message
 		return "Bayeux Message: %s" % (json.dumps(self._message))
 
-	def json(self):
+	def as_json(self):
 		"""
 		Return the message as a json encoded string.
 		"""
 		return json.dumps(self._message)
 
-	# TODO: Add a method to return the message as a URL encoded string, for
-	# use with callback polling, if we want to support that.
+	def as_dict(self):
+		"""
+		Return the message as a python dictionary.
+		"""
+		return self._message
+
 
 # Request Messages
 class BayeuxRequestMessage(BayeuxMessage):
 	messages from the remote machine and ensures that everything remains in
 	the connected state.
 	"""
-	def __init__(self, url, listeners=[]):
+	def __init__(self, url, listeners=[], http_class=HTTPConnection):
 		threading.Thread.__init__(self)
 		p = urlparse.urlparse(url)
 		self._host = p.netloc
 		self._path = p.path
-		self._http = HTTPConnection(self._host)
+		self._http_class = http_class
+		self._http = self._http_class(self._host)
 		self._state = BayeuxStateUNCONNECTED()
 		self._reconnect = 'retry'
 		self._interval = 0
 		"""
 		Send the message m to the server and read back any response messages.
 		"""
-		http_client.request("POST", self._path, m.json(), {"Content-Type": "application/json"})
+		http_client.request("POST", self._path, m.as_json(), {"Content-Type": "application/json"})
 		resp = http_client.getresponse()
 		# TODO: Check for non 200 responses
 		data = resp.read()
 		data = json.loads(data)
-		return [ ResponseMessageFactory(x) for x in data ]
+		return [ ResponseMessageFactory(BayeuxResponseMessage.FROMDICT, x) for x in data ]
 
 	def _update_advice(self, m):
 		"""
 		"""
 		A helper command for subscribe, unsubscribe, and publish.
 		"""
-		http = HTTPConnection(self._host)
+		http = self._http_class(self._host)
 		msgs = self._send_recv(http, m)
 		event_msgs = [ x for x in msgs if isinstance(x, EventMessage) ]
 		response_msgs = [ x for x in msgs if not isinstance(x, EventMessage) ]
 			"""
 			self._payload = []
 			self.request_msg = None
+			self.response_msgs = []
 
 			# An Event so the unittest and the BayeuxReceiver and
 			# operate in lockstep
 			"server."  We simply return what payload had been set
 			for us to return.
 			"""
-			return self._payload
+			return json.dumps(self.response_msgs)
 
-		def add_payload(self, data):
+		def add_response_msg(self, m):
 			"""
-			Add to the list of messages to return.
+			Add Bayeux msg m to the list of messages that will be returned.
 			"""
-			self._payload.append(data)
+			self.response_msgs.append(m.as_dict())
 
 	def test_test(self):
 		h = TestBayeuxReceiver.MockHTTPConnection()