Skip to content

Commit 70a07a7

Browse files
committed
Add a --flavor option to support WoT in the tester
1 parent 551388e commit 70a07a7

File tree

1 file changed

+55
-30
lines changed

1 file changed

+55
-30
lines changed

test-client.py

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -107,33 +107,47 @@ def lists_equal(a, b):
107107
return len(intersection) == len(a)
108108

109109

110+
def check_property_value(body, prop, value):
111+
if _FLAVOR == 'Webthings':
112+
assert body[prop] == value
113+
else:
114+
assert body == value
115+
116+
110117
def run_client():
111118
"""Test the web thing server."""
112119
# Test thing description
113120
code, body = http_request('GET', '/')
121+
122+
links_or_forms = 'links'
123+
media_type = "mediaType"
124+
if _FLAVOR == 'WoT':
125+
links_or_forms = "forms"
126+
media_type = "type"
127+
114128
assert code == 200
115129
assert body['id'] == 'urn:dev:ops:my-lamp-1234'
116130
assert body['title'] == 'My Lamp'
117131
assert body['security'] == 'nosec_sc'
118132
assert body['securityDefinitions']['nosec_sc']['scheme'] == 'nosec'
119-
assert body['@context'] == 'https://webthings.io/schemas'
133+
# assert body['@context'] == 'https://webthings.io/schemas'
120134
assert lists_equal(body['@type'], ['OnOffSwitch', 'Light'])
121135
assert body['description'] == 'A web connected lamp'
122136
assert body['properties']['on']['@type'] == 'OnOffProperty'
123137
assert body['properties']['on']['title'] == 'On/Off'
124138
assert body['properties']['on']['type'] == 'boolean'
125139
assert body['properties']['on']['description'] == 'Whether the lamp is turned on'
126-
assert len(body['properties']['on']['links']) == 1
127-
assert body['properties']['on']['links'][0]['href'] == _PATH_PREFIX + '/properties/on'
140+
assert len(body['properties']['on'][links_or_forms]) == 1
141+
assert body['properties']['on'][links_or_forms][0]['href'] == _PATH_PREFIX + '/properties/on'
128142
assert body['properties']['brightness']['@type'] == 'BrightnessProperty'
129143
assert body['properties']['brightness']['title'] == 'Brightness'
130144
assert body['properties']['brightness']['type'] == 'integer'
131145
assert body['properties']['brightness']['description'] == 'The level of light from 0-100'
132146
assert body['properties']['brightness']['minimum'] == 0
133147
assert body['properties']['brightness']['maximum'] == 100
134148
assert body['properties']['brightness']['unit'] == 'percent'
135-
assert len(body['properties']['brightness']['links']) == 1
136-
assert body['properties']['brightness']['links'][0]['href'] == _PATH_PREFIX + '/properties/brightness'
149+
assert len(body['properties']['brightness'][links_or_forms]) == 1
150+
assert body['properties']['brightness'][links_or_forms][0]['href'] == _PATH_PREFIX + '/properties/brightness'
137151

138152
if not _SKIP_ACTIONS_EVENTS:
139153
assert body['actions']['fade']['title'] == 'Fade'
@@ -146,39 +160,43 @@ def run_client():
146160
assert body['actions']['fade']['input']['properties']['duration']['type'] == 'integer'
147161
assert body['actions']['fade']['input']['properties']['duration']['minimum'] == 1
148162
assert body['actions']['fade']['input']['properties']['duration']['unit'] == 'milliseconds'
149-
assert len(body['actions']['fade']['links']) == 1
150-
assert body['actions']['fade']['links'][0]['href'] == _PATH_PREFIX + '/actions/fade'
163+
assert len(body['actions']['fade'][links_or_forms]) == 1
164+
assert body['actions']['fade'][links_or_forms][0]['href'] == _PATH_PREFIX + '/actions/fade'
151165
assert body['events']['overheated']['type'] == 'number'
152166
assert body['events']['overheated']['unit'] == 'degree celsius'
153167
assert body['events']['overheated']['description'] == 'The lamp has exceeded its safe operating temperature'
154-
assert len(body['events']['overheated']['links']) == 1
155-
assert body['events']['overheated']['links'][0]['href'] == _PATH_PREFIX + '/events/overheated'
168+
assert len(body['events']['overheated'][links_or_forms]) == 1
169+
assert body['events']['overheated'][links_or_forms][0]['href'] == _PATH_PREFIX + '/events/overheated'
156170

157171
if _SKIP_ACTIONS_EVENTS:
158-
assert len(body['links']) >= 1
159-
assert body['links'][0]['rel'] == 'properties'
160-
assert body['links'][0]['href'] == _PATH_PREFIX + '/properties'
161-
remaining_links = body['links'][1:]
172+
assert len(body[links_or_forms]) >= 1
173+
if _FLAVOR == 'Webthings':
174+
assert body[links_or_forms][0]['rel'] == 'properties'
175+
assert body[links_or_forms][0]['href'] == _PATH_PREFIX + '/properties'
176+
remaining_links = body[links_or_forms][1:]
162177
else:
163-
assert len(body['links']) >= 3
164-
assert body['links'][0]['rel'] == 'properties'
165-
assert body['links'][0]['href'] == _PATH_PREFIX + '/properties'
166-
assert body['links'][1]['rel'] == 'actions'
167-
assert body['links'][1]['href'] == _PATH_PREFIX + '/actions'
168-
assert body['links'][2]['rel'] == 'events'
169-
assert body['links'][2]['href'] == _PATH_PREFIX + '/events'
170-
remaining_links = body['links'][3:]
178+
assert len(body[links_or_forms]) >= 3
179+
if _FLAVOR == 'Webthings':
180+
assert body[links_or_forms][0]['rel'] == 'properties'
181+
assert body[links_or_forms][0]['href'] == _PATH_PREFIX + '/properties'
182+
if _FLAVOR == 'Webthings':
183+
assert body[links_or_forms][1]['rel'] == 'actions'
184+
assert body[links_or_forms][1]['href'] == _PATH_PREFIX + '/actions'
185+
if _FLAVOR == 'Webthings':
186+
assert body[links_or_forms][2]['rel'] == 'events'
187+
assert body[links_or_forms][2]['href'] == _PATH_PREFIX + '/events'
188+
remaining_links = body[links_or_forms][3:]
171189

172190
if not _SKIP_WEBSOCKET:
173191
assert len(remaining_links) >= 1
174192

175193
ws_href = None
176194
for link in remaining_links:
177-
if link['rel'] != 'alternate':
195+
if 'rel' in link and link['rel'] != 'alternate':
178196
continue
179197

180-
if 'mediaType' in link:
181-
assert link['mediaType'] == 'text/html'
198+
if media_type in link:
199+
assert link[media_type] == 'text/html'
182200
assert link['href'] == _PATH_PREFIX
183201
else:
184202
proto = 'wss' if _PROTO == 'https' else 'ws'
@@ -189,21 +207,23 @@ def run_client():
189207

190208
# Test properties
191209
code, body = http_request('GET', '/properties')
192-
assert code == 200
193210
assert body['brightness'] == 50
194211
assert body['on']
195212

196213
code, body = http_request('GET', '/properties/brightness')
197214
assert code == 200
198-
assert body['brightness'] == 50
215+
check_property_value(body, "brightness", 50)
216+
217+
value = 25 if _FLAVOR == "WoT" else {'brightness': 25}
218+
199219

200-
code, body = http_request('PUT', '/properties/brightness', {'brightness': 25})
220+
code, body = http_request('PUT', '/properties/brightness', value)
201221
assert code == 200
202-
assert body['brightness'] == 25
222+
check_property_value(body, 'brightness', 25)
203223

204224
code, body = http_request('GET', '/properties/brightness')
205225
assert code == 200
206-
assert body['brightness'] == 25
226+
check_property_value(body, 'brightness', 25)
207227

208228
if not _SKIP_ACTIONS_EVENTS:
209229
# Test events
@@ -401,7 +421,7 @@ def recv():
401421

402422
code, body = http_request('GET', '/properties/brightness')
403423
assert code == 200
404-
assert body['brightness'] == 10
424+
check_property_value(body, 'brightness', 10)
405425

406426
if _SKIP_ACTIONS_EVENTS:
407427
return
@@ -574,6 +594,10 @@ def recv():
574594
parser.add_argument('--debug',
575595
help='log all requests',
576596
action='store_true')
597+
parser.add_argument('--flavor',
598+
help='specify the protocol flavor',
599+
choices=['WoT', 'Webthings'],
600+
default='Webthings')
577601
args = parser.parse_args()
578602

579603
if (args.protocol == 'http' and args.port == 80) or \
@@ -594,5 +618,6 @@ def recv():
594618
_PROTO = args.protocol
595619
_PATH_PREFIX = args.path_prefix
596620
_AUTHORIZATION_HEADER = args.auth_header
621+
_FLAVOR = args.flavor
597622

598623
exit(run_client())

0 commit comments

Comments
 (0)