Skip to content
Merged
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
14 changes: 10 additions & 4 deletions notecard/notecard.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class Notecard:
'os_platform': sys.platform,
'os_version': sys.version
}
_user_agent_app = None
_user_agent_sent = False

def __init__(self):
Expand All @@ -213,16 +214,21 @@ def _preprocessReq(self, req):
"""Inspect the request for hub.set and add the User Agent."""
if 'hub.set' in req.values():
# Merge the User Agent to send along with the hub.set request.
new_req = req.copy()
new_req.update(self.GetUserAgent())
req = new_req
req = req.copy()
req.update({'body': self.GetUserAgent()})

self._user_agent_sent = True
return req

def GetUserAgent(self):
"""Return the User Agent String for the host for debug purposes."""
return self._user_agent
ua_copy = self._user_agent.copy()
ua_copy.update(self._user_agent_app or {})
return ua_copy

def SetAppUserAgent(self, app_user_agent):
"""Set the User Agent info for the app."""
self._user_agent_app = app_user_agent

def UserAgentSent(self):
"""Return true if the User Agent has been sent to the Notecard."""
Expand Down
24 changes: 24 additions & 0 deletions test/test_notecard.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,27 @@ def test_debug_mode_on_i2c(self):
nCard = notecard.OpenI2C(port, 0x17, 255, debug=True)

assert nCard._debug


class MockNotecard(notecard.Notecard):
def Reset(self):
pass


class TestUserAgent:

def setUserAgentInfo(self, info=None):
nCard = MockNotecard()
orgReq = {"req": "hub.set"}
nCard.SetAppUserAgent(info)
req = nCard._preprocessReq(orgReq)
return req

def test_amends_hub_set_request(self):
req = self.setUserAgentInfo()
assert req['body'] is not None

def test_adds_app_info(self):
info = {"app": "myapp"}
req = self.setUserAgentInfo(info)
assert req['body']['app'] == 'myapp'