@@ -107,33 +107,45 @@ 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+
110117def 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+ if _FLAVOR == 'WoT' :
124+ links_or_forms = "forms"
125+
114126 assert code == 200
115127 assert body ['id' ] == 'urn:dev:ops:my-lamp-1234'
116128 assert body ['title' ] == 'My Lamp'
117129 assert body ['security' ] == 'nosec_sc'
118130 assert body ['securityDefinitions' ]['nosec_sc' ]['scheme' ] == 'nosec'
119- assert body ['@context' ] == 'https://webthings.io/schemas'
131+ # assert body['@context'] == 'https://webthings.io/schemas'
120132 assert lists_equal (body ['@type' ], ['OnOffSwitch' , 'Light' ])
121133 assert body ['description' ] == 'A web connected lamp'
122134 assert body ['properties' ]['on' ]['@type' ] == 'OnOffProperty'
123135 assert body ['properties' ]['on' ]['title' ] == 'On/Off'
124136 assert body ['properties' ]['on' ]['type' ] == 'boolean'
125137 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'
138+ assert len (body ['properties' ]['on' ][links_or_forms ]) == 1
139+ assert body ['properties' ]['on' ][links_or_forms ][0 ]['href' ] == _PATH_PREFIX + '/properties/on'
128140 assert body ['properties' ]['brightness' ]['@type' ] == 'BrightnessProperty'
129141 assert body ['properties' ]['brightness' ]['title' ] == 'Brightness'
130142 assert body ['properties' ]['brightness' ]['type' ] == 'integer'
131143 assert body ['properties' ]['brightness' ]['description' ] == 'The level of light from 0-100'
132144 assert body ['properties' ]['brightness' ]['minimum' ] == 0
133145 assert body ['properties' ]['brightness' ]['maximum' ] == 100
134146 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'
147+ assert len (body ['properties' ]['brightness' ][links_or_forms ]) == 1
148+ assert body ['properties' ]['brightness' ][links_or_forms ][0 ]['href' ] == _PATH_PREFIX + '/properties/brightness'
137149
138150 if not _SKIP_ACTIONS_EVENTS :
139151 assert body ['actions' ]['fade' ]['title' ] == 'Fade'
@@ -146,35 +158,39 @@ def run_client():
146158 assert body ['actions' ]['fade' ]['input' ]['properties' ]['duration' ]['type' ] == 'integer'
147159 assert body ['actions' ]['fade' ]['input' ]['properties' ]['duration' ]['minimum' ] == 1
148160 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'
161+ assert len (body ['actions' ]['fade' ][links_or_forms ]) == 1
162+ assert body ['actions' ]['fade' ][links_or_forms ][0 ]['href' ] == _PATH_PREFIX + '/actions/fade'
151163 assert body ['events' ]['overheated' ]['type' ] == 'number'
152164 assert body ['events' ]['overheated' ]['unit' ] == 'degree celsius'
153165 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'
166+ assert len (body ['events' ]['overheated' ][links_or_forms ]) == 1
167+ assert body ['events' ]['overheated' ][links_or_forms ][0 ]['href' ] == _PATH_PREFIX + '/events/overheated'
156168
157169 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 :]
170+ assert len (body [links_or_forms ]) >= 1
171+ if _FLAVOR == 'Webthings' :
172+ assert body [links_or_forms ][0 ]['rel' ] == 'properties'
173+ assert body [links_or_forms ][0 ]['href' ] == _PATH_PREFIX + '/properties'
174+ remaining_links = body [links_or_forms ][1 :]
162175 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 :]
176+ assert len (body [links_or_forms ]) >= 3
177+ if _FLAVOR == 'Webthings' :
178+ assert body [links_or_forms ][0 ]['rel' ] == 'properties'
179+ assert body [links_or_forms ][0 ]['href' ] == _PATH_PREFIX + '/properties'
180+ if _FLAVOR == 'Webthings' :
181+ assert body [links_or_forms ][1 ]['rel' ] == 'actions'
182+ assert body [links_or_forms ][1 ]['href' ] == _PATH_PREFIX + '/actions'
183+ if _FLAVOR == 'Webthings' :
184+ assert body [links_or_forms ][2 ]['rel' ] == 'events'
185+ assert body [links_or_forms ][2 ]['href' ] == _PATH_PREFIX + '/events'
186+ remaining_links = body [links_or_forms ][3 :]
171187
172188 if not _SKIP_WEBSOCKET :
173189 assert len (remaining_links ) >= 1
174190
175191 ws_href = None
176192 for link in remaining_links :
177- if link ['rel' ] != 'alternate' :
193+ if 'rel' in link and link ['rel' ] != 'alternate' :
178194 continue
179195
180196 if 'mediaType' in link :
@@ -189,21 +205,23 @@ def run_client():
189205
190206 # Test properties
191207 code , body = http_request ('GET' , '/properties' )
192- assert code == 200
193208 assert body ['brightness' ] == 50
194209 assert body ['on' ]
195210
196211 code , body = http_request ('GET' , '/properties/brightness' )
197212 assert code == 200
198- assert body ['brightness' ] == 50
213+ check_property_value (body , "brightness" , 50 )
214+
215+ value = 25 if _FLAVOR == "WoT" else {'brightness' : 25 }
216+
199217
200- code , body = http_request ('PUT' , '/properties/brightness' , { 'brightness' : 25 } )
218+ code , body = http_request ('PUT' , '/properties/brightness' , value )
201219 assert code == 200
202- assert body [ 'brightness' ] == 25
220+ check_property_value ( body , 'brightness' , 25 )
203221
204222 code , body = http_request ('GET' , '/properties/brightness' )
205223 assert code == 200
206- assert body [ 'brightness' ] == 25
224+ check_property_value ( body , 'brightness' , 25 )
207225
208226 if not _SKIP_ACTIONS_EVENTS :
209227 # Test events
@@ -401,7 +419,7 @@ def recv():
401419
402420 code , body = http_request ('GET' , '/properties/brightness' )
403421 assert code == 200
404- assert body [ 'brightness' ] == 10
422+ check_property_value ( body , 'brightness' , 10 )
405423
406424 if _SKIP_ACTIONS_EVENTS :
407425 return
@@ -574,6 +592,10 @@ def recv():
574592 parser .add_argument ('--debug' ,
575593 help = 'log all requests' ,
576594 action = 'store_true' )
595+ parser .add_argument ('--flavor' ,
596+ help = 'specify the protocol flavor' ,
597+ choices = ['WoT' , 'Webthings' ],
598+ default = 'Webthings' )
577599 args = parser .parse_args ()
578600
579601 if (args .protocol == 'http' and args .port == 80 ) or \
@@ -594,5 +616,6 @@ def recv():
594616 _PROTO = args .protocol
595617 _PATH_PREFIX = args .path_prefix
596618 _AUTHORIZATION_HEADER = args .auth_header
619+ _FLAVOR = args .flavor
597620
598621 exit (run_client ())
0 commit comments