Skip to content

Commit

Permalink
core link query
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanganelli committed Sep 16, 2015
1 parent 61e0c00 commit f66518f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
22 changes: 21 additions & 1 deletion coapthon/layer/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@ def discover(self, request, response):
if i == "/":
continue
resource = self._parent.root[i]
payload += self.corelinkformat(resource)
ret = self.valid(request.query, resource.attributes)
if ret:
payload += self.corelinkformat(resource)

response.payload = payload
response.content_type = defines.inv_content_types["application/link-format"]
Expand All @@ -389,6 +391,24 @@ def discover(self, request, response):
response = self._parent.message_layer.matcher_response(response)
return response

@staticmethod
def valid(query, attributes):
for q in query:
q = str(q)
assert(isinstance(q, str))
tmp = q.split("=")
if len(tmp) > 1:
k = tmp[0]
v = tmp[1]
if k in attributes:
if v == attributes[k]:
continue
else:
return False
else:
return False
return True

@staticmethod
def corelinkformat(resource):
"""
Expand Down
3 changes: 1 addition & 2 deletions coapthon/messages/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ def query(self):
value.append(option.value)
return value

@query.setter
def query(self, q):
def add_query(self, q):
"""
Adds a query.
:param q: the query
Expand Down
49 changes: 49 additions & 0 deletions plugtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,55 @@ def _test_plugtest(self, message_list):

sock.close()

# def test_td_coap_link_01(self):
# print "TD_COAP_LINK_01"
# path = "/.well-known/core"
# req = Request()
# req.code = defines.inv_codes['GET']
# req.uri_path = path
# req.type = defines.inv_types["CON"]
# req._mid = self.current_mid
# req.destination = self.server_address
#
# expected = Response()
# expected.type = defines.inv_types["ACK"]
# expected._mid = self.current_mid
# expected.code = defines.responses["CONTENT"]
# expected.token = None
# option = Option()
# option.number = defines.inv_options["Content-Type"]
# option.value = defines.inv_content_types["application/link-format"]
# expected.add_option(option)
# expected.payload = """</separate>;</large-update>;</seg1/seg2/seg3>;rt="Type1",</large>;</seg1/seg2>;rt="Type1",</test>;rt="Type1",</obs>;</seg1>;rt="Type1",</query>;rt="Type1","""
#
# self.current_mid += 1
# self._test_plugtest([(req, expected)])

def test_td_coap_link_02(self):
print "TD_COAP_LINK_02"
path = "/.well-known/core"
req = Request()
req.code = defines.inv_codes['GET']
req.uri_path = path
req.type = defines.inv_types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.add_query("rt=Type1")

expected = Response()
expected.type = defines.inv_types["ACK"]
expected._mid = self.current_mid
expected.code = defines.responses["CONTENT"]
expected.token = None
option = Option()
option.number = defines.inv_options["Content-Type"]
option.value = defines.inv_content_types["application/link-format"]
expected.add_option(option)
expected.payload = """</seg1/seg2/seg3>;rt="Type1",</seg1/seg2>;rt="Type1",</test>;rt="Type1",</seg1>;rt="Type1",</query>;rt="Type1","""

self.current_mid += 1
self._test_plugtest([(req, expected)])

def test_td_coap_core_01(self):
print "TD_COAP_CORE_01"
path = "/test"
Expand Down
2 changes: 2 additions & 0 deletions plugtest_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TestResource(Resource):
def __init__(self, name="TestResource", coap_server=None):
super(TestResource, self).__init__(name, coap_server, visible=True, observable=False, allow_children=True)
self.payload = "Test Resource"
self.attributes["rt"] = "Type1"

def render_GET(self, request):
return self
Expand All @@ -31,6 +32,7 @@ def render_POST(self, request):
if option.number == defines.inv_options["Content-Type"]:
res.payload = {option.value: request.payload}
return res

res.payload = request.payload
return res

Expand Down

0 comments on commit f66518f

Please sign in to comment.