9
9
import jsonpointer
10
10
import jsonpatch
11
11
12
+
12
13
def load_json (json_string ):
13
14
try :
14
15
return json .loads (json_string )
15
16
except ValueError , e :
16
17
raise ValueError ("Could not parse '%s' as JSON: %s" % (json_string , e ))
17
18
19
+
18
20
def _with_json (f ):
19
21
@wraps (f )
20
22
def wrapper (self , json_string , * args , ** kwargs ):
21
23
return json .dumps (
22
- f (self , load_json (json_string ), * args , ** kwargs ))
24
+ f (self , load_json (json_string ), * args , ** kwargs ))
23
25
return wrapper
24
26
27
+
25
28
class HTTP :
26
29
"""
27
30
HttpLibrary for Robot Framework
@@ -95,17 +98,18 @@ def post_process_request(self, response):
95
98
# check flag set by "Next Request Should Succeed"
96
99
if next_request_should == True :
97
100
assert int (self .response .status [0 :3 ]) < 400 , \
98
- 'Request should have succeeded, but was "%s".' % \
99
- self .response .status
101
+ 'Request should have succeeded, but was "%s".' % \
102
+ self .response .status
100
103
101
104
# check flag set by "Next Request Should Not Succeed"
102
105
elif next_request_should == False :
103
106
assert int (self .response .status [0 :3 ]) >= 400 , \
104
- 'Request should not have succeeded, but was "%s".' % \
105
- self .response .status
107
+ 'Request should not have succeeded, but was "%s".' % \
108
+ self .response .status
106
109
107
110
elif next_request_should :
108
- self ._http .response_status_code_should_equal (next_request_should )
111
+ self ._http .response_status_code_should_equal (
112
+ next_request_should )
109
113
110
114
ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
111
115
@@ -127,7 +131,8 @@ def app(self):
127
131
@property
128
132
def response (self ):
129
133
if not self .context .response :
130
- raise Exception ('No request available, use e.g. GET to create one.' )
134
+ raise Exception (
135
+ 'No request available, use e.g. GET to create one.' )
131
136
return self .context .response
132
137
133
138
def _path_from_url_or_path (self , url_or_path ):
@@ -141,7 +146,7 @@ def _path_from_url_or_path(self, url_or_path):
141
146
return parsed_url .path
142
147
143
148
raise Exception ('"%s" needs to be in form of "/path" or "http://host/path"'
144
- % url_or_path )
149
+ % url_or_path )
145
150
146
151
# setup
147
152
@@ -160,7 +165,8 @@ def create_http_context(self, host=None, scheme='http'):
160
165
`scheme` the protocol scheme to use. Valid values are 'http', 'https'
161
166
"""
162
167
163
- assert scheme in ('http' , 'https' ), "`scheme` parameter must be 'http' or 'https'"
168
+ assert scheme in (
169
+ 'http' , 'https' ), "`scheme` parameter must be 'http' or 'https'"
164
170
165
171
if host == None :
166
172
host = self .context .app .host
@@ -187,10 +193,11 @@ def http_request(self, verb, url):
187
193
path = self ._path_from_url_or_path (url )
188
194
189
195
self .context .pre_process_request ()
190
- logger .debug ("Performing %s request on %s://%s%s" % (verb , self .context ._scheme , self .app .host , path ,))
196
+ logger .debug ("Performing %s request on %s://%s%s" % (verb ,
197
+ self .context ._scheme , self .app .host , path ,))
191
198
self .context .post_process_request (
192
199
self .context .app .request (path , {}, self .context .request_headers ,
193
- method = verb .upper (),)
200
+ method = verb .upper (),)
194
201
)
195
202
196
203
def HEAD (self , url ):
@@ -201,9 +208,10 @@ def HEAD(self, url):
201
208
"""
202
209
path = self ._path_from_url_or_path (url )
203
210
self .context .pre_process_request ()
204
- logger .debug ("Performing HEAD request on %s://%s%s" % (self .context ._scheme , self .app .host , path ,))
211
+ logger .debug ("Performing HEAD request on %s://%s%s" % (
212
+ self .context ._scheme , self .app .host , path ,))
205
213
self .context .post_process_request (
206
- self .app .head (path , self .context .request_headers )
214
+ self .app .head (path , self .context .request_headers )
207
215
)
208
216
209
217
def GET (self , url ):
@@ -214,9 +222,10 @@ def GET(self, url):
214
222
"""
215
223
path = self ._path_from_url_or_path (url )
216
224
self .context .pre_process_request ()
217
- logger .debug ("Performing GET request on %s://%s%s" % (self .context ._scheme , self .app .host , path ))
225
+ logger .debug ("Performing GET request on %s://%s%s" % (
226
+ self .context ._scheme , self .app .host , path ))
218
227
self .context .post_process_request (
219
- self .app .get (path , {}, self .context .request_headers )
228
+ self .app .get (path , {}, self .context .request_headers )
220
229
)
221
230
222
231
def POST (self , url ):
@@ -228,11 +237,14 @@ def POST(self, url):
228
237
path = self ._path_from_url_or_path (url )
229
238
kwargs = {}
230
239
if 'Content-Type' in self .context .request_headers :
231
- kwargs ['content_type' ] = self .context .request_headers ['Content-Type' ]
232
- logger .debug ("Performing POST request on %s://%s%s" % (self .context ._scheme , self .app .host , url ))
240
+ kwargs [
241
+ 'content_type' ] = self .context .request_headers ['Content-Type' ]
242
+ logger .debug ("Performing POST request on %s://%s%s" % (
243
+ self .context ._scheme , self .app .host , url ))
233
244
self .context .pre_process_request ()
234
245
self .context .post_process_request (
235
- self .app .post (path , self .context .request_body or {}, self .context .request_headers , ** kwargs )
246
+ self .app .post (path , self .context .request_body or {},
247
+ self .context .request_headers , ** kwargs )
236
248
)
237
249
238
250
def PUT (self , url ):
@@ -244,11 +256,14 @@ def PUT(self, url):
244
256
path = self ._path_from_url_or_path (url )
245
257
kwargs = {}
246
258
if 'Content-Type' in self .context .request_headers :
247
- kwargs ['content_type' ] = self .context .request_headers ['Content-Type' ]
259
+ kwargs [
260
+ 'content_type' ] = self .context .request_headers ['Content-Type' ]
248
261
self .context .pre_process_request ()
249
- logger .debug ("Performing PUT request on %s://%s%s" % (self .context ._scheme , self .app .host , url ))
262
+ logger .debug ("Performing PUT request on %s://%s%s" % (
263
+ self .context ._scheme , self .app .host , url ))
250
264
self .context .post_process_request (
251
- self .app .put (path , self .context .request_body or {}, self .context .request_headers , ** kwargs )
265
+ self .app .put (path , self .context .request_body or {},
266
+ self .context .request_headers , ** kwargs )
252
267
)
253
268
254
269
def DELETE (self , url ):
@@ -259,9 +274,10 @@ def DELETE(self, url):
259
274
"""
260
275
path = self ._path_from_url_or_path (url )
261
276
self .context .pre_process_request ()
262
- logger .debug ("Performing DELETE request on %s://%s%s" % (self .context ._scheme , self .app .host , url ))
277
+ logger .debug ("Performing DELETE request on %s://%s%s" % (
278
+ self .context ._scheme , self .app .host , url ))
263
279
self .context .post_process_request (
264
- self .app .delete (path , {}, self .context .request_headers )
280
+ self .app .delete (path , {}, self .context .request_headers )
265
281
)
266
282
267
283
def follow_response (self ):
@@ -272,13 +288,13 @@ def follow_response(self):
272
288
273
289
if location is None :
274
290
self .log_response_headers ('INFO' )
275
- raise Exception ("Can not follow a response without a location header." )
291
+ raise Exception (
292
+ "Can not follow a response without a location header." )
276
293
277
294
logger .debug ("Following response, last response's Location header was %s" % location )
278
295
279
296
self .context .response = self .response .follow ()
280
297
281
-
282
298
def next_request_may_not_succeed (self ):
283
299
"""
284
300
Don't fail the next request if it's status code is >= 400
@@ -329,7 +345,8 @@ def response_status_code_should_equal(self, status_code):
329
345
`status_code` the status code to compare against.
330
346
"""
331
347
assert self .response .status .startswith (status_code ), \
332
- '"%s" does not start with "%s", but should have.' % (self .response .status , status_code )
348
+ '"%s" does not start with "%s", but should have.' % (
349
+ self .response .status , status_code )
333
350
334
351
def response_status_code_should_not_equal (self , status_code ):
335
352
"""
@@ -339,7 +356,8 @@ def response_status_code_should_not_equal(self, status_code):
339
356
`status_code` the status code to compare against.
340
357
"""
341
358
assert not self .response .status .startswith (status_code ), \
342
- '"%s" starts with "%s", but should not.' % (self .response .status , status_code )
359
+ '"%s" starts with "%s", but should not.' % (
360
+ self .response .status , status_code )
343
361
344
362
# response headers
345
363
@@ -348,14 +366,14 @@ def response_should_have_header(self, header_name):
348
366
Fails if the response does not have a header named `header_name`
349
367
"""
350
368
assert header_name in self .response .headers , \
351
- 'Response did not have "%s" header, but should have.' % header_name
369
+ 'Response did not have "%s" header, but should have.' % header_name
352
370
353
371
def response_should_not_have_header (self , header_name ):
354
372
"""
355
373
Fails if the response does has a header named `header_name`
356
374
"""
357
375
assert not header_name in self .response .headers , \
358
- 'Response did have "%s" header, but should not have.' % header_name
376
+ 'Response did have "%s" header, but should not have.' % header_name
359
377
360
378
def get_response_header (self , header_name ):
361
379
"""
@@ -376,8 +394,8 @@ def response_header_should_equal(self, header_name, expected):
376
394
self .response_should_have_header (header_name )
377
395
actual = self .response .headers [header_name ]
378
396
assert actual == expected , \
379
- 'Response header "%s" should have been "%s" but was "%s".' % (
380
- header_name , expected , actual )
397
+ 'Response header "%s" should have been "%s" but was "%s".' % (
398
+ header_name , expected , actual )
381
399
382
400
def response_header_should_not_equal (self , header_name , not_expected ):
383
401
"""
@@ -387,8 +405,8 @@ def response_header_should_not_equal(self, header_name, not_expected):
387
405
self .response_should_have_header (header_name )
388
406
actual = self .response .headers [header_name ]
389
407
assert actual != not_expected , \
390
- 'Response header "%s" was "%s" but should not have been.' % (
391
- header_name , actual )
408
+ 'Response header "%s" was "%s" but should not have been.' % (
409
+ header_name , actual )
392
410
393
411
def log_response_headers (self , log_level = 'INFO' ):
394
412
"""
@@ -409,7 +427,8 @@ def set_request_header(self, header_name, header_value):
409
427
`header_name` is the name of the header, e.g. `User-Agent`
410
428
`header_value` is the key of the header, e.g. `RobotFramework HttpLibrary (Mozilla/4.0)`
411
429
"""
412
- logger .info ('Set request header "%s" to "%s"' % (header_name , header_value ))
430
+ logger .info (
431
+ 'Set request header "%s" to "%s"' % (header_name , header_value ))
413
432
self .context .request_headers [header_name ] = header_value
414
433
415
434
def set_basic_auth (self , username , password ):
@@ -424,7 +443,8 @@ def set_basic_auth(self, username, password):
424
443
"""
425
444
credentials = "%s:%s" % (username , password )
426
445
logger .info ('Set basic auth to "%s"' % credentials )
427
- self .set_request_header ("Authorization" , "Basic %s" % b64encode (credentials ))
446
+ self .set_request_header (
447
+ "Authorization" , "Basic %s" % b64encode (credentials ))
428
448
429
449
# payload
430
450
@@ -460,10 +480,12 @@ def response_body_should_contain(self, should_contain):
460
480
| Response Body Should Contain | version="1.0" |
461
481
| Response Body Should Contain | encoding="UTF-8" |
462
482
"""
463
- logger .debug ('Testing whether "%s" contains "%s".' % (self .response .body , should_contain ))
483
+ logger .debug ('Testing whether "%s" contains "%s".' % (
484
+ self .response .body , should_contain ))
464
485
465
486
assert should_contain in self .response .body , \
466
- '"%s" should have contained "%s", but did not.' % (self .response .body , should_contain )
487
+ '"%s" should have contained "%s", but did not.' % (
488
+ self .response .body , should_contain )
467
489
468
490
def log_response_body (self , log_level = 'INFO' ):
469
491
"""
@@ -484,7 +506,8 @@ def log_response_status(self, log_level='INFO'):
484
506
Specify `log_level` (default: "INFO") to set the log level.
485
507
"""
486
508
487
- logger .write ("Response status line: %s" % self .response .status , log_level )
509
+ logger .write (
510
+ "Response status line: %s" % self .response .status , log_level )
488
511
489
512
# json
490
513
@@ -524,7 +547,8 @@ def stringify_json(self, data):
524
547
try :
525
548
return json .dumps (data )
526
549
except ValueError , e :
527
- raise ValueError ("Could not stringify '%r' to JSON: %s" % (data , e ))
550
+ raise ValueError (
551
+ "Could not stringify '%r' to JSON: %s" % (data , e ))
528
552
529
553
@_with_json
530
554
def get_json_value (self , json_string , json_pointer ):
@@ -550,8 +574,8 @@ def json_value_should_equal(self, json_string, json_pointer, expected_value):
550
574
got = self .get_json_value (json_string , json_pointer )
551
575
552
576
assert got == expected_value , \
553
- 'JSON value "%s" does not equal "%s", but should have.' % (got , expected_value )
554
-
577
+ 'JSON value "%s" does not equal "%s", but should have.' % (
578
+ got , expected_value )
555
579
556
580
def json_value_should_not_equal (self , json_string , json_pointer , expected_value ):
557
581
"""
@@ -571,7 +595,6 @@ def json_value_should_not_equal(self, json_string, json_pointer, expected_value)
571
595
572
596
logger .debug ("%s." % message )
573
597
574
-
575
598
@_with_json
576
599
def set_json_value (self , json_string , json_pointer , json_value ):
577
600
"""
@@ -583,10 +606,10 @@ def set_json_value(self, json_string, json_pointer, json_value):
583
606
| Should Be Equal | ${result} | {"foo": 12} | | |
584
607
"""
585
608
return jsonpatch .apply_patch (json_string , [{
586
- 'op' : 'add' ,
587
- 'path' : json_pointer ,
588
- 'value' : load_json (json_value )
589
- }])
609
+ 'op' : 'add' ,
610
+ 'path' : json_pointer ,
611
+ 'value' : load_json (json_value )
612
+ }])
590
613
591
614
@_with_json
592
615
def log_json (self , json_string , log_level = 'INFO' ):
0 commit comments